Operations

We now move to operations. Operations allow us to describe values indicating that something has to be calculated. Operations are specific to the different types, even if some of them mix types (for example comparing numbers results in a truth value!).

It is important that you understand in what order the calculations are done when you use more than one operation to form an expression. This is guided (like in math) by what we call operator precedence, operator associativity and the order of evaluation. In the book this is described in detail in an appendix. In the Python documentation this is described in chapters 6.16 and 6.17

Operations for integer numbers

You are encouraged to experiment with expressions that use several operators, for example 3 + 5 - 4 / 2, to understand precedence, associativity and the order of evaluation. In order to express the calculation that you want you might need to use paretheses to make it clear what operator has to be used first.

3 + 4
7
3 - 4
-1
3 + 4 - 5
2
3 - 4 - 5
-6
3 - (4 - 5)
4
# this operation converts (casts) 3 and  2 to float 
# and results in a float!

3 / 2
1.5
# this operation returns the quotient of integer division.

123456789 // 10
12345678
# this operation returns the remainder  of integer division.

123456789 % 10
9
2 ** 32
4294967296
# These operations result in a boolean value.
# Make sure you agree to the values in the six-tuple!

(3 == 3, 3 != 3, 3 < 3, 3 > 3, 3 <= 3, 3 >= 3)
(True, False, False, False, True, True)

Operations for floating point numbers

You are encouraged to experiment with expressions that use several operators, for example 3 + 5 - 4 / 2, to understand precedence, associativity and the order of evaluation. In order to express the calculation that you want you might need to use paretheses to make it clear what operator has to be used first.

3.141592653589793 + 2.0
5.141592653589793
0.123456789e2 - 1.5
10.8456789
1.4142135623730951 * -1.4142135623730951
-2.0000000000000004
10.3 / 1.234567
8.343006090394447
1.4142135623730951 ** 2.0
2.0000000000000004
# These operations result in a boolean value.
# Make sure you agree to the values in the six-tuple!

(3.1 == 3.1, 3.1 != 3.1, 3.1 < 3.1, 3.1 > 3.1, 3.1 <= 3.1, 3.1 >= 3.1)
(True, False, False, False, True, True)

Operations for booleans

For the order of evaluation see boolean operations

True and False
False
True or False
True
not True
False
3 > 3 and 3 == 3 or 3 >= 3
True
123456 % 3 == 0
True
12345 % 3 == 0
True
1234 % 3 == 0 or 1234 % 2 == 0 
True

Operations for strings

# + is append!

'Hello to you, ' + 'vero!'
'Hello to you, vero!'
# string * int: use + as many time as the number indicates

10 * '-'
'----------'
# Make sure you learn about precedence, associativity 
# and the order of evaluation

3 * 'this, and ' + 'that!'
'this, and this, and this, and that!'
# string[int] results in the character (string) 
# in the position given by the number.
# The first position is 0.

('this'[0],'this'[1],'this'[2],'this'[3])
('t', 'h', 'i', 's')
# Negative numbers can be used as positions.

'this'[-1]
's'

Operations for tuples

The operation tuple[int] results in the element in the tuple in the position indicated by the number. Positions start with 0. Negative numbers can be used as positions: -1 is the position length of tuple - 1 (the last position in the tuple).

(3.141592653589793, 'Pi!', True)[0]
3.141592653589793
(3.141592653589793, 'Pi!', True)[1]
'Pi!'
(3.141592653589793, 'Pi!', True)[2]
True
(3.141592653589793, 'Pi!', True)[-1]
True

Operations for lists

Again, selecting the element for a position, same as for strings and tuples.

But also slicing: list[start:end] results in a list with the elements at positions start upto end - 1. The end points of the slice can be omitted, see the examples below.

+ and * int as for strings.

Finally, list comprehensions (hopefully familiar from math: set comprehensios).

[1,2,3,4][0]
1
[1,2,3,4][-1]
4
[1,2,3,4][1:3]
[2, 3]
[1,2,3,4][:3]
[1, 2, 3]
[1,2,3,4][1:]
[2, 3, 4]
[1,2,3,4][:-1]
[1, 2, 3]
[1,2,3,4] + [4,3,2,1]
[1, 2, 3, 4, 4, 3, 2, 1]
10 * [0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# Your first list comprehension.
# Read it as:
#      The list of x square for all values x in the list [1,2,3,4]

[x ** 2 for x in [1,2,3,4]]
[1, 4, 9, 16]
# Your second list comprehension.
# Read it as:
#      The list of x in the list [1,2,3,4] if x is even

[x for x in [1,2,3,4] if x % 2 == 0]
[2, 4]
# Your third list comprehension.
# Read it as:
#      The list of x square for x in the list [1,2,3,4] if x is even

[x ** 2 for x in [1,2,3,4] if x % 2 == 0]
[4, 16]