Python, Concept by Concept

unit

Numbers & Arithmetic

A school trip lasts 135 minutes. The schedule needs that time written as hours and leftover minutes:

2 hours 15 minutes

How can Python split one number into those two useful parts?

We will build that payoff in three lessons. Each idea climbs the same ladder: minimal, one twist, then realistic. Read every Notice line before moving up.

You will learn

  • Calculate with whole numbers and decimals using Python's arithmetic symbols.
  • Use parentheses, division, quotients, and remainders to control a calculation.
  • Convert between text and numbers when you read input or build output.
  • Compare values and read the True or False result.

Lesson 1: Calculate with Whole Numbers

Arithmetic operators

Python can calculate with whole numbers called integers. The symbols +, -, and * mean add, subtract, and multiply.

Minimal: add two integers.

print(8 + 4)

Output

12

Notice

Numbers need no quotation marks. Python calculates 8 + 4 before printing 12.

One twist: saved numbers can take part in calculations.

tickets = 6
price_each = 4
print(tickets * price_each)

Output

24

Notice

tickets * price_each uses the values saved in both variables.

One twist: multiplication happens before addition.

print(2 + 3 * 4)

Output

14

Notice

Python multiplies 3 * 4 first, then adds 2, giving 14.

One twist: parentheses choose a different order.

print((2 + 3) * 4)

Output

20

Notice

Parentheses make 2 + 3 happen first, giving 20 after multiplication.

Realistic: combine operations to find how many seats remain.

rows = 5
seats_per_row = 8
students = 34
open_seats = rows * seats_per_row - students
print(f"Open seats: {open_seats}")

Output

Open seats: 6

Notice

Multiplication happens before subtraction, so 40 seats are counted before 34 students are removed. Parentheses can make a chosen order clear.

Powers

Minimal: ** raises a number to a power.

print(2 ** 5)

Output

32

Notice

2 ** 5 means 2 multiplied by itself five times, giving 32.

Quotients and remainders

The operator // gives the whole-number quotient. The operator % gives the remainder. We will use both only with non-negative numbers.

Minimal: divide 17 items into groups of 5.

