8.10.2. Numbers and math¶
8.10.2.1. Python arithmetic operators:¶
Here is an overview about the basic math operators and how they are used in Python v3. You can type simple math equations (e.g. the examples from the table below) directly into the itom terminal to see how some of the operators work. Alternatively, you can create yourself a new Python script file and type in some math there.
Operator 
Description 
Example 


Addition 
10 + 20 will give 30 

Subtraction 
10  20 will give 10 

Multiplication 
10 * 20 will give 200 

Division 
20 / 10 will give 2 

Modulus 
20 % 10 will give 0 

Exponent 


Floor division 

8.10.2.2. Python comparison/boolean operators:¶
In addition, comparison operators are shown in the following table. As displayed in the example column, they mainly serve for the comparison of two numbers and return a boolean value  true or false.
Operator 
Description 
Example 


Equal 
(10 == 20) is not true 

Not equal 
(10 != 20) is true 

Not equal 
(10 <> 20) is true 

Lessthan 
(10 < 20) is true 

Greaterthan 
(10 > 20) is not true 

Lessthanequal 
(10 <= 20) is true 

Greaterthanequal 
(10 >= 20) is not true 
8.10.2.3. More examples¶
Here are a few more examples to practice for yourself how the different operators work. Type in the following source code in another script file and run the code to see what happens.
1 2 3 4 5 6 7 8  # Python calculates with the correct order of operations
print(25 + 30 / 6)
print(100  25 * 3 % 4)
print(3 + 2 + 1  5 + 4 % 2  1 / 4 + 6)
# Difference between regular division (/) and floor division (//)
print(8/5)
print(8//5)

30.0
97
6.75
1.6
1
You can also combine your math/arithmetic operations with the print()
command
1 2 3 4 5 6  print("Is it true that 3 + 2 < 5  7?") # outputs all characters in between the quotation marks
print(3 + 2 < 5  7) # prints the actual result
print("Is 5 greater than 7?", 5 > 2) # combines the previous example in one line
print("What is 3 + 2?", 3 + 2 )

Is it true that 3 + 2 < 5  7?
False
Is 5 greater than 7? True
What is 3 + 2? 5
As you might have noticed, lines starting with the hash character #
are not interpreted by Python. In this fashion, the programmer can render his file easier understandable for another programmer or for himself when coming back to a complex program after a while. Comments can also start behind a line of code. They extend until the end of the physical line.
Also, you can combine the print()
command with mathematic/arithmetic operations, which is explained in more detail in chapter Strings and text.
See also
You can find even more math functions in the official documentation.