Skip to content

04 Arithmetic and Comparison

Python can calculate values and compare results. These skills help with small IT and everyday data tasks.

Run in Binder Open in Colab Download notebook

Read here. Practice in a notebook.

This page shows the original notebook. Open Binder or Colab to run and edit cells. Work one cell at a time: some examples are deliberately broken for debugging practice. Download your work before leaving Binder; its sessions are temporary.

View original source

Learning Objectives

By the end, you should be able to:

  • Use +, -, *, and / for simple calculations.
  • Use >, <, ==, and != to compare values.
  • Explain that comparisons return True or False.
  • Use calculations and comparisons in a small lab task.

Concept Explanation

Arithmetic means doing math with values. Comparison means checking how two values relate to each other.

First, we use arithmetic to calculate values. Then we use comparison operators to check those values and make decisions.

Arithmetic in Context

Think Before You Run: What total do you expect for the expenses below?

# Total expenses using +
books = 45.50
software = 24.99
printing = 6.50

total_expenses = books + software + printing
print("Total expenses:", total_expenses)

Think Before You Run: Which number will be larger: completed tasks or remaining tasks?

# Subtraction using -
total_tickets = 20
closed_tickets = 14
remaining_tickets = total_tickets - closed_tickets

print("Closed tickets:", closed_tickets)
print("Remaining tickets:", remaining_tickets)

Think Before You Run: What will this calculate for page count and average score?

# Multiplication using * and division using /
students = 18
pages_per_student = 3
total_pages = students * pages_per_student

score_1 = 88
score_2 = 92
score_3 = 85
average_score = (score_1 + score_2 + score_3) / 3

print("Total pages needed:", total_pages)
print("Average score:", average_score)

Order of Operations

Python follows the usual math order. Parentheses can make the order clear and change the result.

Think Before You Run: Will these two calculations give the same result?

# Without parentheses, multiplication happens before addition.
result_one = 10 + 5 * 2

# Parentheses tell Python what to do first.
result_two = (10 + 5) * 2

print("Without parentheses:", result_one)
print("With parentheses:", result_two)

Parentheses change the order of calculation. This short example shows the difference clearly.

# Multiplication happens before addition.
print(2 + 3 * 4)

# Parentheses happen first.
print((2 + 3) * 4)

Comparison Operators

After calculating a value, we can compare it to a target or threshold. Comparison expressions return either True or False.

True and False are Boolean values.

Think Before You Run: Will this result be True or False?

# A comparison gives a Boolean result: True or False.
score = 75
print(score > 50)

The output is True because 75 is greater than 50.

Think Before You Run: Which line will print True, and which line will print False?

# Side-by-side comparison
score = 60

print(score > 50)
print(score < 50)

score > 50 is True because 60 is greater than 50. score < 50 is False because 60 is not less than 50.

Think Before You Run: Which disk usage line will print True, and which line will print False?

# Comparison operators
disk_usage = 82
warning_level = 80

print("Greater than:", disk_usage > warning_level)
print("Less than:", disk_usage < warning_level)
print("Equal to:", disk_usage == warning_level)
print("Not equal to:", disk_usage != warning_level)

Important: = vs ==

= is used to assign a value. == is used to compare values.

# = means assignment: store 10 in the variable.
open_tickets = 10

# == means comparison: ask whether the value is 10.
print("Open tickets is 10:", open_tickets == 10)
# Short equality example
score = 80
print(score == 80)

Think Before You Run

Read the code first. Predict whether each comparison will print True or False.

# Predict each result before running.
available_storage = 45
needed_storage = 50
login_attempts = 3
max_attempts = 3

print("Enough storage:", available_storage > needed_storage)
print("Attempts reached max:", login_attempts == max_attempts)
print("Attempts are not over max:", login_attempts != 4)

Guided Practice

Change the values, run the code again, and notice how the output changes.

Think Before You Run: What do you expect the output to be after changing the values?

# TODO: Change these values and rerun the cell.
morning_logins = 24
afternoon_logins = 18

total_logins = morning_logins + afternoon_logins
difference = morning_logins - afternoon_logins

print("Total logins:", total_logins)
print("Difference:", difference)
print("Morning had more logins:", morning_logins > afternoon_logins)
# TODO: Try a different disk usage value.
disk_usage = 76
threshold = 80

print("Disk usage:", disk_usage)
print("Threshold:", threshold)
print("Needs attention:", disk_usage > threshold)

Common Mistakes

Small typing mistakes are common. Check symbols carefully, especially = and ==.

# Mistake to avoid: using = when you meant to compare.
# This line stores a value.
status_code = 200

# This line compares a value.
print("Status code is OK:", status_code == 200)
# Mistake to avoid: forgetting parentheses for an average.
score_1 = 80
score_2 = 90
score_3 = 100

wrong_average = score_1 + score_2 + score_3 / 3
correct_average = (score_1 + score_2 + score_3) / 3

print("Wrong average:", wrong_average)
print("Correct average:", correct_average)

Mini Lab

Store three numeric values, calculate a total or average, compare the result to a threshold, and print clear output.

Think Before You Run: Will the average response time be above the threshold?

# Mini Lab: average system response time
# TODO: Change the three values and rerun the cell.
# TODO: Complete the threshold value below.

response_time_1 = 1.2
response_time_2 = 1.6
response_time_3 = 1.4
threshold = 0

average_response_time = (response_time_1 + response_time_2 + response_time_3) / 3
over_threshold = average_response_time > threshold

print("Average response time:", average_response_time)
print("Threshold:", threshold)
print("Needs review:", over_threshold)

Wrap-Up

You practiced arithmetic, order of operations, and comparisons. Remember: comparisons return True or False, = stores a value, and == checks whether two values are equal.

These True/False results will be used in the next module to make decisions using if statements.