Goal

You’ll be able to use Python’s core arithmetic and comparison operators correctly, and understand a specific operator distinction that trips up many beginners: the difference between / and //.

Learn

Python’s arithmetic operators handle basic math:

5 + 3   # 8  (addition)
5 - 3   # 2  (subtraction)
5 * 3   # 15 (multiplication)
5 / 3   # 1.6666... (division — always returns a float)
5 // 3  # 1  (floor division — discards the remainder, rounds down)
5 % 3   # 2  (modulo — returns just the remainder)
5 ** 2  # 25 (exponentiation)

The distinction between / and // is genuinely important and easy to get wrong: / always performs true division and returns a float, even when dividing two integers evenly (6 / 2 gives 3.0, not 3). // performs floor division, discarding any remainder and rounding down to the nearest whole number.

Comparison operators return a boolean result:

5 == 5   # True (equal to)
5 != 3   # True (not equal to)
5 > 3    # True
5 <= 5   # True

A genuinely common beginner mistake: confusing = (assignment — sets a variable’s value) with == (comparison — checks if two values are equal). These look similar but do completely different things; x = 5 assigns, x == 5 compares and returns True or False.

Decision Task

You calculate 10 // 3 and 10 % 3. Before reading on: what does each one return, and how do they relate to each other?

Show Answer

10 // 3 returns 3 (floor division — how many times 3 fits into 10, rounded down). 10 % 3 returns 1 (the remainder left over: 10 = 3×3 + 1). Together, floor division and modulo give you the whole quotient and the remainder separately — genuinely useful together, for example when converting a total number of seconds into minutes and remaining seconds.

Common Mistake

Using a single = when a comparison was intended, like writing if x = 5: instead of if x == 5:. In Python, this specific mistake actually raises a clear syntax error rather than silently doing the wrong thing (unlike some other languages), but it’s still a common typo worth knowing to watch for, especially coming from habits formed in other contexts.

Practice Questions

1. What does 7 / 2 return in Python, and what type is the result?

Show Answer

3.5, a float — the / operator always performs true division, returning a float even if the division happens to be uneven.

2. What does 7 // 2 return, and how is it different from the previous question?

Show Answer

3, an int — floor division discards the remainder and rounds down, unlike true division which keeps the decimal.

3. Write a comparison expression checking if a variable age is greater than or equal to 18.

Show Answer

age >= 18

4. True or False: = and == do the same thing in Python, just with different syntax preferences.

Show Answer

False — = assigns a value to a variable; == compares two values and returns a boolean. They are fundamentally different operations.

5. What does the modulo operator (%) return?

Show Answer

The remainder left over after division — for example, 10 % 3 returns 1.

Try It Yourself

Without looking back, write an expression using floor division and modulo together to convert 130 seconds into minutes and remaining seconds (you don’t need to run it, just write the two expressions).

Show Answer

minutes = 130 // 60 (gives 2) and remaining_seconds = 130 % 60 (gives 10) — together representing 2 minutes and 10 seconds, exactly the practical use case floor division and modulo are often combined for.

Quick Check

1. What does the / operator always return, regardless of whether the division is even?

Show Answer

A float.

2. What does the // operator do differently from /?

Show Answer

It discards the remainder and rounds down to a whole number (floor division), rather than returning a decimal.

3. What does the % operator return?

Show Answer

The remainder of a division.

4. What’s the key difference between = and ==?

Show Answer

= assigns a value; == compares two values and returns a boolean.

5. What does ** do in Python?

Show Answer

Exponentiation — raises a number to a power, e.g. 5 ** 2 equals 25.

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.