← Intro to programming with Python

True, False and comparisons

Ask Python yes-or-no questions.

Start this lesson

Before a program can decide anything, it needs to ask a yes-or-no question. In Python the answer is a boolean: either True or False. You make one with a comparison: == (is equal to), != (is not equal to), <, >, <= and >=. Notice that "is equal to" uses TWO equals signs, because a single = already means "store".

age = 16
print(age >= 18)        # False
print(age == 16)        # True
print("cat" != "dog")   # True
print("Apple" == "apple")  # False

Comparisons give True or False. Text comparisons are exact: a capital letter makes it a different string.

Common mistake: writing if age = 18. A single = means store, so Python gives a SyntaxError. Use == to compare.

Comparing text with numbers never matches: "5" == 5 is False, because one is text and one is a number.

True and False start with capital letters, and have no quotes.