Logical Operators: 'or' for Alternative Conditions
From the csc241\ curriculum
TL;DR
The or logical operator lets you check if at least one of several conditions is true. If any condition connected by or is true, the entire expression becomes true. This is super useful when your code needs to react to multiple possible situations.
1. The Mental Model
Think of or like saying, "Is this true, or is that true, or is something else true?" If any of those individual things are true, then the whole statement is true. It only becomes false if all the individual things are false.
2. The Core Material
In programming, you often need to make decisions based on whether any of several possibilities is true. That's where the or operator comes in. It combines two or more boolean expressions (things that evaluate to True or False) and returns True if at least one of them is True. If all of them are False, then the or expression returns False.
Let's look at how it works:
Understanding or with True and False

Photo by Pixabay on Pexels
| Condition 1 | or |
Condition 2 | Result |
|---|---|---|---|
True |
or |
True |
True |
True |
or |
False |
True |
False |
or |
True |
True |
False |
or |
False |
False |
As you can see, the only way for an or statement to be False is if all its parts are False.
Using or in if statements

Photo by Benjamin Lehman on Pexels
The or operator is most commonly used inside if statements (and elif statements) to create flexible conditions.
age = 17
is_student = True
# Check if someone is eligible for a student discount
# They are eligible if they are a student OR if they are under 18
if is_student or age < 18:
print("You qualify for a student discount!")
else:
print("No student discount for you.")
In this example, is_student is True, so True or (17 < 18) becomes True or True, which evaluates to True. The if block executes.
If is_student was False and age was 20:
False or (20 < 18) becomes False or False, which evaluates to False. The else block would execute.
Combining Multiple or Conditions

Photo by Mike van Schoonderwalt on Pexels
You can link as many conditions as you need with or.
weather = "rainy"
day_of_week = "Saturday"
has_umbrella = True
# Check if it's a good day to stay inside
if weather == "rainy" or day_of_week == "Sunday" or not has_umbrella:
print("Let's stay in and read a book.")
else:
print("Perfect day for an outing!")
Here, weather == "rainy" is True. Because one part of the or chain is True, the entire condition becomes True immediately, and Python doesn't even need to check the other conditions (day_of_week == "Sunday" or not has_umbrella). This is called "short-circuiting."
Here's a diagram showing how an or condition flows:
graph TD
A["Start evaluating 'or' condition"] --> B{Is Condition 1 True?};
B -- Yes --> D["Overall 'or' is True"];
B -- No --> C{Is Condition 2 True?};
C -- Yes --> D;
C -- No --> E{Is Condition 3 True?};
E -- Yes --> D;
E -- No --> F["Overall 'or' is False"];
D --> G["Proceed with True branch"];
F --> H["Proceed with False branch"];
3. Worked Example
Let's say you're writing a simple game and a player can only pick up a special item if they meet certain criteria: they must have at least 10 gold coins, OR they must have a magic key.
player_gold = 7
player_has_magic_key = False
required_gold = 10
if player_gold >= required_gold or player_has_magic_key:
print("You picked up the special item!")
else:
print("You don't meet the requirements to pick up the item.")
# Let's trace it:
# 1. Is player_gold >= required_gold?
# Is 7 >= 10? No, this is False.
# 2. Since the first part was False, we check the second part.
# Is player_has_magic_key True?
# Is False True? No, this is False.
# 3. Both parts are False, so (False or False) is False.
# 4. The else block executes.
Now, what if the player had 12 gold?
player_gold = 12
player_has_magic_key = False
required_gold = 10
if player_gold >= required_gold or player_has_magic_key:
print("You picked up the special item!")
else:
print("You don't meet the requirements to pick up the item.")
# Let's trace this:
# 1. Is player_gold >= required_gold?
# Is 12 >= 10? Yes, this is True.
# 2. Since the first part was True, the entire 'or' condition is True (due to short-circuiting).
# Python doesn't even bother checking player_has_magic_key.
# 3. The if block executes.
4. Key Takeaways
- The
oroperator returnsTrueif any of its combined conditions areTrue. - It only returns
Falseif all of its combined conditions areFalse. - You can chain multiple conditions together using
or(e.g.,cond1 or cond2 or cond3). oris very useful inifandelifstatements when you need to accept multiple alternatives.- Python uses "short-circuiting": if the first condition in an
orchain isTrue, it stops checking the rest.
Common Mistakes to Avoid

Photo by KATRIN BOLOVTSOVA on Pexels
- Using
orwhen you meanand: Make sure you truly need any condition to be true, not all of them. - Forgetting parentheses with complex conditions: If you mix
andandorin one line, use parentheses to clarify the order of operations, just like in math. - Writing
if x == 1 or 2instead ofif x == 1 or x == 2: Each side of theorneeds to be a complete boolean expression.2by itself is consideredTruein a boolean context, which isn't what you want. - Overcomplicating conditions: Sometimes a simpler
if/elif/elsestructure is clearer than a very long chain oforconditions.
5. Now Try It
Imagine you're building a login system. A user should be considered an "admin" if their username is "admin" OR their access_level is greater than 9.
Write a Python script that defines a username (e.g., "john.doe") and an access_level (e.g., 7). Then, use an if statement with the or operator to check if this user is an admin. Print a message indicating whether they are an admin or not. Test it with different combinations: a regular user, a user with access_level 10, and a user named "admin".
What success looks like: Your code correctly identifies an admin if either the username is "admin" OR the access_level is above 9, and correctly identifies non-admins otherwise.
Frequently asked about Logical Operators: 'or' for Alternative Conditions
More from csc241\
Get the full csc241\ curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free