Skip to main content

✍️ Arithmetic operators

NoteLearning Objectives Checklist

Note: if you forget what any of these operators do, review the lecture notes on Operators!

✍️ Practice

What is the output of the following programs?

1. Double Star Double Slash

x = 5
y = 2
print(x ** y)
print(x // y)

What does the ** operator do? What does the // operator do?

25
2

2. Division Divisions

print((-2 / 4), (2 / 4), (2 // 4), (-2 // 4))

The print function will insert a space between each comma-separated argument.

-0.5 0.5 0 -1

3. One divided by One

Wha’t the output and resulting data type of the following program?

print(1/1)

Does the / operator return an int or float?

1.0

float

4. No parenthesis

What is the output of the following program?

x = 1 / 2 + 3 // 3 + 4 ** 2
print(x)

Remember the PEDMAS (bracket/exponent/division/multiplication/addition/subtraction) rules!

17.5

5. Modulo

What is the output of the following program?

x = 7
y = 5
x = x % y
x = x % y
y = y % x
print(y)

Make sure you pay attention to the changes in x and y from line to line.

1

6. All operators

What is the output of the following program?

a = 9
b = 4
c = 5
x = a % b
y = a // c
print(x, y)
a = 5.5
b = 5

x = a % b
y = a // b
print(x, y)
a = -7
b = -4
x = a % b
y = (a + b) // b
print(x, y)
a = 9
b = 4
c = 5
x = a % b
y = a // c
print(x, y)
a = 5.5
b = 5

x = a % b
y = a // b
print(x, y)
a = -7
b = -4
x = a % b
y = (a + b) // b
print(x, y)
1 1
0.5 1.0
-3 2

🧩 Problem Solving

1. T-shirts

Write a short program which given a budget, calculates how many t-shirts they can purchase. Assume each t-shirt costs $15, including tax.

budget = 115.0
cost_one = 15.0

num_t_shirts = budget//cost_one

2. Egg Feast

Given a required number of eggs (num_eggs), where eggs can only be purchased in containers of 30, 12, 6, or as individual eggs, determine the number of containers of each size needed to purchase exactly the required number of eggs with no waste.

For example, if I need 24 eggs, I should purchase 2 containers of 12 rather than 1 container of 30.

Complete the following code:

Start by figuring out how many of the largest containers you can buy without exceeding the number of eggs you need.

Calculate how many eggs are still needed after buying the largest containers, then repeat the process with the next container size.

num_eggs = 100
large_size = 30
normal_size = 12
small_size = 6

# 1. Finding how many large can fit in num_eggs
large_count = num_eggs // large_size

# 2. How many eggs are left after buying the large containers
remaining = num_eggs % large_size


normal_count = remaining // normal_size
remaining_n = remaining % normal_size

small_count = remaining_n // small_size

# What's left are individual eggs to purchase 
individual_count = remaining_n % small_size

print("Here's what you need to purchase...")
print("Count large: ", large_count)
print("Count normal: ", normal_count)
print("Count small: ", small_count)
print("Individual eggs: ", individual_count)
Here's what you need to purchase...
Count large:  3
Count normal:  0
Count small:  1
Individual eggs:  4

🚀 Challenge

Write a short program to help the school administration calculate the number of sections per course given a certain number of registered students. Most groups should have an even number of students, with at least 18 students per classroom and a maximum of 32 students. The administration tries to minimize the number of groups and balances the number of students per classroom.

Note: There are many possible answers for this problem, but some answers are more optimal than others. You are allowed to use any arithmetic operators and simple if statements.

Find it here