Level 1
0 / 100 XP

True, False, and how Python compares things

Before Python can make a decision, it needs to evaluate whether something is true or false. That's where booleans come in.

The boolean type

A boolean is a value that is either True or False — nothing else:

Python
is_logged_in = True is_admin = False print(is_logged_in) # True print(is_admin) # False

Note the capital letters. True and False are keywords in Python — true and false (lowercase) will throw an error.

Comparison operators

Most of the time you won't write True or False directly — you'll get them as the result of a comparison:

Python
print(10 > 5) # True print(10 < 5) # False print(10 == 10) # True print(10 != 9) # True print(10 >= 10) # True print(10 <= 9) # False
OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

= vs ==

This trips up almost everyone at least once:

Python
age = 25 # = assigns a value print(age == 25) # == checks if two values are equal

= is assignment. == is a question: "are these the same?" They are not interchangeable.

Combining comparisons

Use and, or, and not to combine booleans:

Python
age = 25 has_ticket = True print(age >= 18 and has_ticket) # True — both must be true print(age < 18 or has_ticket) # True — at least one must be true print(not has_ticket) # False — flips True to False

Key takeaways

  • Booleans are either True or False — capital letters required
  • Comparison operators return booleans — use them to ask questions about your data
  • = assigns a value; == compares two values — don't mix them up
  • and, or, and not let you combine and invert boolean expressions