Lesson 8/48 ยท ๐Ÿ Your First Python
๐Ÿ Your First PythonLesson 8/48
Phase 1 ยท Your First Python18 min

Numbers & Math

Every program uses numbers, learn how Python thinks about arithmetic

Why does math matter in code?
Before we talk about operators, let's answer: *why does a beginner programmer need math?*
Almost everything programs do involves numbers: calculating totals, counting items, checking limits, generating positions, measuring time. Even a simple "how many times have you logged in?" feature needs addition.
Python is great at math, and the syntax is almost identical to what you learned in school.
The basic operators:
  • + addition: 3 + 4 โ†’ 7
  • - subtraction: 10 - 3 โ†’ 7
  • * multiplication: 6 * 7 โ†’ 42
  • / division: 10 / 4 โ†’ 2.5 (always gives a decimal)
  • // integer division: 10 // 4 โ†’ 2 (drops the decimal, "floor division")
  • % remainder (modulo): 10 % 4 โ†’ 2 (the leftover after dividing)
  • ** power: 2 ** 8 โ†’ 256
  • Basic arithmetic in Pythonpython
    print(10 + 3)   # 13
    print(10 - 3)   # 7
    print(10 * 3)   # 30
    print(10 / 3)   # 3.3333... (always a float)
    print(10 // 3)  # 3 (integer division)
    print(10 % 3)   # 1 (remainder: 10 = 3*3 + 1)
    print(2 ** 10)  # 1024
    Common confusion: / vs //
    10 / 3 gives 3.3333..., always a decimal (float), even if it divides evenly: 6 / 3 gives 2.0 not 2.
    10 // 3 gives 3, always a whole number, no remainder.
    Use // when you need "how many whole times does X fit into Y?", like figuring out how many full weeks are in 100 days: 100 // 7 = 14.
    The % (modulo) operator, the most useful one nobody teaches
    % gives you the remainder after division. It looks strange but it's incredibly useful:
  • Is a number even? n % 2 == 0 โ†’ yes if no remainder when dividing by 2
  • What minute of the hour? seconds % 60
  • Wrap around a list? index % len(list)

  • Example: A movie is 142 minutes. How many full hours? How many remaining minutes?142 // 60 = 2 full hours. 142 % 60 = 22 remaining minutes.
    Order of operations works exactly like in math:** โ†’ * / // % โ†’ + -
    Use parentheses to control order: (2 + 3) * 4 = 20, but 2 + 3 * 4 = 14.
    ๐Ÿค”Quick Check

    What does print(17 % 5) output?

    ๐Ÿค”Quick Check

    What does print(7 / 2) output?

    Practice Exercises

    0/3 solved
    Exercise 1 of 3easyGuided
    โฑ 00:00

    Rectangle Area

    Calculate and print the area of a rectangle.The length is 12 and the width is 8.Area = length * width
    solution.py
    1 / 3
    Exercise 2 of 3easy
    โฑ 00:00

    Movie Time Converter

    A movie is 155 minutes long.Print how many full hours it is, then print the remaining minutes.
    Expected output:235
    solution.py
    2 / 3
    Exercise 3 of 3medium
    โฑ 00:00

    Compound Interest

    You invest $1000 at 10% annual interest for 3 years.Using the formula: total = principal * (1.1 ** years)Print the total rounded to 2 decimal places.
    Hint: use round(value, 2) to round.Expected output: 1331.0
    solution.py
    3 / 3
    Solve all 3 exercises to unlock completion