Menu
Coddy logo textTech

Logical Operators Part 1

Part of the Fundamentals section of Coddy's Python journey — lesson 15 of 77.

Logical operators are used to combine conditional statements.

Python has three logical operators:

  • and
  • or
  • not

Let's see how the and operator works:

The and operator returns True if both statements are true.

# Create two boolean variables
x = True
y = True

# Check if both x and y are True
result = x and y

After executing the above code, result contains:

True

If one of the values is False, the result will be False:

# Create two boolean variables
x = True
y = False

# Check if both x and y are True
result = x and y

This gives us:

False
challenge icon

Challenge

Easy

Complete the code to determine if a person is eligible to drive based on their age and license status.

A person is eligible to drive when:

  • They are at least 18 years old AND
  • They have a valid driving license

Fill in the blanks with the correct values:

  1. Fill in the age variable with 20
  2. Fill in the has_license variable with True
  3. Fill in the minimum age requirement in the comparison

Cheat sheet

Logical operators in Python:

OperatorMeaningExample
andTrue if all operands are Truea and b
orTrue if any operand is Truea or b
notTrue if the operand is Falsenot a

Examples:

b1 = (5 > 3) and (1 == 1)  # True
b2 = not 5 == 4 or 5 == 2  # True
b3 = not 1 == 1 or False   # False
b4 = not (3 > 4)           # True
b5 = not (5 > 10 or 5 > 1) # False

Try it yourself

age = ?
has_license = ?

result = age >= ? and has_license

# Don't change the line below
print("Eligible to drive:", result)
quiz iconTest yourself

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

All lessons in Fundamentals