DEV Community

Bonface Thuo
Bonface Thuo

Posted on

🔀 DAY 10: Logical Operators

Welcome to Day 10, squad! 🚀 Yesterday, we learned how to check single conditions, like whether a user is logged in. But in the real world, decisions are rarely that simple.

Think about a streaming app like Netflix. To play a premium movie, a user needs to be logged in AND they must have an active paid subscription. If they only have one of those things, access is denied!❌

Today, we are learning how to chain multiple comparisons together using Python's gatekeepers: Logical Operators.

🚪 Meet the Three Gatekeepers: and, or, not

Python uses clean, simple English words to handle logic gating:

1. The Strict Gatekeeper: and

The and operator only returns True if every single condition on both sides is perfectly true. If even one part is false, the whole thing collapses into False.

has_email = True
has_password = False

print(has_email and has_password)  # Prints: False (Missing password!)
Enter fullscreen mode Exit fullscreen mode

2. The Relaxed Gatekeeper: or

The or operator is super chill. It returns True if at least one of the conditions is true. It only outputs False if absolutely everything is broken.

has_cash = False
has_credit_card = True

print(has_cash or has_credit_card)  # Prints: True (We can still pay! 💳)
Enter fullscreen mode Exit fullscreen mode

3. The Rebel: not

The not operator is a simple inverter. It flips whatever Boolean value comes after it completely upside down. It turns True to False, and False to True.

is_game_over = False

print(not is_game_over)  # Prints: True
Enter fullscreen mode Exit fullscreen mode

🚀 Today's Challenge 🏆

Let's build a gate for an online shop checkout system!

Create a boolean item_in_stock = True.

Create a boolean wallet_loaded = False.

Create a boolean has_discount_coupon = True.

Write a logical statement to check if a user can buy the item. They can buy it if item_in_stock is true AND either (wallet_loaded is true OR has_discount_coupon is true).

Print out your final result.

Did your checkout process succeed? Let me know in the comments! Tomorrow, we finally make our scripts dynamic using If/Else Conditional Statements! 🛣️

Top comments (0)