Python · Module 02: Core Data Types & Control FlowTutorial 4 of 30
All Tutorials·~1.5 hrs

2.1 Numbers, Booleans & Operators (/, //, %, is vs ==)

Master Python numbers, the 0.1 + 0.2 floating-point surprise, the difference between /, //, and %, logical short-circuiting, and why == and is are NOT the same.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. Numbers in Python: int vs. float

Python gives you two everyday number types:

  • int (Integers): Whole numbers like -42, 0, 1_000_000 (yes, you can use underscores _ as commas to make big numbers readable!). Unlike C++ or Java, Python integers have arbitrary precision — they never overflow, even for a 500-digit number!
  • float (Floating-Point): Decimal numbers like 3.14, -0.5, or 2.0.
PYTHON
population = 8_100_000_000   # Underscores are ignored by Python!
huge = 2 ** 100              # 1267650600228229401496703205376 (no overflow!)
price = 19.99                # float

The Famous 0.1 + 0.2 Floating-Point Trap

Try typing 0.1 + 0.2 == 0.3 in Python — you will get False!

PYTHON
print(0.1 + 0.2)         # 0.30000000000000004
print(0.1 + 0.2 == 0.3)  # False !!

Why? Computers store decimals in binary (0s and 1s), and 0.1 is a repeating binary fraction (just like 1/3 = 0.3333... in base 10).

  • For money / banking: Use Python's from decimal import Decimal (Decimal("0.1") + Decimal("0.2") == Decimal("0.3")).
  • For scientific comparisons: Use math.isclose(0.1 + 0.2, 0.3) (True).

2. Arithmetic Operators: The Pizza-Slice Rule (/ vs. // vs. %)

Freshers constantly mix up Python's three division operators. Imagine sharing 7 pizza slices between 2 friends:

Visual: Sharing 7 Pizza Slices Among 2 Friends (`/` vs `//` vs `%`)
True Division7 / 2 = 3.5floatExact decimal answer (always float!)Floor Division7 // 2 = 3intWhole slices each friend getsModulo (Remainder)7 % 2 = 1intLeftover slice remaining in box!
OperatorNameExampleResultEveryday Use
+, -, *Add, Sub, Mul5 * 315Basic math
/True Division8 / 24.0Always returns a float!
//Floor Division7 // 23Whole quotient (rounds down toward -∞)
%Modulo7 % 21Remainder — check even/odd (n % 2 == 0)
**Exponent (Power)2 ** 532Powers (9 ** 0.5 gives square root 3.0)

Watch Out with Negative Floor Division: -7 // 2 is -4 (NOT -3), because // always floors downward toward negative infinity (-3.5 floors down to -4).


3. Booleans & Logical Operators (and, or, not)

A bool has only two values: True or False (capital T and F!).

Python uses clean English words instead of &&, ||, !:

  • and — True only if both sides are true.
  • or — True if at least one side is true.
  • not — flips True ↔ False.

Chained Comparisons (Python Superpower!)

Instead of writing age >= 18 and age <= 65, Python lets you write math the way humans do:

PYTHON
age = 25
if 18 <= age <= 65:
    print("Eligible!")

Short-Circuit Evaluation

Python is smart and lazy:

  • In A and B, if A is False, Python never evaluates B.
  • In A or B, if A is True, Python never evaluates B.
PYTHON
count = 0
# Safe! Because count != 0 is False, Python never runs 100 / count (no ZeroDivisionError!)
if count != 0 and (100 / count) > 5:
    print("High average")

4. == vs. is — The Classic Interview & Fresher Trap

  • == asks: "Do these two objects have the same value?" (Two identical red Honda Civics parked side by side.)
  • is asks: "Are these two labels pointing to the exact same object in RAM?" (Two keys to the same car.)
Visual: `==` Checks Contents (Twin Cars) vs. `is` Checks Same Object in RAM
a = [1, 2] ; b = [1, 2]a[1, 2] · id: 0xA1b[1, 2] · id: 0xB9a == b → True | a is b → Falsea = [1, 2] ; c = aac[1, 2] · id: 0xA1a == c → True | a is c → True

Golden Rule:

  • Use == to compare numbers, strings, lists, and dicts.
  • Use is ONLY when checking for None (if user is None: / if user is not None:).

5. Interactive Trace: Operators, Shorthand Updates & Precedence

Remember PEMDAS (Parentheses → Exponents ** → * / // % → + - → Comparisons → not → and → or):

Tracing Arithmetic, Modulo & Boolean Logic
Interactive Line-by-Line Execution Trace · Step 1 of 5
1→total_mins = 135
2hours = total_mins // 60
3mins = total_mins % 60
4is_long = hours >= 2 and mins > 0
5print(f"{hours}h {mins}m | Long? {is_long}")
Variables in Memory
total_mins135int
Console Output
(nothing printed yet)
What just happened: We start with 135 total minutes (e.g., a movie duration).

Quick Quiz: Module 2.1 — Numbers, Booleans & Operators

Q1.What is the result and type of `10 / 2` in Python 3?
Q2.What does this snippet output?
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b, a is b)
Q3.How do you check if an integer `n` is EVEN?

Keep Exploring: Related Lessons & Tools

All Courses →