Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2.2 Built-in types

All general-purpose languages provide the programmer with different types of variables as the basic building blocks of programs.

Integers

Integers are the numbers zero (0), positive natural numbers (1, 2, 3, ...), or the negation of positive natural numbers (-1, -2, -3, ...)

You can perform mathematical operations like addition

30

or division

2.0

Note: As already mentioned above, if we execute Python code in interactive notebooks (.ipynb files), the print() statement can be omitted if the variable to be printed is in the last line of the code block. We will use both options from time to time, as the automatic printing will perform some automatic formatting and sometimes one or the other will look nicer. However, please note that you will need to write the print statement explicitly if you work with normal Python (.py) scripts.

Floating point numbers

Notice that while the previous addition of two integers resulted in another integer, the division resulted in a number with a decimal point. The latter is what we call a float (short for floating point number), which is a way computers represent real numbers.

All of the standard arithmetic operations that work on integers also work on floats (or on any combination of them):

The circumference of the circle is 12.56

Strings

Strings are sequences of characters. In Python, we can define strings by enclosing zero or more characters in a pair of quotes. It does not matter whether you use single or double quotes and both work equally well as long as the opening and closing quotes match.

There are many built-in functions you can use on strings, like figuring out their length:

6

Or converting them to lower case:

'oldenburg'

We can even replace a substring with another substring:

'Oldencastle'

One thing you might have noticed is that these examples seem to use two different syntaxes. In the first example, len() seems to be a function which takes a string as its parameter (or argument), while in the other examples the function comes after the string with a dot notation .lower(). If this is a bit confusing do not worry, we will talk about this difference a bit later.

Another useful thing about strings is that you can use formatted strings (f-strings) to nicely format strings when printing results. For this you can just add an f before the opening quotation marks of the string and you can then print the value of any variable by enclosing it with curly brackets {} in the middle of your string:

The human brain has 86 billion neurons.

You can also do many more things like formatting the number of decimal points shown for a number. See for example the Python documentation for more information: https://docs.python.org/3/tutorial/inputoutput.html.

Booleans

Handling Boolean values in Python is pretty much the same as in other programming languages. Boolean values can only take the value True (corresponding to 1) or False (corresponding to 0) and not other versions like true or "False":

True

One of the ways Boolean values are typically generated in Python is through logical or comparison operations. For example, the statement “5 is larger than 3” can be answered in a binary way (it is either true or false):

True

Similarly, if we want to compare two numbers, this is also a logical operation that returns a Boolean value:

False

But what if the question you are trying to ask is not so simple? Python lets you build conjunctions of several subexpressions:

True

In logical operations, and requires ALL statements to be true, which is the case here. Alternatively, or requires only one of the statements to be true:

True

This expression still returns True even though the last comparison is false due to it being joined with the previous expression through or. This brief example hopefully illustrates nicely how the Python syntax is more readable than most other programming languages.

Note: The round brackets are not strictly necessary here, as Python will evaluate this expression left to right, following the logical and comparison operator precedence rules. It is simply a design choice that subjectively enhances readability.

None

So what if you want to create a variable but not assign a specific value to it? This is where None comes in handy. None represents the absence of a value. Please note that it is not the same as NaN (not a number), which is a special floating point value used for missing numerical data (you will encounter it later in NumPy and Pandas). Similarly, None and False are not the same thing!

False