Common data types

Like many other programming languages, Python has built-in data types that the programmer uses to create a program. These data types are the building blocks of the program. Depending on the language, different data types are available. Some languages, notably C and C++, have very primitive types; a lot of programming time is spent simply combining these primitive types into useful data structures.

Python does away with a lot of this tedious work. It already implements a wide range of types and structures, leaving the developer more time to actually create the program. Having to constantly recreate the same data structures for every program is not something to look forward to.

Python has the following built-in types:

  • Numbers
  • Strings
  • Lists
  • Dictionaries
  • Tuples
  • Files
  • Sets
  • Databases

In addition, functions, modules, classes, and implementation-related types are also considered built-in types, because they can be passed between scripts, stored in other objects, and otherwise treated like the other fundamental types.

Naturally, you can build your own types if needed, but Python was created so that very rarely will you have to roll your own. The built-in types are powerful enough to cover the vast majority of your code and are easily enhanced.

It was mentioned previously that Python is a dynamic typed language; that is, a variable can be used as an integer, a float, a string, or whatever. Python will determine what is needed as it runs. The following screenshot shows how variables can be assigned arbitrarily. You can also see that it is trivial to change a value, as shown in line 16:

Dynamic typing

Other languages often require the programmer to decide what the variable must be when it is initially created. For example, C would require you to declare x in the preceding program to be of type int and y to be of type string. From then on, that's all those variables can be, even if, later on, you decide that they should be a different type.

That means you would have to decide what each variable will be when you started your program; that is, deciding whether a number variable should be an integer or a floating-point number. Obviously, you could go back and change them at a later time, but it's just one more thing for you to think about and remember. Plus, any time you forgot what type a variable was and you tried to assign the wrong value to it, you would get a compiler error.

Python also has a difference between expressions and statements. In Python, an expression can contain identifiers (names), literals (constant values of built-in types), and operators (primarily arithmetic-looking symbols, but can be other items, such as [], (), or other symbols). Expressions can be reduced to a derived value, much like solving a math equation.

Statements, on the other hand, are everything that can make up a line (or multiple lines) of code; that is, they actually perform the programming logic. Statements can contain expressions. In other words, lines that equate to a value are expressions, whereas lines that actually do something are statements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.233