Python 4b – Mathematical Operators

Modulo (%), integer division (//), and exponentiation (**) — with practical examples and bite-size tasks.

Modulo (%)

The modulo operator gives the remainder after division:

print(30 % 6)  # 0
print(30 % 7)  # 2

Using variables is just as simple:

num1 = 33
num2 = 4
print("The remainder is", num1 % num2)  # The remainder is 1

A classic use is checking even/odd (remainder when divided by 2):

num = int(input("Enter a number: "))
if num % 2 == 0:
    print(num, "is even.")
else:
    print(num, "is odd.")

Modulo Task 1 – Remainder /5

Ask for a whole number and print the remainder when divided by 5:

number = int(input("Enter a number: "))
print("The remainder when divided by 5 is", number % 5)

Modulo Task 2 – Rollercoaster

A rollercoaster only allows groups of 4. Check if a group size divides evenly into 4:

group = int(input("How many in your group? "))
if group % 4 == 0:
    print("Perfect groups of four!")
else:
    print("You will be split up!")

Integer Division (//)

Integer division discards any fractional part:

print(20 / 3)   # Regular division: 6.666666666666667
print(20 // 3)  # Integer division: 6

Integer Division Task 1 – Divide by 5

Read a number and show how many whole times 5 fits in it (with clear wording):

num = int(input("Enter a number: "))
result = num // 5
print("5 goes into", num, "exactly", result, "whole times.")

Integer Division Task 2 – Plane Rows

A plane has 6 seats per row. How many full rows are filled?

passengers = int(input("How many passengers are there in total? "))
rows = passengers // 6
print("There will be", rows, "full rows on the plane.")

Exponentiation (**)

Exponentiation raises a number to a power:

print(4 ** 4)  # 256

With variables:

base = 5
exponent = 4
print(base ** exponent)  # 625

Exponent Task 1 – Square a Number

Ask for a number and print its square:

number = int(input("Enter a number: "))
print(number, "squared is", number ** 2)

Exponent Task 2 – Custom Exponent

Ask for a base and exponent, then show the result nicely formatted:

base = int(input("Enter the base: "))
exp = int(input("Enter the exponent: "))
result = base ** exp
print(base, "to the power of", exp, "is", result)