Skip to content
PythonControl Flowbeginner18 min read

Boolean Conditions

Understand True and False, comparison expressions, and how Python evaluates conditions using practical examples and exercises.

In this lesson, you will learn

  • ✓Explain the purpose of Boolean values and identify Python's Boolean literals.
  • ✓Use comparison operators to determine relationships between values.
  • ✓Trace Boolean expressions and predict their exact output.
  • ✓Explain how Python evaluates truthy and falsy values in conditions.
  • ✓Write and test simple Boolean conditions using realistic examples.

1. Understanding Boolean Values

A Boolean value represents one of two possibilities: True or False. Python uses the built-in bool type for these values. Boolean values are useful whenever a program needs to represent whether something is true, false, satisfied, or not satisfied.

For example, a program might check whether a student has passed an exam, whether a password is valid, or whether a temperature exceeds a limit. Each check can produce a Boolean result.

Python's Boolean literals are written as True and False, with capital first letters. The lowercase names true and false are not Python Boolean literals.

A comparison expression can produce a Boolean value. You can store that value in a variable, print it, or use it in a condition.

Store and display a Boolean value

Store and display a Boolean value
python
is_learning = True
has_finished = False
 
print(is_learning)
print(has_finished)
print(type(is_learning))
Output
True False <class 'bool'>

The variable is_learning refers to the Boolean value True. The variable has_finished refers to the Boolean value False. The type() function reports that is_learning contains a value of type bool. Each print() call displays its result on a separate line.

Note

True and False are Boolean values. The strings "True" and "False" are text values instead. Their printed appearance can be similar, but their types are different.

2. Comparison Operators

Comparison operators compare two values and produce a Boolean result. They let a program check equality, inequality, and ordering relationships.

The equality operator is ==, while the assignment operator is =. Use = to assign a value to a variable and == to test whether two values are equal.

When comparing numeric values, Python evaluates the relationship and returns True if it holds or False if it does not. The same comparison operators can also work with other compatible types, such as strings, although ordering depends on the type being compared.

Compare numeric values

Compare numeric values
python
temperature = 24
 
print(temperature > 20)
print(temperature == 24)
print(temperature < 10)
print(temperature != 24)
Output
True True False False

temperature > 20 is True because 24 is greater than 20. temperature == 24 is True because both values are equal. temperature < 10 is False because 24 is not less than 10. temperature != 24 is False because the two values are equal.

Note

The main comparison operators are ==, !=, <, >, <=, and >=. Remember that == compares values, whereas = assigns a value.

3. Reading and Tracing Conditions

To understand a Boolean expression, evaluate the values involved before deciding whether the expression is True or False. Reading a condition carefully is more reliable than guessing its result.

When a variable is involved, first identify its current value. Then apply the comparison operator and determine the resulting Boolean value.

A dry run records the important steps in execution order. It helps you connect each expression with the value Python evaluates.

Trace a comparison step by step

Trace a comparison step by step
python
score = 75
passed = score >= 60
 
print(score)
print(passed)
print(score == 75)
Output
75 True True

First, score is assigned the integer value 75. Next, score >= 60 compares 75 with 60 and evaluates to True. The resulting Boolean value is assigned to passed. The first print() displays 75, the second displays True, and the third confirms that score equals 75.

Caution

Do not confuse the expression being evaluated with its displayed result. For example, print(5 > 2) displays True; it does not display the expression's source code.

4. Combining Comparisons with and, or, and not

Python provides the logical operators and, or, and not to combine or reverse conditions. They are useful when a decision depends on more than one requirement.

The and operator requires both operands to be truthy for its result to be truthy. The or operator produces a truthy result when at least one operand is truthy. The not operator reverses the truth value of its operand.

When both operands are Boolean values, these operators make it easy to express conditions such as whether a score is within a range or whether a user has met at least one requirement.

Combine two conditions

Combine two conditions
python
age = 22
has_id = True
 
print(age >= 18 and has_id)
print(age < 18 or has_id)
print(not has_id)
Output
True True False

