top of page
top

## Python 4b - Mathematical Operators ## Modulo Division

The modulo operator - the percentage symbol % - will work out the remainder left over when one value is divided by another.

print(30 % 6)

=

0

30 ÷ 6 = 5, which is a whole number, so there is no remainder and 0 is output.

print(30 % 7)

=

2

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.

=

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 5Print the remainder.

Example solution:

Enter a number: 123
The remainder when divided by 5 is 3

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)

=

256

base = 5

exponent = 4

print(base**exponent)

625

=

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

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

bottom of page