Coding/Learn/python/logical operators
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnLogical Operators (and, or, not)
14 / 32 0%
Lesson 14 of 32
6 mins read

Logical Operators (and, or, not)

PreviousNext Lesson
In Plain English

Real-world decisions often require checking multiple conditions simultaneously. Can a user enter? They need an active account AND a verified email. Can a player respawn? If they have an extra life OR 100 gold coins. Python uses clean English words: and, or, not.

The Mental Model

"Think of ordering lunch. If you say "I want pizza AND soda", you are only happy if you get both. If you say "I want tacos OR sushi", you are happy with either one. If you say "NOT spicy", you invert the condition."

Deep Dive: How It Works

and: Returns True ONLY if both conditions on the left and right are True. If either is False, the whole check is False.

or: Returns True if AT LEAST ONE condition is True. It only returns False if both sides are False.

not: Inverts whatever boolean follows it. not True becomes False, not False becomes True.

Short-Circuit Evaluation: Python is smart. In an "and" expression, if the first check is False, Python stops checking immediately because the result cannot be True. In an "or" check, if the first is True, Python skips the rest.

Syntax Blueprint

condition1 and condition2  # Both must be True
condition1 or condition2   # At least one must be True
not condition               # Inverts True <-> False

Use plain English keywords and, or, not to compose compound logic.

PointCore Rules to Remember

Point star
and requires BOTH: True and True is True. Anything with a False becomes False.
Point star
or requires ONE: True or False is True. Only False or False is False.
Point star
not inverts truth: Turns True into False, and False into True.

Common Beginner Traps & How to Fix Them

Mistake:Writing if x == 1 or 2:

Why it happens: Python interprets this as (x == 1) or (2). Since 2 is truthy, this condition is ALWAYS True.

How to fix: Write out both sides: if x == 1 or x == 2: or use membership: if x in (1, 2):.

Pro Tip: Use parentheses to group complex logic for readability: if (is_vip or score > 1000) and not is_banned:.

Live Interactive Example

Hit Run Code to see it live
Combining Conditions with and, or, not
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: The user gained general access via has_ticket, but failed VIP access because has_vip_pass was False.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Check eligibility with and

  • PointGiven age = 20 and has_license = True, check if both are satisfied.
  • PointPrint the boolean result of (age >= 18 and has_license).
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates