Skip to content
IT Vizag
← Decisions & Loops

True, False and comparisons

Lesson 1 of 9 · 1 min read

Booleans and the operators that compare values.

🎬 Video version coming soon — read it now

Before a program can make a decision, it needs a way to ask yes-or-no questions. In Python the answers are two special values: True and False. They're called booleans (type bool).

Comparison operators

print(5 > 3)     # True
print(5 < 3)     # False
print(5 >= 5)    # True
print(4 <= 2)    # False
print(7 == 7)    # True   equal to
print(7 != 7)    # False  not equal to

`=` vs `==`: one equals sign stores a value (age = 18). Two equals signs ask whether two values are the same (age == 18). Mixing them up is the most common beginner bug.

Comparing text

city = "Vizag"
print(city == "Vizag")   # True
print(city == "vizag")   # False, because capitals matter
print(city.lower() == "vizag")  # True

When you compare what a user typed, use .lower() or .strip() first so "Vizag ", "VIZAG" and "vizag" all match.

Storing a comparison

The result of a comparison is a value like any other, so you can store it:

marks = 72
passed = marks >= 35
print(passed)        # True
print(type(passed))  # <class 'bool'>

Checking membership with in

print("a" in "Araku")          # True
print("beach" in "RK Beach")   # False, because B is capital
Tip: Before writing an if statement, print the condition on its own. If print(marks >= 35) shows what you expect, your if will work too.

In one line

Comparisons like ==, !=, > and <= give True or False, and that is what every decision in Python is built on.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend