Skip to main content

How to Check Whether Either of Two Conditions Is True in Python with or

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

"Is it Saturday or Sunday?" is two questions with one answer: yes if either is true. Python writes that with or. It also checks the two in order and stops early, which lets you guard a test that would fail on bad data. This guide shows both on Python 3.15.2.

Syntax​

Syntax
if <first condition> or <second condition>:
<code to run when at least one is true>
PartWhat it means
<first condition>Checked first
orMakes the whole test true when either side is true
<second condition>Checked only when the first one is false

The Python language reference puts it exactly: "The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned." So when the first condition is true, Python never looks at the second.

The Example​

The first script tells weekend days from weekdays with one or. The second checks a name that may be None, where the order of the two conditions decides whether the script works.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

weekend.py
day = "Sunday"

if day == "Saturday" or day == "Sunday":
print("Weekend")
else:
print("Weekday")

The weekend.py script in a macOS-style code window: day is Sunday, and an if prints Weekend when day equals Saturday or day equals Sunday, otherwise Weekday.

Each side of or is a full comparison. day == "Saturday" or "Sunday" would not do the same thing, as Common Mistakes shows.

Step 2: Run It​

Terminal
uv run python weekend.py
Output
Weekend

The terminal after uv run python weekend.py: Weekend.

day == "Saturday" was False, so Python checked day == "Sunday", which was True, and the whole test was True. The command works unchanged in PowerShell.

Step 3: Let the First Condition Guard the Second​

len(name) fails when name is None. Put the None check first, and Python never reaches len() for a missing name:

name_check.py
name = None

if name is None or len(name) == 0:
print("No name given")
Terminal
uv run python name_check.py
Output
No name given

Now swap the two conditions:

name_check_swapped.py
name = None

if len(name) == 0 or name is None:
print("No name given")
Terminal
uv run python name_check_swapped.py
Output
Traceback (most recent call last):
File "/Users/you/source/python-basics/name_check_swapped.py", line 3, in <module>
if len(name) == 0 or name is None:
~~~^^^^^^
TypeError: object of type 'NoneType' has no len()

The terminal after both runs: name_check.py prints No name given, while name_check_swapped.py stops with TypeError: object of type NoneType has no len().

With name is None first, the test was True straight away, so len(name) never ran. With len(name) first, Python ran it on None and stopped with a TypeError before it reached the None check.

Common Mistakes​

MistakeWhat happensDo this instead
if day == "Saturday" or "Sunday":Always true: with day = "Monday" it printed Weekend, because the right side is just the text "Sunday", which counts as true on its ownWrite the full comparison on both sides
Putting the risky test first, as in len(name) == 0 or name is NoneTypeError: object of type 'NoneType' has no len()Put the guard first
Using or when both must be trueThe code runs when only one isUse and

FAQ​

What does or give outside an if?​

One of its two values, not always True or False: the reference says it returns the first value if that one is true, otherwise the second. That's what makes name = given or "Guest" work.

Can I chain more than two conditions?​

Yes: a or b or c checks them from left to right and stops at the first true one.

Conclusion​

day == "Saturday" or day == "Sunday" printed Weekend for Sunday. With name is None first, the check printed No name given and never called len(); with the order swapped, it stopped with a TypeError.

Additional Resources​