0%

Making Decisions · practice

Combine Conditions

A can ask one question, such as 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 is True only when the condition on both sides is true:

Try it

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 conditionRight conditionResult with and
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

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:

Try it

The result is True because is_holiday is true. Both conditions would need to be false for this or expression to produce False.

Left conditionRight conditionResult with or
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Use or when one satisfied option is enough.

Repeat the variable in a range check

To ask whether a falls between two boundaries, write a complete comparison on each side of and:

Try it

Read the expression in two parts:

  • temperature >= 18

  • temperature <= 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 :

Try it

The first branch runs only when both requirements are true.

For longer logic, clear Boolean names can make the final decision easier to read:

Try it

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:

  1. comfortable_temperature is true when the temperature is at least the minimum and at most the maximum.

  2. day_off is true when it is a weekend or a holiday.

  3. outdoor_time is 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 placeholders with combined .

  • Set comfortable_temperature using both temperature boundaries and and.

  • Set day_off from is_weekend and is_holiday using or.

  • Set outdoor_time from comfortable_temperature and day_off using and.

Keep the fixed values and supplied output code unchanged.