Lesson 7 of 60
โฎPrev Nextโฏ

PY-101 ยท Operators

Arithmetic operators

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

7of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    Arithmetic operators do maths on numbers: + - * and / work as you expect, // divides and drops the remainder, % gives the remainder, and ** raises to a power. / always returns a float. Python follows the usual order: brackets, powers, then * / // %, then + and -.

    • Use the seven arithmetic operators
    • Know the difference between / and //
    • Use % to find remainders
    • Control order with brackets

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Arithmetic operators, running
    1. a = 17
    2. b = 5
    3. print(a + b)
    4. print(a - b)
    5. print(a * b)
    6. print(a / b)
    7. print(a // b)
    8. print(a % b)
    9. print(2 ** 10)
    10. print(2 + 3 * 4)
    11. print((2 + 3) * 4)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA film is 135 minutes long and the cinema app shows 2 h 15 min. How does it split 135 into 2 and 15? With two operators most beginners have never met: // and %.
    2. The Python ideaa + b a - b a * b a / bArithmetic operators do maths on numbers: + - * and / work as you expect, // divides and drops the remainder, % gives the remainder, and ** raises to a power.
    3. In real lifePaging through search results: page_count = (total + per_page - 1) // per_page. With 101 results and 10 per page you get 11 pages.

    Start with a question

    A film is 135 minutes long and the cinema app shows 2 h 15 min. How does it split 135 into 2 and 15? With two operators most beginners have never met: // and %.

    Prices, timers, pagination and game scores are all arithmetic. // and % in particular show up everywhere: splitting minutes into hours, checking even numbers, wrapping around a grid.

    Explain it like I'm 10

    Python is a calculator that never slips. // asks "how many whole times does it fit?" and % asks "what is left over?"

    The technical explanation

    17 / 5 is 3.4. / always gives a float, even 10 / 2 gives 5.0.

    17 // 5 is 3: how many whole times 5 fits in 17. 17 % 5 is 2: what is left over.

    n % 2 == 0 is the standard test for an even number.

    Python works out 2 + 3 * 4 as 14 because * comes before +. Brackets make the order clear: (2 + 3) * 4 is 20.

    OperatorNameExampleResult
    +Add17 + 522
    -Subtract17 - 512
    *Multiply17 * 585
    /Divide17 / 53.4
    //Floor divide17 // 53
    %Modulus17 % 52
    **Power2 ** 101024

    Syntax

    a + b   a - b   a * b   a / b
    a // b  a % b   a ** b

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Paging through search results: page_count = (total + per_page - 1) // per_page. With 101 results and 10 per page you get 11 pages.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What is 20 // 6?
    2. 2. What is 20 % 6?
    3. 3. What is the type of 10 / 2?
    4. 4. What is 2 ** 3?
    5. 5. What is 10 - 2 * 3?

    Questions people ask

    What is the difference between / and // in Python?

    / is true division and always returns a float (7 / 2 is 3.5). // is floor division and returns the whole-number part, rounded down (7 // 2 is 3).

    What does % mean in Python?

    For numbers, % is the modulus operator: it returns the remainder after division. 10 % 3 is 1. It is often used to test for even numbers with n % 2 == 0.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between / and // in Python?

    / is true division and always returns a float (7 / 2 is 3.5). // is floor division and returns the whole-number part, rounded down (7 // 2 is 3).

    beginner What does % mean in Python?

    For numbers, % is the modulus operator: it returns the remainder after division. 10 % 3 is 1. It is often used to test for even numbers with n % 2 == 0.

    intermediate What is 20 // 6?

    3. 6 fits into 20 three whole times, so 20 // 6 is 3.

    intermediate What is 20 % 6?

    2. 6 * 3 = 18, and 20 - 18 leaves a remainder of 2.

    Quick revision

    Remember

    • Use the seven arithmetic operators
    • Know the difference between / and //
    • Use % to find remainders
    • Control order with brackets
    • / always gives a float, even 10 / 2 is 5.0. Use // when you need a whole number, like a page count.

    Syntax

    a + b   a - b   a * b   a / b
    a // b  a % b   a ** b

    Official documentation