and, or, not

Making Decisions, lesson 4 of 5

🔀 Making Decisions, lesson 4 of 5

and, or, not

Combine conditions with logical operators.

9 min

1 exercise

2 quizzes

0/3 solved

Getting Python ready… examples can run in a moment.

Logical operators combine booleans:

  • a and b is True only if both are true
  • a or b is True if at least one is true
  • not a flips True to False and back

and is evaluated before or, so add parentheses when you mix them. Python also lets you chain comparisons: 13 <= age <= 19 means "age is between 13 and 19".

Example

Combining conditions

Set has_ticket = False and run it again.

Quiz

What does this print?

Example

Using them in if

Storing a condition in a well-named variable (weekend) makes the if read like English. Set raining = True and run it again.

Exercise

Theme park rules

A ride requires a height of at least 120 cm and an age of at least 8. Set can_ride to a single boolean expression using and.

Quiz

What does this print?