Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use or in Python

Setting the Stage

Before we dive in, let's take a moment to set the stage. Imagine you're a chef trying to cook a delicious meal. You have a recipe that calls for either onions or garlic. You don't need both, but at least one of the two is necessary. If you have either onions or garlic, you can proceed with the recipe. In the world of programming, this is akin to the logical operator "or".

In Python, "or" is a logical operator that you can use to test whether any of at least two conditions is True. If either (or both) of the conditions is True, the "or" operator returns True. If both conditions are False, the "or" operator returns False. This might sound a bit abstract now, but it will become clearer once we go through some actual code examples.

A Basic Example of "or" in Python

Let's start with a basic example. Suppose we have two variables, a and b, and we want to check if either a or b is True. Here's how we can do this in Python:

a = True
b = False

if a or b:
    print("At least one of a or b is True.")
else:
    print("Neither a nor b is True.")

In this case, the output of the program will be "At least one of a or b is True." because a is True.

Adding More Conditions

You are not limited to using the "or" operator with just two conditions. You can use it with three, four, or even more conditions. Let's say we now have three variables, a, b, and c, and we want to check if at least one of them is True. Here's how we can do that:

a = True
b = False
c = False

if a or b or c:
    print("At least one of a, b, or c is True.")
else:
    print("Neither a, b, nor c is True.")

In this case, the output will still be "At least one of a, b, or c is True." because a is True.

"or" with Non-Boolean Values

In Python, the "or" operator does not only work with Boolean values (True and False). It also works with non-Boolean values. When used with non-Boolean values, the "or" operator returns the first "truthy" value if there is one. If all values are "falsy", it returns the last value.

In Python, the following values are considered "falsy":

  • None
  • False
  • Zero (0, 0.0, 0j, etc.)
  • Empty collections (list, set, dictionary, string, tuple, etc.)
  • Objects of classes that define their __bool__() or __len__() method to return 0 or False

All other values are considered "truthy". Let's look at an example:

a = 0
b = "Hello, World!"

print(a or b)  # This prints: Hello, World!

In this case, since a is a "falsy" value (because it's zero), the "or" operator returns b.

Conclusion

Just like the chef who can make a delicious meal with either onions or garlic, Python gives us the flexibility to check for multiple conditions using the "or" operator. It's a simple yet powerful tool that can make our code more efficient and easier to read.

Remember, the "or" operator in Python checks if at least one of the given conditions is True. It works not only with Boolean values, but with non-Boolean values as well. This is part of Python's charm and one of the reasons why it's loved by programmers around the world.

As you continue to explore Python, you'll find that the "or" operator, along with other logical operators like "and" and "not", will become indispensable tools in your programming toolkit. So, keep experimenting, keep learning, and most importantly, have fun coding!