2  Simple Data types

Download notebook.

2.1 Overview

Every variable or other object in Python has a type. This is the kind of information which is stored insice the variable or object Here is an overview over the most important simple data types.

Type Example Description
int 5 Integer
float 3.14 Floating-point number
bool True Boolean value
str "hello" Text string
NoneType None No value / null value
  • You can find about the type of any expression x in Python using type(x).
  • In fact, type(x) is already the first example of a built-in-function. The function type takes a single value as input, and returns its type. We will encounter various more functions already in this section. We will learn more about functions (built-in and self-written) in Section 3.4.
  • There are conversions between some types. For example, "0" is a string, but int("0") (again a function, taking a single value, and returning its integer value) the integer 0. Conversely, str(0) is the string "0". We will have more examples below.
  • You can optionally annotate variables with their type using type hints, e.g. x: int = 5. Python does not enforce these annotations, but they help readability. See Section 11.5 for more details.
  • We will use print() throughout to display output. It takes one or more values and prints them to the screen.
x: int = 5
y: float = 3.14
name: str = "Alice"
print(type(x), type(y), type(name))
<class 'int'> <class 'float'> <class 'str'>

2.2 Integers (int)

With ints (numbers …,-2,-1,0,1,2,… ), you can compute as usual.

x = 7
y = 2
print("x + y = ", x + y)
print("x * y =  ", x * y)
x + y =  9
x * y =   14
# x to the power of y
print("x ** y = ", x ** y)
x ** y =  49
# x divided by y (which gives a `float`)
print("x / y = ", x / y)
x / y =  3.5
# integer-valued division of x by y 
print("x // y = ", x // y)
x // y =  3
# x modulo y
print("x % y = ", x % y)
x % y =  1

In most programming languages (e.g. C++), there is a minimal and a maximal possible value for an int. Python, however, does not have this limitation. Only the amount of available memory restricts the numbers which can be stored in an int.

2.3 Floating point numbers (float)

Generally speaking, floats are decimal numbers, which come with an exponent and a mantissa, e.g. \(1.2 \cdot 10^2\). Python can do the same computations as with ints, but the following should be kept in mind:

  • If you type 1, Python thinks this is an int, but if you type 1.0 or 1., Python thinks this is a float.
  • Unlike ints, floats do have a maximal and minimal element. The reason is that every float is stored in 64 bits, one for the sign, 11 for the exponent and 52 for the mantissa. As a result, floats range from about \(10^{-324}\) to \(10^{308}\).
  • When doing mixed computations with floats and int, e.g. 1.0 + 1, the int is changed into a float.
  • It is not possible to perform exact computations with some floats; see the example below.
x = 1.0
y = 1
print("x + y = ", x + y)
x + y =  2.0
print("0.1 + 0.2 = ", 0.1 + 0.2)
0.1 + 0.2 =  0.30000000000000004

Special floats are inf, -inf and nan (not a number). Let us see them in action:

print((10. ** 308) * 10)
inf
print((10 ** 308) * 10)
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
print((10 ** (-323)) / 10)
0.0
print((10. ** 308) * 10 - (10. ** 308) * 10)
nan

You can transform ints to floats and vice versa by using float() and int().

print(float(0))
0.0
print(int(0.0))
0
print(int(0.5))
0

If you deliberately want to introduce nan or inf, you can do this:

print(float("inf"))
print(float("nan"))
inf
nan

On float, you can use many functions. We only name two at the moment, for some float x:

  • abs(x): The absolute value of x.
  • round(x) and round(x,n): Round x to the next int, or after n digits.

In Section 5.2, we will encounter that many more usual functions (like sqrt) can be used on floats, but we need to import the mathematical library for this.

2.4 True-False variables (bool)

There are exactly two boolean values, known as True and False. You can compute with them using not, and, or and ‘xor’. In addition, True and False can be the result of a condition, such as the expression 1 == 2.

x = True
y = False
print("not x =", not x)
not x = False
print("x and y =", x and y)
x and y = False
print("x or y =", x or y)
x or y = True
# xor (exclusive or, written as ^)
print("x xor y =", x ^ y)
x xor y = True
print("((not x) == y) =", (not x) == y)
((not x) == y) = True
print("(1 == 2) =", 1 == 2)
(1 == 2) = False
# Python converts `bool` into `int`
print("x + y =", x + y)
x + y = 1
# != means "not equal"
print("(1 != 2) =", 1 != 2)
(1 != 2) = True
# < refers to less than
print("(1 < 1) =", 1 < 1)
(1 < 1) = False
# <= refers to less than or equal
print("(1 <= 1) =", 1 <= 1)
(1 <= 1) = True
z = float("nan")
print("(z == z) =", z == z)
(z == z) = False

2.5 Text (string)

In many cases, strings are objects in quotation markes, such as in `“Hello, world!”.

  • Operations on strings: You can concencatenate strings using + as in `“Python” + “for” + “Data” + “Analysis” (but you cannot subtract strings). You can also repeat a string by multiplying it with an int.
  • f-strings: They were introduced in Python 3.6. They are the way to go if you want to mix description and variables. They are written as f"Hello, {name}". The f introduces the f-string. Within curly braces, there is a variable, which should be printed instead of the variable name.
  • Special characters: There are many of them, we only mention \t (tabulator) and \n (new line) here.
  • Multiline strings: Usually, Python interprets a new line such that all commands are finished. However, if you want to use a sttring spanning multiple lines, you don’t want this. Multiline strings start and end with """ (a triple quotation mark).
  • Quotation marks: In various cases, you can use ' instead of ". Sometimes this is useful when nesting quotation marks.
  • Substrings: We will learn more about substrings, but here is already a way to see if a substring occurs within a string or not. The value of "th" in "Python" amount to True.
x = "Hello," 
y = "world"
# Concatenating two (or more) strings
print(x + y + "!")
Hello,world!
# repeating a string 
z = "bla"
print(z*3)
blablabla
# a special character
print("\U0001F60E")
😎
# an example of an f-string
name = "Alice"
print(f"Hello {name} 😀")
Hello Alice 😀
# a multiline string
text = """This is a multiline string.
It can span several lines.
Each line break is preserved."""
print(text)
This is a multiline string.
It can span several lines.
Each line break is preserved.
# determine if a given substring occurs within a string
print("Is 'th' a substring of 'Python'? ", 'th' in 'Python')
print("Is 'the' a substring of 'Python'? ", 'the' in 'Python')
Is 'th' a substring of 'Python'?  True
Is 'the' a substring of 'Python'?  False

For strings, there are various build-in functions. We mention some of them. Here, s is a string.

  • str.strip(s): Deletes spaces in the front and back.
  • len(s): gives the length of the string.
  • str.replace(s, "x", "y"): Replaces each occurrence of "x" in s by "y".
  • str.split(s, "x"): Splits s at each occurrence of "x". The result is a list, which we will encounter in Section 4.1.
  • ", ".join(list): Joins the elements of a list into a single string, separated by the given string.
  • str.lower(s): Return the same string, but all letters are lowercase.
  • str.upper(s): Return the same string, but all letters are uppercase.
  • str.isalpha(s): Returns True if the string consists only of alphabetic letters, and False otherwise.
  • str.isdigit(s): Returns True if the string is actually a number, and False otherwise.
  • str.isalnum(s): Returns True if the string consists only of letters and numbers, and False otherwise.
  • str.isspace(s): Returns True if the string consists only empty spaces, and False otherwise.
  • str.islower(s): Returns True if the string consists only of lower case letters, and False otherwise.
  • str.isupper(s): Returns True if the string consists only of upper case letters, and False otherwise.
  • str.count(s, "x"): Counts how ofter "x" appears in s.

Note that for a function str.something(s,...), you can also write s.something(). For example, s.strip() is the same as str.strip(s). This is called dot-notation and is explained in Section 11.4.

2.5.1 Formatting numbers in f-strings

When printing numerical results, we often want to control the number of decimal places, alignment, or padding. Inside an f-string, a format specifier follows the variable after a colon: f"{x:.4f}".

  • f"{x:.4f}": float with 4 decimal places.
  • f"{x:.2e}": scientific notation with 2 decimal places.
  • f"{x:>10}": right-align in a field of width 10.
  • f"{x:<10}": left-align in a field of width 10.
  • f"{n:,}": integer with thousands separator.
  • f"{x:.2%}": format as percentage with 2 decimal places.
pi = 3.141592653589793
print(f"pi = {pi}")
print(f"pi = {pi:.2f}")
print(f"pi = {pi:.2e}")
print(f"123456 = {123456:,}")
print(f"pi = {pi:.2%}")
pi = 3.141592653589793
pi = 3.14
pi = 3.14e+00
123456 = 123,456
pi = 314.16%

2.6 Missing things (NoneType)

When analysing data, it is common to have missing values. These usually come as None. This is different from anything else we encountered so far (including nan and False).

x = None
print(x)
None

The is operator checks whether two variables refer to the same object in memory, as opposed to == which checks whether they have the same value. For None, always use is rather than ==.

print("(x is None) =", x is None)
(x is None) = True
print("(None == False) =", None == False)
(None == False) = False
print("(None == nan) =", None == float("nan"))
(None == nan) = False

2.7 Exercises

Exercise 1 Find out how to use scientific notation in Python. (If you don’t know what this is, please find out!)

# Exercise 1

Exercise 2 Is there a Python package which can compute 0.1 + 0.2 == 0.3 without error?

# Exercise 2

Exercise 3 Print "Hello, world!" including the quotation marks in the output.

# Exercise 3

Exercise 4 If you are on Windows, it is annoying that file paths come with \, which is interpreted as the start of a special character. Find out two ways how to give the string of the file path C:\Users\Alice\Documents\file.txt.

# Exercise 4

Exercise 5 What is bool(None)? What is int(None)?

# Exercise 5

Exercise 6 In tables, we often have "" for a missing value. Is this the same as None?

# Exercise 6

Exercise 7 Given a float x, write a one-liner that rounds x to 3 decimal places without using round. (Hint: use arithmetic with int().)

# Exercise 7