1.2.6Introduction to Programming (Python)

Arithmetic operators — +, −, - , - , - , %, - (floor div, modulo, power)

1,877 words9 min readdifficulty · medium

WHAT are they?

The key one to internalise: ==/ always gives a float==, even 4 / 2 gives 2.0, while ==// gives the largest integer not greater than the true quotient==.


WHY do // and % exist? (derive from long division)

When you compute a÷ba \div b by hand you produce two numbers: a whole-number quotient qq and a leftover remainder rr. Python keeps both for you:

a=bq+ra = b \cdot q + r

This is the division algorithm. Solve it:

  • q=a/ ⁣/bq = a \,/\!/\, b (the floor division — how many whole bb's fit in aa)
  • r=a%br = a \,\%\, b (modulo — what is left over)
Figure — Arithmetic operators — +, −,  - ,  - ,  - , %,  -  (floor div, modulo, power)

Operator precedence (HOW Python decides order)


Worked examples


Common mistakes (steel-manned)


Flashcards

What does / always return in Python 3, even for 4/2?
A float (2.0) — / is true division.
What is the difference between / and //?
/ true (float) division; // floor division → largest integer ≤ the true quotient.
State the division-algorithm identity linking // and %.
a == (a // b) * b + (a % b).
17 // 5 and 17 % 5?
3 and 2.
-17 // 5 =? Why?
-4, because floor division rounds toward −∞ (true quotient −3.4 → −4).
-17 % 5 =?
3 — result takes sign of divisor b; check (-4)*5+3=-17.
Which sign does Python's % follow?
The sign of the divisor b (unlike C/Java).
How do you get the last decimal digit of n?
n % 10.
How do you remove the last decimal digit of n?
n // 10.
Is ** left- or right-associative? 2 ** 3 ** 2?
Right-associative → 2 ** (3 ** 2) = 512.
Highest-precedence arithmetic operator?
** (exponentiation).
Test that n is even?
n % 2 == 0.
9 ** 0.5 =? Why?
3.0; fractional exponent 1/2 is the square root, returns a float.

Recall Feynman: explain it to a 12-year-old

Imagine you have 17 candies and 5 friends. Floor division 17 // 5 = 3 says "each friend gets 3 whole candies." Modulo 17 % 5 = 2 says "2 candies are left over in your hand." Put them back together: 3 candies × 5 friends + 2 leftover = 17. That's exactly the candies you started with! / is the fussy friend who says "actually each gets 3.4 candies" by chopping some up. And ** is just "multiply a number by itself again and again."


Connections

  • Variables and Assignment — operators combine the values you store.
  • Data Types — int float complex/ promotes to float; ** with fractions too.
  • Operator Precedence and Parentheses — the full PEMDAS-style rules.
  • Boolean and Comparison Operators% powers even/odd and divisibility tests.
  • Augmented Assignment — += -= //= %= **= — shorthand built from these.
  • Loops and Iteration% for "every Nth", // for index math.

Concept Map

produces quotient

produces remainder

combined via

combined via

defines

sign follows

always returns

rounds toward

highest

is

use to disambiguate

Long division of a by b

Floor div //

Modulo %

Identity a = a//b*b + a%b

a %25 b = a - a//b*b

Sign of divisor b

True div /

float result

Negative infinity floor

Operator precedence

Power **

Right-associative

Parentheses

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Python ke saat arithmetic operators hain: + - * / // % **. In mein se + - * aur ** (power) toh school wale hi hain. Asli kaam ke do naye dost hain — // (floor division) aur % (modulo). Yeh dono actually wahi purani long division hmain jo bachpan mein karte the: // batata hai kitne poore groups bante hain (quotient), aur % batata hai bacha kitna (remainder). Jaise 17 candies, 5 dost: har ek ko 3 milti hain (17 // 5 = 3) aur 2 bach jaati hain (17 % 5 = 2). Ek golden identity yaad rakho: a = (a // b)*b + (a % b) — yeh hamesha sach hoti hai.

Sabse bada confusion: Python 3 mein / hamesha float deta hai, matlab 4 / 2 bhi 2.0 dega, aur 5 / 2 2.5. Agar tumhe integer chahiye toh // use karo. C/Java se aaye ho toh dimaag mein yeh fix kar lo.

Negative numbers mein thoda dhyaan. // "floor" karta hai matlab -∞ ki taraf round, truncate nahi. Isliye -17 // 5 = -4 (na ki -3), aur -17 % 5 = 3 (positive!), kyunki Python ka % divisor ka sign leta hai. Confuse ho jao toh seedha formula laga do: r = a - (a // b) * b. Aur ** right-associative hai: 2 ** 3 ** 2 = 2 ** 9 = 512, not 64. Jab bhi doubt ho, bracket laga do — clean aur safe. Bas itna samajh lo toh yeh poora topic 20% effort mein 80% clear ho jaata hai.

Test yourself — Introduction to Programming (Python)

Connections