A school trip lasts 135 minutes. The schedule needs that time written as hours and leftover minutes:
2 hours 15 minutesHow 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.
-
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
TrueorFalseresult.
-
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)Scroll the code for more ↓
Output
12Numbers need no quotation marks. Python calculates
8 + 4before printing12.One twist: saved numbers can take part in calculations.
tickets = 6 price_each = 4 print(tickets * price_each)Scroll the code for more ↓
Output
24tickets * price_eachuses the values saved in both variables.One twist: multiplication happens before addition.
print(2 + 3 * 4)Scroll the code for more ↓
Output
14Python multiplies
3 * 4first, then adds 2, giving 14.One twist: parentheses choose a different order.
print((2 + 3) * 4)Scroll the code for more ↓
Output
20Parentheses make
2 + 3happen 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}")Scroll the code for more ↓
Output
Open seats: 6Multiplication 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)Scroll the code for more ↓
Output
322 ** 5means 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)Scroll the code for more ↓
Output
3 217 makes 3 complete groups of 5 with 2 left over.
One twist: an f-string can calculate inside its braces.
print(f"{3 + 4}")Scroll the code for more ↓
Output
7Python calculates
3 + 4inside the braces and prints7.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}")Scroll the code for more ↓
Output
Each team gets: 4 Stickers left: 5The 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")Scroll the code for more ↓
Output
2 hours 15 minutes//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)Scroll the code for more ↓
Output
============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)Scroll the code for more ↓
Output
============ MATH CLUB ============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)Scroll the code for more ↓
Output
5.0/gives a float,//gives an int. The output above is exactly5.0.One twist: a division can produce a half.
print(7 / 2)Scroll the code for more ↓
Output
3.5Python 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}")Scroll the code for more ↓
Output
Average: 4.0The 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}")Scroll the code for more ↓
Output
3.50:.2fdisplays3.5as3.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)Scroll the code for more ↓
Output
13The sample value is
"12"with quotes, so it begins as text. Afterint(age_text), Python can calculate13.One twist: joining text and adding numbers use the same symbol but do different jobs.
print("3" + "4") print(3 + 4)Scroll the code for more ↓
Output
34 7"3" + "4"joins text to make34, while3 + 4calculates7.A mixed text-and-number join stops with a
TypeError. Read the final traceback line in this broken example.Error demo: this code stops with an error print("Age: " + 12)Scroll the code for more ↓
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}")Scroll the code for more ↓
Output
Age: 12 Age: 12Both 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.
Error demo: this code stops with an error int("abc")Scroll the code for more ↓
int("abc")raises aValueErrorbecause 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 age = int(input("Age: ")) print(f"Next year: {age + 1}")Scroll the code for more ↓
input()supplies text, andint()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}")Scroll the code for more ↓
Output
Next year: 13The 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:
TrueorFalse.Minimal: compare two numbers.
print(4 < 9) print(9 > 4)Scroll the code for more ↓
Output
True True<asks whether the left value is less, while>asks whether it is greater. Both comparisons print the booleanTrue.One twist: include the boundary with less-than-or-equal and greater-than-or-equal.
score = 8 print(score <= 8) print(score >= 8)Scroll the code for more ↓
Output
True True<=and>=include equality, so both comparisons printTruewhen the two values match.Minimal:
==asks whether two values are equal.print(8 == 8)Scroll the code for more ↓
Output
True8 == 8isTrue; 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)Scroll the code for more ↓
Output
True8 != 9isTruebecause the numbers are different.Realistic: test equality and inequality directly.
score = 8 print(score == 8) print(score != 10)Scroll the code for more ↓
Output
True True==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)Scroll the code for more ↓
Output
4 7Complete 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)Scroll the code for more ↓
Output
63Place 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}")Scroll the code for more ↓
Output
Number: 82 Tens: 8 Ones: 2The 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 number = int(input("Two-digit number: ")) tens = number // 10 ones = number % 10 print(f"Tens: {tens}") print(f"Ones: {ones}")Scroll the code for more ↓
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
nfrom 1 to 100, add it tototal.total = 0 for n in range(1, 101): total = total + n print(total)Scroll the code for more ↓
Output
5050The 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_minutesto 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")Scroll the code for more ↓
Output
2 hours 15 minutesOne given number now controls a useful hours-and-minutes report. Quotient and remainder create the two parts.
-
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(), andstr()convert a suitable value to the type you need. -
A comparison such as
minutes > 60producesTrueorFalse.
You can now turn a trip length into hours and leftover minutes.
-