Python 4b - Mathematical Operators
Common Mathematical Operators
When comparing two values, like in an if statement, there are a number of operators than can be used:

In the example below the print line will only be executed if the temperature value is greater than 25:
If we entered 25 then the print line beneath would not run because is not above 25.
Alternatively, you can use the greater than or equal to sign ( >= ) :


You can use the less than ( < ) and less than or equal to sign ( <= ) in a similar way (and implement an elif at the same time):

Now there’s only one thing missing from this program – we need an else statement for any value that is inputted between:

Can you work out which sentences would be printed if we entered 8, 10 and 24?
The opposite of equal to ( == ) is not equal to ( != ). For example:

Practice Task
A school is putting on a quiz for sixth form students.
Students enter their age and they will receive a ticket to the quiz if it is between (and including) 16 to 18.
Example solution:

Modulo Division to Find the Remainder
The modulo operator - the percentage symbol % - will work out the remainder left over when one value is divided by another.
print(30 % 6)
=
0
print(30 % 7)
=
2
30 ÷ 6 = 5, which is a whole number, so there is no remainder and 0 is output.
30 ÷ 7 = 4 remainder 2; so the remainder is output.
You can use modulo with variables too:
num1 = 33
num2 = 4
print("The remainder is" , num1 % num2)
The remainder is 1
=
A common use of modulo is to check if a number is odd or even.
If a number has no remainder when divided by 2 then it is even.
=
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even.")
else:
print(num , "is odd.")
Enter a number: 400
400 is even.
Enter a number: 191
191 is odd.
=
Modulo Division Task 1 (Remainder)
Ask the user to input a whole number.
Use the modulo operator (%) to check if there is a remainder when the user's number is divided by 5. Print the remainder.
Example solution:
Enter a number: 123
The remainder when divided by 5 is 3
Modulo Division Task 2 (Rollercoaster)
Use the odd/even program above to help solve this problem:
A rollercoaster only lets people on in groups of 4.
Ask the user to input a number for how many people are in their group.
Check if that number is directly divisible by 4 using modulo division (%). If it is then print “Perfect groups of four!” Else print “You will be split up”.
Example solutions:
Welcome to the Hyper Coaster!
How many in your group? 6
You will be split up!
Welcome to the Hyper Coaster!
How many in your group? 12
Perfect groups of four!
Integer Division
Integer division removes any decimal numbers when performing division, leaving just the integer (whole number).
In Python integer division is performed using //.
print(20 / 3)
print(20 // 3)
=
6.666666666666667
6
Integer Division Task 1 (Integer Division by 5)
Use an input line with int to ask the user to enter a number.
Use integer division (//) to divide the number by 5 without keeping any decimal values.
Challenge: Improve your solution by altering the print line to be more user friendly.
Example solutions:
Enter a number: 27
5
Enter a number: 27
5 goes into 27 5 times.
Integer Division Task 2 (Plane Rows)
A large plane has 6 seats in row.
Input the number of passengers on the plane and use integer division to work out how many full rows will be filled.
Example solution:
How many passengers are there in total? 174
There will be 29 full rows on the plane.
Exponent (Powers)
An exponent is the number of times a value is multiplied by itself, for example 2 = 2 x 2 x 2 = 8.
The symbol to represent an exponent in Python is **. For example: 4**2 represents 4 which is also 4 x 4.
3
2
print(4**4)
base = 5
exponent = 4
print(base**exponent)
=
256
625
=
Exponent Task 1 (Square Number)
Use an input line with int to ask the user to enter a number.
Output the square of this number.
Example solution:
Enter a number: 12
12 squared is 144
Exponent Task 2 (Custom Exponent)
Use an input line with int to ask the user to enter a number, this will be the base.
Make another input line with int to ask for the exponent.
Use ** between the base and the exponent and print it.
Challenge: Make your solution better by including the base and exponent in the print line.
Example solutions:
Enter the base: 7
Enter the exponent: 3
343
Enter the base: 7
Enter the exponent: 3
7 to the power of 3 is 343