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.
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 like3.14,-0.5, or2.0.
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!
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:
| Operator | Name | Example | Result | Everyday Use |
|---|---|---|---|---|
+, -, * | Add, Sub, Mul | 5 * 3 | 15 | Basic math |
/ | True Division | 8 / 2 | 4.0 | Always returns a float! |
// | Floor Division | 7 // 2 | 3 | Whole quotient (rounds down toward -∞) |
% | Modulo | 7 % 2 | 1 | Remainder — check even/odd (n % 2 == 0) |
** | Exponent (Power) | 2 ** 5 | 32 | Powers (9 ** 0.5 gives square root 3.0) |
Watch Out with Negative Floor Division:
-7 // 2is-4(NOT-3), because//always floors downward toward negative infinity (-3.5floors 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—Trueonly if both sides are true.or—Trueif at least one side is true.not— flipsTrue↔False.
Chained Comparisons (Python Superpower!)
Instead of writing age >= 18 and age <= 65, Python lets you write math the way humans do:
age = 25
if 18 <= age <= 65:
print("Eligible!")
Short-Circuit Evaluation
Python is smart and lazy:
- In
A and B, ifAisFalse, Python never evaluatesB. - In
A or B, ifAisTrue, Python never evaluatesB.
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.)isasks: "Are these two labels pointing to the exact same object in RAM?" (Two keys to the same car.)
Golden Rule:
- Use
==to compare numbers, strings, lists, and dicts. - Use
isONLY when checking forNone(if user is None:/if user is not None:).
5. Interactive Trace: Operators, Shorthand Updates & Precedence
Remember PEMDAS (Parentheses → Exponents ** → * / // % → + - → Comparisons → not → and → or):
Quick Quiz: Module 2.1 — Numbers, Booleans & Operators
a = [1, 2, 3] b = [1, 2, 3] print(a == b, a is b)
Keep Exploring: Related Lessons & Tools
All Courses →Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.
Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.