Stop 02 · Variables, Data Types & OperatorsTopics · 4/8

Arithmetic Operators

Part of the Python Roadmap

In short

Splitting a bill, converting paise to rupees, checking if a number is even: it all comes down to seven operators. Two of them, // and %, are the secret weapons beginners underuse.

Python supports +, -, *, / (always returns a float), // (floor division), % (remainder) and ** (power), and follows normal operator precedence, which you can override with brackets.

The jaadu hands out round laddoos from a big steel box to five small plates, with two laddoos left over in the box.

Think of it like this

Sharing laddoos

Mummy hands you 17 laddoos to share among 5 cousins. Each cousin gets 3 (that's 17 // 5) and 2 are left in the box (that's 17 % 5). If you insisted on cutting them exactly, everyone would get 3.4 laddoos (that's 17 / 5). Three operators, three different questions.

Watch it happen

Evaluating 2 + 3 * 4 ** 2

( )
**
* / // %
+ -
Result

1/5Brackets first. There are none here, so Python moves on.

The big ideas

What you need to know

/ always gives a float

8 / 2 is 4.0, not 4. Use // when you want a whole-number answer.

// rounds down

Floor division drops toward minus infinity: 7 // 2 is 3, but -7 // 2 is -4, not -3.

% gives the remainder

n % 2 == 0 checks for even numbers, and total_paise % 100 gives the paise part of an amount.

Shortcuts like +=

stock -= 3 means stock = stock - 3. The same works for +=, *=, /=, //= and %=.

See it in code

Walk through the code

python
1total_paise = 123452rupees = total_paise // 100     # 1233paise = total_paise % 100       # 454print(rupees, paise)            # 123 455 6print(7 / 2, 8 / 2)             # 3.5 4.07print(7 // 2, -7 // 2)          # 3 -48print(2 ** 10)                  # 10249print(2 + 3 * 4, (2 + 3) * 4)   # 14 2010 11stock = 5012stock -= 3                      # same as stock = stock - 313print(stock)                    # 47

1/512345 paise: // 100 gives the 123 whole rupees and % 100 gives the 45 paise left over.

Comments show each output. // and % together split any amount into whole units and leftovers.

Save yourself the pain

Common mistakes

Oops

Using ^ for powers, like 2 ^ 3, and getting 1.

Do this instead

^ is bitwise XOR in Python. Use 2 ** 3 for 8.

Oops

Using / to count how many full boxes you can fill, then getting 3.5 boxes.

Do this instead

Use // for whole units and % for what's left over.

Oops

Writing x++ to add one.

Do this instead

Python has no ++ operator. Write x += 1.

Your mission

Everyday maths

~20 min

Quick check

What does 17 % 5 return?

Go deeper

  1. 1Compare 7 / 2, 7 // 2 and 7 % 2 in the REPL.
  2. 2Use % 2 to check whether a number is even or odd.
  3. 3Use augmented assignment like total += price to update a running total.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge