Introduction to Python

Logical Operators

Lesson 6: Conditional Statements

What are Logical Operators?

Logical operators allow you to combine multiple conditions into a single expression. They are essential for writing complex conditional statements in Python.

The Three Logical Operators

Two green checkmarks side by side with the word AND between them — both must be true
AND — Both must be True
One green checkmark and one red cross with the word OR between them — at least one must be true
OR — At least one True
A True label with a flip arrow pointing to False representing the NOT operator that inverts a boolean value
NOT — Flips the value

Python Logical Operators

OperatorDescriptionExampleResult
andTrue only if BOTH conditions are true5 > 3 and 8 > 6True
orTrue if AT LEAST ONE condition is true5 > 3 or 2 > 8True
notFlips the Boolean valuenot (5 > 3)False

Truth Tables

AND Truth Table

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

OR Truth Table

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Using Logical Operators in Python

age = 20 has_id = True # AND — both must be true if age >= 18 and has_id: print("Access granted.") # OR — at least one must be true if age >= 18 or has_id: print("Partial access.") # NOT — flip the condition if not has_id: print("Please show your ID.")

Three illustrated scenarios — a door with two locks for AND, a door with one lock for OR, and a no-entry sign being reversed for NOT — showing real-world analogies for logical operators
Logical operators are evaluated after comparison operators. Python evaluates not first, then and, then or — just like PEMDAS for arithmetic!

Subscribe to our newsletter.

Get updates to news and events.