The first expression is True because age >= 18 and has_id are both True. The second expression is True because has_id is True, even though age < 18 is False. The third expression is False because not reverses the value True.

Note

Python evaluates and and or from left to right and uses short-circuit evaluation. With and, a falsy left operand can determine the result without evaluating the right operand. With or, a truthy left operand can determine the result. These operators can return operands rather than always returning a Boolean object; when you are beginning, focus first on how they evaluate conditions.

5. Truthy and Falsy Values

Python conditions do not require the expression to be exactly True or False. Python can evaluate other values in a Boolean context, such as an if condition.

A truthy value behaves like True in a Boolean context, while a falsy value behaves like False. Common falsy values include False, None, numeric zero, and empty collections or strings, such as 0, 0.0, '', [], (), and {}. Most other values are truthy.

The bool() function lets you explicitly check how Python interprets a value in a Boolean context. This is useful for learning and for checking values, but it does not mean that the original value changes into a Boolean.

Check truthiness with bool()

Check truthiness with bool()
python
print(bool(0))
print(bool(7))
print(bool(""))
print(bool("Python"))
print(bool([]))
print(bool([1, 2]))
Output
False True False True False True

The integer zero is falsy, while the nonzero integer 7 is truthy. An empty string is falsy, while a non-empty string is truthy. An empty list is falsy, while a list containing elements is truthy. bool() returns a Boolean value representing the truthiness of each supplied value.

Caution

Do not assume that a value must be a Boolean to be used as a condition. Also, do not confuse an empty string with the string "0": the empty string is falsy, but "0" is a non-empty string and is truthy.

6. Chained Comparisons and Real-World Conditions

Python supports chained comparisons, which let you compare one value against a lower and an upper bound in a readable expression. For example, 18 <= age <= 60 checks both age >= 18 and age <= 60.

A chained comparison is often clearer than repeating a variable in separate comparisons joined by and. Python evaluates the comparisons as a chain, and the overall result is Boolean.

Boolean conditions appear in everyday programs. They can determine whether a score meets a passing threshold, whether a number falls within a permitted range, or whether a value is present before it is processed.

Use a chained comparison

Use a chained comparison
python
age = 22
print(18 <= age <= 60)
 
age = 15
print(18 <= age <= 60)
Output
True False

When age is 22, it is at least 18 and at most 60, so the chained comparison is True. When age is 15, the lower-bound comparison fails, so the chained comparison is False.

Note

A Boolean condition can be evaluated and stored before you learn how to use if, elif, and else to control which statements run. In the next lesson, you will use conditions to make programs choose between different paths.

Common pitfalls to avoid

Real debugging lessons and exact syntax contrasts to avoid runtime bugs

#1 Using = when comparing two values.

Why it happens: A single = is assignment syntax; comparisons in conditions generally use == or another comparison operator.

Incorrect Approach
script.py
python
Using = when comparing two values.
Correct Solution
script.py
python
Use = for assignment and == for equality comparisons. For example, score == 60 checks whether score equals 60.

Key takeaway: Use = for assignment and == for equality comparisons. For example, score == 60 checks whether score equals 60.

#2 Writing true or false in lowercase.

Why it happens: Python Boolean literals are capitalized as True and False; lowercase true and false are treated as undefined names unless assigned.

Incorrect Approach
script.py
python
Writing true or false in lowercase.
Correct Solution
script.py
python
Write Python's Boolean literals as True and False. Lowercase true and false are not the built-in Boolean literals.

Key takeaway: Write Python's Boolean literals as True and False. Lowercase true and false are not the built-in Boolean literals.

#3 Assuming a comparison always produces True.

Why it happens: A comparison evaluates the actual values and can return False when the condition is not satisfied.

Incorrect Approach
script.py
python
Assuming a comparison always produces True.
Correct Solution
script.py
python
Evaluate the actual values. For example, 4 >= 8 is False because four is less than eight.

Key takeaway: Evaluate the actual values. For example, 4 >= 8 is False because four is less than eight.

#4 Using and when either condition should be sufficient.

Why it happens: and requires both conditions to be true; use or when either condition should independently satisfy the requirement.

