Types

Resources

Types

In a computer language variables have types. For instance, we have already used numbers and text strings.

We can ask for the type of variable (or object).

The type for the number 42 is int and for the text string “My favorite number is” is str. In most computer languages text is called string, or something similar, because, for the computer, a text is a string of characters.

Number types: int and float

In the previous example the type for the number was int (integer), but, in Python, and most other languages, we have another very important numeric type: float (floating point number).

The main practical difference between the integer and float types is that the arithmetic for integers will be exact, but for float will be only approximate. This is not a Python quirk, but a general feature of the computers.

Another practical limitation, besides the inherent error due to the approximation, of the floating point arithmetic is that you have to be careful when you compare floats.

Other types: bool and None

bool (boolean) is a special type that can only have two values: True or False.

Another special type that you will find quite often in Python code is None. This type can only have one value: None. It is usally employed when you want to have a variable that still has no value, it signifies that it is empty.

Python is dynamic

In Python we don’t need to specify the type of a variable before using it, and we can even change the type of object that the variable refers to. That is not the case in other languages, like C or Rust. In those static languages the compiler needs to know the type of the variable beforehand, and the type of a variable can not be changed.

Modern Python has the option of specifying the types. This is not a requirement, but you will see some type hints in Python code.

Be careful because Python does not enforce those type hints. If you wanted to use them for anything else than documentation you would need a type checker like mypy. But if you are starting in programming, just forget about this, the idea to remember is that the objects refered to by the variables have types.

Type casting

Type casting or type conversion is the action of changing one type into another. Python has functions to do this type casting.

We can also type cast to boolean.

Find out which int and str values will be True and False when converted to boolean.

Note

Try with different int and str values, like 0, 1, 2, 3, “Hello”, ” “,”“.

Tip

Try to convert a boolean to integer.

Tip

Why do we need types?

For a programming language using types is unavoidable. Different languages have different types, but they all have types. Why? Because, internally, the only thing that a computer can really stores in memory is a binary number, and the same binary number can be interpreted as different objects depending on its type.

flowchart LR
    subgraph memory
        0b110100
    end
    int["as int: 52"]
    0b110100 --> int
    float["as float: 7.3e-44"]
    0b110100 --> float
    str["as str: '4'"]
    0b110100 --> str