Exit slides Esc

Numbers & Arithmetic

Slide 1 of 61

  1. 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.

    • 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.

  2. 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.

  3. print(8 + 4)

    Output

    12
    

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

    One twist: saved numbers can take part in calculations.

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

    Output

    24
    

    tickets * price_each uses the values saved in both variables.

  5. One twist: multiplication happens before addition.

  6. print(2 + 3 * 4)

    Output

    14
    

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

  7. One twist: parentheses choose a different order.

  8. print((2 + 3) * 4)

    Output

    20
    

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

  9. Realistic: combine operations to find how many seats remain.

  10. 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
    

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

  11. Powers

    Minimal: ** raises a number to a power.

  12. print(2 ** 5)

    Output

    32
    

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

  13. 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.

  14. print(17 // 5)
    print(17 % 5)

    Output

    3
    2
    

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

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

  16. print(f"{3 + 4}")

    Output

    7
    

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

  17. One twist: label both quotient and remainder results.

  18. 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
    

    The same two given values answer two related questions.

    Realistic: return to the trip schedule.

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

    Output

    2 hours 15 minutes
    

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

  20. Repeated text

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

  21. print("=" * 12)

    Output

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

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

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

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

    Output

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

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

  24. 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.

  25. print(10 / 2)

    Output

    5.0
    

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

    One twist: a division can produce a half.

  26. print(7 / 2)

    Output

    3.5
    

    Python shows the computed float exactly as 3.5.

    Realistic: calculate the average of three given scores.

  27. 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
    

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

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

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

    Output

    3.50
    

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

  30. 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.

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

    Output

    13
    

    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.

  32. print("3" + "4")
    print(3 + 4)

    Output

    34
    7
    

    "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.

  33. Error demo: this code stops with an error
    print("Age: " + 12)
  34. 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.

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

    Output

    Age: 12
    Age: 12
    

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

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

  37. Error demo: this code stops with an error
    int("abc")

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

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

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

    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.

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

    Output

    Next year: 13
    

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

  41. 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.

  42. print(4 < 9)
    print(9 > 4)

    Output

    True
    True
    

    < 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.

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

    Output

    True
    True
    

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

  44. Minimal: == asks whether two values are equal.

  45. print(8 == 8)

    Output

    True
    

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

  46. One twist: != asks whether two values differ.

  47. print(8 != 9)

    Output

    True
    

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

  48. Realistic: test equality and inequality directly.

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

    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.

  50. Split digits

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

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

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

    Output

    4
    7
    

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

    One twist: rebuild the original value.

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

    Output

    63
    

    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.

  53. 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
    

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

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

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

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

  56. 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.

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

    Output

    5050
    

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

  58. 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.

  59. # 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
    

    One 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(), 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.