TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 7 of 22 · By Ethan Cole · Updated November 21, 2026

Python Booleans: Comparisons, and, or, not

Programs make decisions, and decisions need yes-or-no answers. Python's bool type has exactly two values, True and False, both capitalized. You rarely type them directly; they usually come from comparisons.

Comparison operators

== (equal), != (not equal), <, <=, > and >= compare two values and return a bool. Python lets you chain them the way you would write maths: 1 < age < 30 means 1 < age and age < 30. Text compares alphabetically by character code, so "apple" < "banana" is True but uppercase letters sort before lowercase ones.

Use is only to check identity, mainly x is None. For comparing values, always use ==.

Combining conditions

  • a and b is true only when both sides are true.
  • a or b is true when at least one side is true.
  • not a flips the value.

Both and and or short-circuit: Python stops evaluating as soon as the answer is known. In user and user.is_admin, the second part never runs if user is empty. not binds tighter than and, which binds tighter than or; add parentheses when mixing them.

Python 3 · editable

Expected output

True
False
True
True
True
False False True False
False

Truthy and falsy values

Any value can be used where a bool is expected. Empty or zero values are falsy: 0, 0.0, "", [], {}, set() and None. Nearly everything else is truthy, including the string "0" and the list [0], because they are not empty. That is why if items: is the idiomatic way to ask "does this list have anything in it?". The built-in bool() shows how a value would be judged.

The final line of the example repeats a lesson from the numbers chapter: floating-point results are approximations, so test them with math.isclose() instead of ==.

Common mistakes

  • Writing = instead of == in a condition, which is a syntax error inside if.
  • Writing true in lowercase; it raises NameError.
  • Testing if x == True: instead of the simpler if x:.
  • Writing if color == "red" or "blue":. The right side is the non-empty string "blue", which is always truthy. Write color in ("red", "blue").

Summary

  • True and False come from comparisons such as == and <.
  • and, or and not combine conditions and short-circuit.
  • Empty and zero values are falsy; most other values are truthy.

Quiz: Booleans and logic

1. What is bool("0")?

"0" is a non-empty string, and every non-empty string is truthy.

2. What does 3 < 5 < 4 evaluate to?

It means 3 < 5 and 5 < 4; the second comparison is false.

3. Why is if color == "red" or "blue": always true?

Python reads it as (color == "red") or "blue", and a non-empty string is truthy.

4. Which value is falsy?

An empty list is falsy; a list containing anything, even 0, is truthy.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.