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 to drive if ALL of the following conditions are met:

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

The following starter code is already provided for you — it reads the inputs and converts them to the correct types:

  • age = int(input()) — reads the age as an integer
  • has_license = input() == "true" — reads the license string and converts it to a boolean
  • has_insurance = input() == "true" — reads the insurance string and converts it to a boolean

Your task is to:

  1. Check all three conditions using the variables age, has_license, and has_insurance, and store the result in a variable named result

Try it yourself

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

result = # Complete this line to check if all conditions are met

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