Pylerno
English

Boolean logic

Combine conditions and distinguish missing data from false-valued data.

On this page

The concept

Comparison operators produce True or False. Use == for equality and != for inequality; = is assignment. A chained comparison such as 18 <= age < 65 checks a range clearly. and requires both operands to be truthy, or chooses an alternative, and not negates truthiness.

Empty strings, empty collections, zero, and None are false-valued. Nonempty strings such as "False" and "0" are truthy. Furthermore, and/or return one of their operand values, not necessarily a boolean. The expression name or "Guest" is useful only when every false-valued name should mean “use the default.”

Worked example

python
age = 22
has_ticket = True
print(age == 22, age != 18)
print(18 <= age < 65)
print(age >= 18 and has_ticket)
print(age < 18 or has_ticket)
print(not has_ticket)
print(bool(""), bool("False"), bool(0))
display_name = "" or "Guest"
print(display_name)

Trace the result

  1. Check each comparison against age = 22 before running the file.
  2. Combine the age condition and ticket status to see how and/or differ.
  3. The final expression chooses Guest because the left string is empty. It demonstrates a returned value, not just True or False.
text
True True
True
True
True
False
False True False
Guest
Line-by-line walkthrough

Line 1: age = 22

Evaluate 22, then bind or update age with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 2: has_ticket = True

Evaluate True, then bind or update has_ticket with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 3: print(age == 22, age != 18)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 4: print(18 <= age < 65)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 5: print(age >= 18 and has_ticket)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 6: print(age < 18 or has_ticket)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: print(not has_ticket)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print(bool(""), bool("False"), bool(0))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 9: display_name = "" or "Guest"

Evaluate '' or 'Guest', then bind or update display_name with the result. Assignment itself does not imply a deep copy.

Line 10: print(display_name)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

An index of 0 can be valid even though it is false-valued. Use is None when you mean “no result,” rather than a general truthiness check.

Check your understanding

What does this program print?

python
print(5 > 3 and 5 < 4)
Choose one answer

Search documentation

Share this page