Python: 05 Numbers and Operators ( + - / * )
Numbers work as you would expect.
Note that 2 and 2.0 are considered different types.
2 is an integer and 2.0 of a double. You can also specify a floating point number by including an f after the number 2.0f
When carrying out calculations you should always try to have numbers of the same type.
We use the following operators in Python (note the differences for division, when programming in other languages check how they handle modulo and whole number division)
- + addition
- - subtraction
- * multuiplication
- / regular division e.g. 3/2 gives 1.5
- // floor division (how many whole times a number can be divided) e.g. 5/3 gives 1
- % modulo or remainder division e.g. 5/3 gives the result 2
- ** exponents
Note that Python uses BEDMAS to carry out operations.
num1 = 5
num2 = 6
addition = num1 + num2 # stores 11
subtraction = num1 + num2 # stores -1
multiplication = num1 + num2 # stores 30
exponent = 2 ** 3 # stores 8
division = 3 / 2 # stores 1.5
remainder = num2 % num1 # stores 1
whole_number_division = 7 // 2 # stores 3