Incorrect Approach
script.py
python
Using and when either condition should be sufficient.
Correct Solution
script.py
python
Use and when both conditions must hold and or when at least one condition must hold.

Key takeaway: Use and when both conditions must hold and or when at least one condition must hold.

#5 Assuming only Boolean values can be conditions.

Why it happens: Python conditions accept truthy and falsy values as well as explicit Boolean expressions.

Incorrect Approach
script.py
python
Assuming only Boolean values can be conditions.
Correct Solution
script.py
python
Python also evaluates truthiness. Values such as 0 and an empty string are falsy, while nonzero numbers and non-empty strings are truthy.

Key takeaway: Python also evaluates truthiness. Values such as 0 and an empty string are falsy, while nonzero numbers and non-empty strings are truthy.

#6 Assuming the string "0" behaves like the integer 0 in a Boolean context.

Why it happens: The string "0" is non-empty and therefore truthy, while the integer 0 is falsy.

Incorrect Approach
script.py
python
Assuming the string "0" behaves like the integer 0 in a Boolean context.
Correct Solution
script.py
python
The integer 0 is falsy, but the string "0" is truthy because it is not empty.

Key takeaway: The integer 0 is falsy, but the string "0" is truthy because it is not empty.

#7 Ignoring boundary values when checking a range.

Why it happens: Inclusive and exclusive boundaries differ; test values at the minimum and maximum, not only values inside the range.

Incorrect Approach
script.py
python
Ignoring boundary values when checking a range.
Correct Solution
script.py
python
Test values at the lower and upper limits, as well as values just outside them, to confirm that comparison operators match the requirement.

Key takeaway: Test values at the lower and upper limits, as well as values just outside them, to confirm that comparison operators match the requirement.

Ready for hands-on challenges?

Test your skills with shuffled coding challenges in the Practice Arena.

Practice Arena →

Technical and placement interview questions

Frequently tested interview questions with detailed, full answers (click to expand)

Q1:What is a Boolean value in Python?
Answer:
A Boolean value represents a truth value. Python's bool type has two Boolean values: True and False. Boolean results are commonly produced by comparisons and used to evaluate conditions.
Q2:What is the difference between = and ==?
Answer:
A single = assigns a value to a variable, while == compares two values for equality and produces a Boolean result. For example, score = 75 assigns 75 to score, whereas score == 75 checks whether score equals 75.
Q3:What do Python's comparison operators do?
Answer:
They compare values and produce Boolean results. The common operators are == for equality, != for inequality, < for less than, > for greater than, <= for less than or equal to, and >= for greater than or equal to.
Q4:What is the difference between and and or?
Answer:
For Boolean operands, and is True only when both operands are True. The or operator is True when at least one operand is True. Both operators use short-circuit evaluation where applicable and can return operands when used with non-Boolean values.
Q5:What does not do?
Answer:
The not operator reverses the truth value of an expression. For example, not True evaluates to False, and not False evaluates to True.
Q6:What are truthy and falsy values?
Answer:
Truthy values behave like True in a Boolean context, while falsy values behave like False. Examples of falsy values include False, None, numeric zero, and empty strings or collections. A non-empty string such as "0" is truthy.
Q7:What is a chained comparison?
Answer:
A chained comparison checks multiple relationships in one expression. For example, 10 <= score <= 100 checks that score is at least 10 and at most 100. It is often more readable than writing both comparisons separately.
Q8:How would you debug an incorrect Boolean condition?
Answer:
Check the values involved, confirm the intended comparison operators, evaluate each comparison separately, and inspect how logical operators combine the results. Test values at important boundaries and compare actual output with expected output.

Key takeaways

  • Python represents Boolean values using True and False, and their type is bool.
  • Comparison operators produce Boolean results; use == for equality and = for assignment.
  • Use and, or, and not to combine or reverse conditions, remembering their evaluation rules.
  • Python evaluates truthiness in conditions. Zero and empty collections are falsy, while non-empty strings such as "0" are truthy.
  • Chained comparisons provide a readable way to check whether a value falls within a range.
  • Trace expressions and test boundary cases instead of guessing their results.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: