Blog Post #23: Combining Ideas: The Logical Operators and, or, and not

In Post #22, we learned how to ask simple questions using comparison operators, resulting in a True or False value. This is a great start, but real-world logic is often more complex. We frequently need to check multiple conditions at once.

For example: Is the user over 18 and a resident of this country? Does the player have the magic sword or the magic shield? Is the game not over?

To handle these compound questions, Python provides three logical operators: and, or, and not. This post will teach you how to use them to build complex Boolean expressions.

The and Operator: When Both Must Be True

The and operator evaluates two boolean expressions. It returns True only if both the expression on its left and the expression on its right are True. If either side (or both) is False, the entire expression becomes False.

Think of it like this:

  • True and True evaluates to True
  • True and False evaluates to False
  • False and True evaluates to False
  • False and False evaluates to False

A perfect real-world example is qualifying for a driver’s license. You must be old enough AND you must have passed the test.

age = 19
has_passed_test = True

# Both conditions must be True to get a license
can_get_license = (age >= 18) and (has_passed_test == True)

print(f"Age is sufficient: {age >= 18}")
print(f"Has passed test: {has_passed_test == True}")
print(f"Can get license: {can_get_license}")

Since both sub-expressions are True, the final result is True.

The or Operator: When at Least One is Enough

The or operator also evaluates two boolean expressions. It returns True if at least one of the expressions is True. It only returns False if both sides are False.

Think of it like this:

  • True or True evaluates to True
  • True or False evaluates to True
  • False or True evaluates to True
  • False or False evaluates to False

An example could be entering a secure area. You might be allowed in if you have a keycard OR you know the secret passcode.

has_keycard = False
knows_passcode = True

# Only one of the conditions needs to be True to gain access
can_enter_area = has_keycard or knows_passcode

print(f"Has keycard: {has_keycard}")
print(f"Knows passcode: {knows_passcode}")
print(f"Can enter area: {can_enter_area}")

Because knows_passcode is True, the entire or expression evaluates to True.

The not Operator: The Opposite Day Operator

Unlike and and or, the not operator only works on a single boolean expression to its right. Its job is to simply invert or “flip” the value.

  • not True evaluates to False
  • not False evaluates to True

This is very useful for checking for the absence of a condition. For example, we might want some code to run only while a game is still in progress.

is_game_over = False

# We want to know if the game is still running
is_game_running = not is_game_over

print(f"Is the game over? {is_game_over}")
print(f"Is the game still running? {is_game_running}")

The output will be True because not False is True. Using if not is_game_over: is often considered more readable and “Pythonic” than writing if is_game_over == False:.

What’s Next?

You have now unlocked the ability to create complex logical conditions. By combining boolean expressions with and, or, and not, you can model almost any real-world scenario, from simple checks to intricate business rules.

We’ve been working with the explicit boolean values True and False. However, Python has a powerful concept where other data types, like numbers and strings, can be treated as if they were booleans in a logical context. What does it mean for the number 0 or an empty string "" to be ‘true’ or ‘false’? In Post #24, we will explore this fascinating and very ‘Pythonic’ concept of Truthy and Falsy values.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment