Menu
Coddy logo textTech

Logical Operators Part 3

Part of the Fundamentals section of Coddy's Python journey. Lesson 18 of 77.

In Python, you can combine multiple conditions using logical operators (and, or, not) to create more complex expressions.

Let's create a condition that checks if a number is both positive and even:

number = 6

Now let's check if the number is positive:

is_positive = number > 0

Let's also check if the number is even:

is_even = number % 2 == 0

We can combine these conditions using the and operator:

result = is_positive and is_even

This evaluates to True because 6 is both positive and even.

For a more direct approach, you can combine conditions without intermediate variables:

result = number > 0 and number % 2 == 0

Similarly, you can use the or operator to check if at least one condition is true:

number = -4
is_negative_or_odd = number < 0 or number % 2 != 0

This evaluates to True because -4 is negative (even though it's not odd).

challenge icon

Challenge

Easy

Write code that checks if a person is eligible to drive. A person is eligible if ALL of the following are true:

  1. The person is at least 18 years old
  2. The person has a license
  3. The person has insurance

Use the variables age, has_license and has_insurance, and store the answer in result.

Try it yourself

age = int(input())
has_license = input() == "true"
has_insurance = input() == "true"

result = False   # <-- your condition goes here

print(result)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online Python compiler