
So far, we have learned how to store data — integers (Part 9), strings (Part 10). We can hold a user's name, their age, a price, a password. We know how Python keeps them in memory, how to manipulate them.
But think about this — what have our programs actually done so far?
They store values. They calculate. They print. That is it. Right now, our code is like a warehouse full of raw materials — wood, bricks, steel, all neatly organized, labeled, stacked. But nothing is being built. The materials are just sitting there.
A warehouse does not become a building by itself. Someone has to look at the materials and start asking questions: "Is this beam strong enough?" "Do we have enough bricks?" "Is this the right size?" — and then decide what to do based on the answers.
Software works the exact same way.
Every app you use — banking, Instagram, Google — is not just storing data. It is constantly asking questions about that data:
Without these questions, your program is just a notebook — it writes things down but cannot act on them. It stores age = 16 but cannot do anything different for a 16-year-old vs a 25-year-old. It holds password = "abc123" but cannot check if what the user typed matches.
This is the moment your code goes from a calculator to actual software.
From this part onward, your programs will start thinking. And it all begins with the simplest question a program can ask — yes or no. That is what a Boolean is.
True and Falseprint(True)
print(False)
print(type(True)) # <class 'bool'>
True is an object stored in the heapx = True
print(type(x)) # <class 'bool'>
print(id(x)) # some memory address — this is the object's location in the heap
STACK HEAP
┌──────────┐ ┌──────────────────┐
│ x ──────│───────────▶│ type: bool │
└──────────┘ │ value: True │
│ id: 4345618736 │
└──────────────────┘
True object and ONE False object — everTrue points to the same object in the heapa = True
b = True
print(id(a)) # 4345618736
print(id(b)) # 4345618736 — same id!
print(a is b) # True — same object in heap
c = (10 > 5) # comparison produces True
print(id(c)) # 4345618736 — still the same object!
Why does bool even exist?
1 for true and 0 for false — just like C languagereturn 1 — is that "yes/true" or the actual number one? You cannot tell by reading the codebool with True and Falseis_active = True says what it means, is_active = 1 does notWhy subclass of int and not a separate type?
1 and 0 as booleansbool was a completely new type, all that existing code would breakbool inherit from int = backward compatibility — old code using 1/0 keeps working, new code can use True/FalseSo True IS 1 and False IS 0 — by design, not by accident:
print(True + True) # 2
print(False + 1) # 1
print(True * 10) # 10
Real-world use case 1 — Counting matches:
print(sum([True, True, False, True])) # 3
sum() adds up the list — True is 1, False is 0, total is 3True valuesReal-world use case 2 — Indexing with booleans:
names = ["Alice", "Bob"]
has_permission = True
print(names[True]) # "Bob" — True is 1, so index 1
print(names[False]) # "Alice" — False is 0, so index 0
Real-world use case 3 — Arithmetic with conditions:
price = 100
is_member = True
discount = price * 0.1 * is_member # 10.0 if member, 0.0 if not
final_price = price - discount
print(final_price) # 90.0
is_member is True (= 1), so 0.1 * 1 = 0.1 → 10% discount appliedis_member were False (= 0), 0.1 * 0 = 0 → no discountprint(bool(1)) # True
print(bool(0)) # False
print(bool("hello")) # True
print(bool("")) # False
print(bool()) # False — no argument defaults to False
False, everything else → TrueGotcha — bool("False") is True:
print(bool("False")) # True — surprise!
print(bool("0")) # True — also surprise!
"False" is a non-empty string → truthybool("") is False — any other string, even "False" or "0", is TrueTrue or False)a = 10
b = 20
print(a == b) # False (equal to)
print(a != b) # True (not equal to)
print(a > b) # False (greater than)
print(a < b) # True (less than)
print(a >= 10) # True (greater than or equal to)
print(a <= 5) # False (less than or equal to)
| Operator | What It Asks | Example | Result |
|---|---|---|---|
== | Are they equal? | 10 == 10 | True |
!= | Are they different? | 10 != 5 | True |
> | Is left bigger? | 10 > 5 | True |
< | Is left smaller? | 10 < 5 | False |
>= | Is left bigger or equal? | 10 >= 10 | True |
<= | Is left smaller or equal? | 5 <= 10 | True |
True/False are singletons → no new object is createdTrue or False in the heapresult = (10 > 5)
print(result) # True
print(id(result)) # same id as every other True
True/False objects'A' = 65, 'B' = 66, 'a' = 97, 'b' = 98print("apple" < "banana") # True — a(97) < b(98)
print("abc" == "abc") # True
print("A" < "a") # True — A(65) < a(97), uppercase is "smaller"
print("app" < "apple") # True — "app" is shorter
== checks value — do they hold the same data?is checks identity — are they the exact same object in the heap (same id())?a = 256
b = 256
print(a == b) # True — same value
print(a is b) # True — same object (256 is a cached singleton from Part 9)
a = 1000
b = 1000
print(a == b) # True — same value
print(a is b) # False — different objects in heap (1000 is outside cache range)
== and is always agree (because singletons)None (Part 12)== to check values. Use is only for None, True, False== works between any types — checks if values are equalprint(10 == 10.0) # True — int and float, same mathematical value
print(10 == "10") # False — int and str, different types = not equal
print(True == 1) # True — bool is subclass of int
print(False == 0) # True
print(True == 1.0) # True — 1.0 equals 1 equals True
<, >, <=, >= raise TypeError when types are incompatibleprint(10 > 5) # True — both int, works
print(10 > 5.0) # True — int vs float, Python can compare these
print("10" > 5) # TypeError: '>' not supported between 'str' and 'int'
"10" is a string, 5 is an integerx = 10 # Assignment — puts value 10 into x
x == 10 # Comparison — asks "is x equal to 10?", returns True
= puts a value into a variable== asks a question and returns True/False= when you mean == is one of the most common beginner bugs= inside a condition → catches it for youentered_password == stored_password — comparison is the foundation of every loginprice > budget — every filter, sort, and search uses comparisonsstatus_code == 200 — checking if a request succeededage >= 18 — every rule and filter starts with a comparisonsum() on boolean results counts how many items match — accuracy, precision, recall all use thisNext: Part 12 — Logical Operators and Truthiness. You can now ask one question at a time. But what if you need to check two things at once? And what if every value in Python is secretly True or False?
Why Python Rejected switch for 30 Years: match-case Explained | Python in Kannada | Part-14
Part 14
Why Python Rejected switch for 30 Years: match-case Explained | Python in Kannada | Part-14
Part 14