If with multiple “or” conditions in Python: Explained with Examples

In this article, let us see some examples of how to write if statements with multiple or conditions in Python.

Let us start with the simple case of just 2 conditions!

Example for “condition1 or condition2”

Before we look at the example, one point worth noting is that

The ‘or’ operator returns True even if one condition is found to be True, no matter how many conditions there are!

Here’s a simple example with 2 conditions combined via the ‘or’ operator.

num = int(input("Please enter a number: "))

if num > 0 or num % 2 == 0:
    print("Your number is either positive or even or both!")

In this program, the ‘or’ operator just sees if a number is either positive or even.

If even one condition is True, the whole condition is considered True. In our case, both of the conditions are True. Hence the statements inside the if block will be executed.

Also, notice how natural Python language feels when writing if statements with multiple conditions!

Next, let’s kick things up a notch and see another example this time with 3 conditions!

Example for “condition1 or condition2 or condition3”

experience_years = 3
completed_internship = True
has_relevant_degree = False

if experience_years >= 2 or completed_internship or has_relevant_degree :
    print("You are eligible to apply to MNCs!")

The above if statement checks if the user is eligible to apply for a position if they satisfy any of the following criteria.

  1. Has more than 3 years of experience
  2. Has completed an internship relevant to the position
  3. Has a relevant degree

In this particular case, 2 or 3 conditions are True, and the minimum requirement is just 1 condition to be True. hence the statements inside the if-block will be executed and we will get the following output.

You are eligible to apply to MNCs!

The Fundamentals Of Writing If with Multiple Conditions

Learning from examples can only take you so far. If you wish to get your fundamentals strong and learn what happens behind the scenes, then I strongly suggest watching the video we made on that topic shown below.

You can only cover so much in a video, here is its “article-counterpart“!

Python “if” Statement with Multiple Conditions: Explained with Examples!

Variations of writing the if statement+

We can also use different techniques to write the if statement. What we can do is, we can evaluate the conditions prior to the if statement. Then we pass the boolean results of these conditions directly to the if statement.

Let’s do this for both the programs we saw earlier in this article:

num = int(input("Please enter a number: "))

is_positive = num > 0 
is_even = num % 2 == 0

if is_positive or is_even:
    print("Your number is either positive or even or both!")

The variables is_positive and is_even evaluate the conditions and store the results (i.e. the boolean values).

These are in turn used in the if statement which directly evaluates it. Pretty neat right!

Here’s another program that can be re-written:

num = int(input("Please enter a number: "))

if num > 0 or num % 2 == 0 or num % 4 == 0:
    print("Your number is a either 'positive' or 'even' or 'is a multiple of 4'!")

Challenge time

Theory only gets you so far! It’s time to practice! 

The challenge for you is to refer back to the num_analysis_rewritten.py and rewrite the code above!

You will need to check the appropriate conditions and store them in variables. All the best!

For those of you who didn’t get it, no worries! Here’s the answer:

num = int(input("Please enter a number: "))

is_positive = (num > 0) 
is_even = (num % 2 == 0)
is_multiple_of_4 = (num % 4 == 0)

if is_positive or is_even or is_multiple_of_4:
    print("Your number is a either 'positive' or 'even' or 'is a multiple of 4'!")

By writing the boolean values of the condition beforehand and not dumping it all into one if statement line, we improve the readability of the code and avoid making the if statement look like a complex formula!

And with that, I will end this article.

Congratulations on making it to the end of the article, not many have the perseverance to do so!

I hope you enjoyed reading this article and found it useful!

Feel free to share it with your friends and colleagues!

If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!

Related Articles

If with multiple “and” conditions in Python: Examples

If-Else in Python: Explained with Examples!

Python “if” Statement with Multiple Conditions: Explained with Examples!

Python: “if not true” How to write? Explained with Examples!

Thanks to Namazi Jamal for his contributions in writing this article!

Photo of author
Editor
Balaji Gunasekaran
Balaji Gunasekaran is a Senior Software Engineer with a Master of Science degree in Mechatronics and a bachelor’s degree in Electrical and Electronics Engineering. He loves to write about tech and has written more than 300 articles. He has also published the book “Cracking the Embedded Software Engineering Interview”. You can follow him on LinkedIn