Basics · Lesson 5 of 27
Maths operators
Python works like a calculator.
a = 17 b = 5 print(a + b) # 22 add print(a - b) # 12 subtract print(a * b) # 85 multiply print(a / b) # 3.4 divide (always gives a float) print(a // b) # 3 floor division: divide and drop the decimal part print(a % b) # 2 remainder (modulo) print(a ** 2) # 289 power: 17 squared
% is used a lot. n % 2 == 0 means n is even. n % 10 gives the last digit of n.
Brackets work like in school maths: (2 + 3) * 4 is 20, while 2 + 3 * 4 is 14.
Your task
The variables a = 17 and b = 5 are given. Print these seven results, one per line, in this order:
a + b, a - b, a * b, a / b, a // b, a % b, a ** b