print(17 // 5)
print(17 % 5)

Output

3
2

Notice

17 makes 3 complete groups of 5 with 2 left over.

One twist: an f-string can calculate inside its braces.

print(f"{3 + 4}")

Output

7

Notice

Python calculates 3 + 4 inside the braces and prints 7.

One twist: label both quotient and remainder results.

stickers = 29
team_size = 6
print(f"Each team gets: {stickers // team_size}")
print(f"Stickers left: {stickers % team_size}")

Output

Each team gets: 4
Stickers left: 5

Notice

The same two given values answer two related questions.

Realistic: return to the trip schedule.

total_minutes = 135
hours = total_minutes // 60
minutes_left = total_minutes % 60
print(f"{hours} hours {minutes_left} minutes")

Output

2 hours 15 minutes

Notice

// finds the 2 complete hours, and % finds the 15 minutes left over.

Repeated text

Minimal: multiply a string by an integer to repeat it.

print("=" * 12)

Output

============

Notice

Multiplying "=" by 12 prints twelve equal signs in one line.

One twist: use the repeated line above and below a title.

divider = "=" * 12
print(divider)
print("MATH CLUB")
print(divider)

Output

============
MATH CLUB
============

Notice

Saving the divider lets the same twelve-character line frame both sides of the title.

Lesson 2: Work with Number Types

Division and floats

A number with a decimal point is a float. The / operator performs regular division.

Minimal: divide 10 by 2.

print(10 / 2)

Output

5.0

Notice

/ gives a float, // gives an int. The output above is exactly 5.0.

One twist: a division can produce a half.

print(7 / 2)

Output

3.5

Notice

Python shows the computed float exactly as 3.5.

Realistic: calculate the average of three given scores.

score_one = 3
score_two = 4
score_three = 5
average = (score_one + score_two + score_three) / 3
print(f"Average: {average}")

Output

Average: 4.0

Notice

The parentheses add the three scores first. The exact printed average is 4.0.

One twist: show a price with two digits after the decimal point.

price = 3.5
print(f"{price:.2f}")

Output

3.50

Notice

:.2f displays 3.5 as 3.50; it changes the printed format, not the saved number.

Convert between text and numbers

int() turns suitable text into an integer. float() turns suitable text into a float. str() turns a value into text.

Minimal: convert quoted digits before calculating.

age_text = "12"
age = int(age_text)
print(age + 1)

Output

13

Notice

The sample value is "12" with quotes, so it begins as text. After int(age_text), Python can calculate 13.

One twist: joining text and adding numbers use the same symbol but do different jobs.

print("3" + "4")
print(3 + 4)

Output

34
7

Notice

"3" + "4" joins text to make 34, while 3 + 4 calculates 7.

A mixed text-and-number join stops with a TypeError. Read the final traceback line in this broken example.

Read the error

print("Age: " + 12)

The final line says TypeError: can only concatenate str (not "int") to str. Convert the number to text, or let an f-string place it in the sentence.

age = 12
print("Age: " + str(age))
print(f"Age: {age}")

Output

Age: 12
Age: 12

Notice

Both repaired lines print Age: 12; each line keeps text and number types compatible.

Try a broken conversion: read the final traceback line to find out why these letters cannot become an integer.

Read the error

int("abc")

Notice

int("abc") raises a ValueError because the text does not contain digits for an integer.

Try real input: run the next cell yourself. It waits for an age, then converts it to an integer.

Try it yourself

age = int(input("Age: "))
print(f"Next year: {age + 1}")

Notice

input() supplies text, and int() converts it before the addition.

Realistic: use a fixed value to make the same calculation repeatable in the next cell.

age_text = "12"
age = int(age_text)
print(f"Next year: {age + 1}")

Output

Next year: 13

Notice

The fixed value keeps the lesson repeatable; the real-input cell above lets you try your own age.

Lesson 3: Ask Number Questions

Comparisons and booleans

A comparison asks a yes-or-no question. Its result is a boolean value: True or False.

Minimal: compare two numbers.

print(4 < 9)
print(9 > 4)

Output

True
True

Notice

< asks whether the left value is less, while > asks whether it is greater. Both comparisons print the boolean True.

One twist: include the boundary with less-than-or-equal and greater-than-or-equal.

score = 8
print(score <= 8)
print(score >= 8)

Output

True
True

Notice

<= and >= include equality, so both comparisons print True when the two values match.

Minimal: == asks whether two values are equal.

print(8 == 8)

Output

True

Notice

8 == 8 is True; use two equal signs to compare, rather than one equal sign to save a value.

One twist: != asks whether two values differ.

print(8 != 9)

Output

True

Notice

8 != 9 is True because the numbers are different.

Realistic: test equality and inequality directly.

score = 8
print(score == 8)
print(score != 10)

Output

True
True

Notice

== asks whether two values are equal, while != asks whether they are different. Each comparison prints a boolean. A single = saves a value instead.

Split digits

Quotient and remainder can separate the digits of a non-negative integer.

Minimal: split a two-digit number from 10 through 99.

number = 47
tens = number // 10
ones = number % 10
print(tens)
print(ones)

Output

4
7

Notice

Complete tens come from // 10; the leftover ones come from % 10.

One twist: rebuild the original value.

number = 63
tens = number // 10
ones = number % 10
rebuilt = tens * 10 + ones
print(rebuilt)

Output

63

Notice

Place value reverses the split: 6 tens plus 3 ones makes 63.

Realistic: print a labeled digit report for a two-digit number from 10 through 99.

number = 82
tens = number // 10
ones = number % 10
print(f"Number: {number}")
print(f"Tens: {tens}")
print(f"Ones: {ones}")

Output

Number: 82
Tens: 8
Ones: 2

Notice

The stated 10-through-99 range guarantees exactly one tens digit and one ones digit.

Try real input: type a two-digit number from 10 through 99 to see its place values.

Try it yourself

number = int(input("Two-digit number: "))
tens = number // 10
ones = number % 10
print(f"Tens: {tens}")
print(f"Ones: {ones}")

Notice

int() turns the typed text into a number before // and % split its digits.

Notice — peek ahead (not needed for the exercises):

Adding every integer from 1 through 100 by hand would be tedious. Read this example; you do not need to write or change it.

For each number n from 1 to 100, add it to total.

total = 0
for n in range(1, 101):
    total = total + n
print(total)

Output

5050

Notice

The displayed result is 5050. The repeated pattern is a preview only; the exercises use the number tools taught above.

Final build

Use the given trip length to print the schedule line from the opening. Then change total_minutes to another non-negative value and observe how both parts update.

# Change this value to test another trip length.
total_minutes = 135

hours = total_minutes // 60
minutes_left = total_minutes % 60
print(f"{hours} hours {minutes_left} minutes")

Output

2 hours 15 minutes

Notice

One given number now controls a useful hours-and-minutes report. Quotient and remainder create the two parts.

Recap

  • Integers are whole numbers; floats can have a decimal part.
  • +, -, *, /, and ** calculate; parentheses change the order.
  • // gives a whole-number quotient and % gives a remainder.
  • int(), float(), and str() convert a suitable value to the type you need.
  • A comparison such as minutes > 60 produces True or False.

You can now turn a trip length into hours and leftover minutes.

Go to the exercises (23)

For parents and teachers: report a problem