Data types

Precap

Different data types (e.g. text, numbers, calendar dates, etc.) offer different possibilities regarding what they can be used for and how they can be manipulated. There are built-in data types that are always available and data types that have to be imported (= explicitly included in the code) before they can be used. Common built-in data types are:

  • Boolean: bool (either True or False)

  • Numbers: int (whole numbers), float (real numbers), complex (complex numbers)

  • Sequences: str (text), tuple (immutable list), list (mutable list)

  • Mappings: dict (key:value pairs)

An example for a non built-in data type that can be imported from the standard library is:

Boolean

The bool data type handles (named after George Boole) only two possible values: True or False. It is used for boolean expressions in e.g. if and while statements (see next section) or as value in boolean operations.

Boolean expression are expressions with `True` or `False` as the result

Boolean expression can use value comparison operators like less than <, greater than >, equal ==, not equal !=, less or equal than <=, and greater or equal than >=. The membership test operator in can be used if a value is part of another value. Note the double equal sign == for testing the equality of two values (the single equal sign = is used for assignments like x = 1).

There are only 3 basic operations in Boolean algebra: not, or, and

Integer

The int data type handles whole numbers (= no radix point) with unlimited length.

int examples

In contrast to other programming languages which have an upper and lower limit for integer values because of the way they are stored internally, Python has no such limit.

Float

The float (short for floating point number) data type handles real numbers (= with radix point) within a limited range and precision (+1.7976931348623157e+308 .. -2.2250738585072014e-308). Since the float (and int) data type is actually converted to and stored as a binary number, the part right to the radix dot . of a float most of the time cannot always be converted exactly from decimal to binary, which leads to (usually insignificant) inacurracies and non-obvious results.

float examples

In Python 2 (and many other programming languages) the result of a division between two int numbers also was an int . Which leads to non-obvious calculations like 3 / 4 returns 0 instead of 0.75.

The limited precision of a float can lead to non-obvious results

The internal conversion from the decimal (= base 10) to the binary (= base 2) number system exposes the limited accuracy of a float.

Complex

Note

Feel free to ignore complex numbers, if You are not familiar with their concept and when they are helpful.

The complex data type is written in the form of a + bj, where a and b are real or int numbers and j is the symbol for the imaginary unit (which is the result of the equation j2 = -1).

Complex number example

String

The str data type handles sequences of charcters. They have to be delimited with either single-quotes ', double-quotes ", or, in the case of a docstring, in triple-double-quotes """ or (not recommended) triple-single-quotes '''.

String are sequences of characters

Tuple

The tuple data type handles sequences of values separated by commas , enclosed in round () brackets. They are useful if more than one value needs to be stored in a single variable. Tuples are immutable which means they cannot be altered (elements cannot be modified, removed or added after the tuple has been created). Any operation on a tuple leaves the original untouched and creates a new one.

Tuples are immutable sequences of values

List

The list data type is like a tuple, but mutable (it can be altered) and enclosed in square [] brackets.

List examples

Dictionary

The dict data type is a mapping, which is a sequence of key: value pairs separated by commas , enclosed in curly {} brackets. Dictionary values can be accessed by their key.

Dict examples

Date & time

Some data types are part of Python 3 in a way that You cannot use them right away but have tell Python that You want to use them. That process is called importing a module or parts of it.

Datetime examples

References

https://docs.python.org/3/library/stdtypes.html
https://docs.python.org/3/tutorial/floatingpoint.html