Logical Operators: 'or' for Alternative Conditions

SA
StudyAI
AI-generated study notes
· Published Updated

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

Close-up of PHP code on a monitor, highlighting development and programming concepts.
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

A man in a cap and hoodie using a smartphone in a dimly lit, moody urban setting.
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

Red building exterior with multiple windows and air conditioners, showcasing urban architecture.
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 or operator returns True if any of its combined conditions are True.
  • It only returns False if all of its combined conditions are False.
  • You can chain multiple conditions together using or (e.g., cond1 or cond2 or cond3).
  • or is very useful in if and elif statements when you need to accept multiple alternatives.
  • Python uses "short-circuiting": if the first condition in an or chain is True, it stops checking the rest.

Common Mistakes to Avoid

Flat lay of a spiral notebook and eraser on a pastel pink background with crossed out words.
Photo by KATRIN BOLOVTSOVA on Pexels

  • Using or when you mean and: Make sure you truly need any condition to be true, not all of them.
  • Forgetting parentheses with complex conditions: If you mix and and or in one line, use parentheses to clarify the order of operations, just like in math.
  • Writing if x == 1 or 2 instead of if x == 1 or x == 2: Each side of the or needs to be a complete boolean expression. 2 by itself is considered True in a boolean context, which isn't what you want.
  • Overcomplicating conditions: Sometimes a simpler if/elif/else structure is clearer than a very long chain of or conditions.

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

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. Read the full notes above for the details.

Logical Operators: 'or' for Alternative Conditions is a core topic in csc241\. Most exam papers test it via a mix of definitions, worked examples, and applied problems. The notes above cover the high-yield sub-topics, common pitfalls, and the kind of questions examiners typically set.

Yes — every note in the StudyAI Campus Hub is free to read in full, right here on this page, with no account needed. If you clone the plan into your own dashboard, the free plan shows a preview of each note there; Basic and above unlock the full notes in your dashboard, along with practice quizzes, flashcards and offline study. You can always come back here to read the complete note for free.
Continue with
Logical Operators: 'and' for Conjunctions

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