Making Decisions · practice
Combine Conditions
A age >= 18. Many real decisions depend on two or more questions.
Python provides the and and or for combining conditions.
and requires both conditions
An and True only when the condition on both sides is true:
Here, age >= 18 is True and has_ticket is also True, so the combined result is True.
Change has_ticket to False and run the example again. One false condition makes the complete and expression false.
| Left condition | Right condition | Result with and |
|---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Use and when every requirement must be satisfied.
or requires at least one condition
An or expression is True when either condition is true, including when both are true:
The result is True because is_holiday is true. Both conditions would need to be false for this or expression to produce False.
| Left condition | Right condition | Result with or |
|---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Use or when one satisfied option is enough.
Repeat the variable in a range check
To ask whether a and:
Read the expression in two parts:
temperature >= 18temperature <= 27
Both must be true. Writing temperature >= 18 and <= 27 is not valid because the second comparison has no value on its left side.
Python also supports a compact chained comparison, but the expanded form makes both Boolean questions visible and prepares you to combine different kinds of condition.
Use combined conditions in a decision
A combined Boolean expression can be used directly after
The first branch runs only when both requirements are true.
For longer logic, clear Boolean
Each line answers one understandable question. The final result combines answers that already have useful names.
Write the operators as words
Python uses the lowercase words and and or. It does not use && and || for these operations like some other programming languages do.
When a condition becomes hard to say aloud, split it into named Boolean values as in the previous example. Clear logic is more valuable than fitting every rule onto one line.
A program should allow entry when a visitor has a ticket and is at least 18. Which condition expresses that rule?
What does this code print?
is_weekend = False
is_holiday = False
print(is_weekend or is_holiday)Combine conditions for a day plan
The exercise gives you a temperature and two calendar flags. Create three Boolean values:
comfortable_temperatureis true when the temperature is at least the minimum and at most the maximum.day_offis true when it is a weekend or a holiday.outdoor_timeis true when the temperature is comfortable and it is a day off.
The supplied if/else statement turns the final Boolean result into a recommendation.
Task
Replace the three
Set
comfortable_temperatureusing both temperature boundaries andand.Set
day_offfromis_weekendandis_holidayusingor.Set
outdoor_timefromcomfortable_temperatureandday_offusingand.
Keep the fixed values and supplied output code unchanged.