Logical Operators in Python
Have you ever made a decision based on more than one thing? For example, maybe you’ll only go for a walk if it’s sunny and you have free time. Or perhaps…
Have you ever made a decision based on more than one thing? For example, maybe you’ll only go for a walk if it’s sunny and you have free time. Or perhaps you’ll watch a movie if you finish your homework or it’s the weekend. In programming, we often need to make choices based on several conditions, just like in real life.
In this article, you’ll learn how to combine conditions in Python using logical operators. By the end, you’ll know how to use and, or, and not to make your programs smarter and more flexible.
What Are Logical Operators?
Logical operators are special words in Python that let you combine two or more conditions. They help your program make decisions by checking if certain things are true or false.
The most common logical operators in Python are:
andornot
These are the building blocks of Python boolean logic, which is all about working with values that are either True or False.
Why Combine Conditions?
Think about everyday decisions. Sometimes, you need more than one thing to be true before you act:
- You’ll eat dessert if you finish your dinner and your chores.
- You’ll buy a new game if it’s on sale or you get a gift card.
In programming, combining conditions lets you write code that reacts to more complex situations. This makes your programs much more powerful and flexible.
For example, you might want your program to print a message only if a user is logged in and has admin rights. Or maybe you want to allow access if the user is an admin or a moderator.
The and Operator Explained
The and operator checks if both conditions are true. If they are, the result is True. If either condition is false, the result is False.
Here’s a simple example:
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("You can enter the concert.")
else:
print("Sorry, you can't enter.")
In this example, both age >= 18 and has_ticket must be true for the message to print.
Think of and as needing all the green lights before you go!
Quiz Question 1
Question: Which statement about the and operator in Python is correct?
- A) It returns True only if both conditions are True.
- B) It returns True if at least one condition is True.
- C) It flips the value of a condition from True to False.
The or Operator Explained
The or operator checks if at least one condition is true. If either (or both) are true, the result is True. If both are false, the result is False.
Here’s how it looks:
is_weekend = False
finished_homework = True
if is_weekend or finished_homework:
print("You can play video games!")
else:
print("No games until you finish your homework or it's the weekend.")
With or, you only need one green light to go.
Quiz Question 2
Question: What does the or operator do in Python?
- A) Returns True only if both conditions are True
- B) Returns True if at least one condition is True
- C) Returns False if both conditions are True
The not Operator Explained
The not operator flips a condition. If something is True, not makes it False. If it’s False, not makes it True.
Let’s see an example:
is_raining = False
if not is_raining:
print("Let's go outside!")
else:
print("Better stay indoors.")
Here, not is_raining is True because is_raining is False. So the message to go outside will print.
not is like saying, “If it’s not raining, let’s go!”
Quiz Question 3
Question: What does the not operator do to a condition that is already False?
- A) Leaves it as False
- B) Changes it to True
- C) Changes it to None
Using Logical Operators in Python Conditions
Logical operators are most often used in if statements to control what your program does.
Here’s how you might use all three:
age = 16
has_permission = False
if age >= 18 or has_permission:
print("You can join the club.")
else:
print("Sorry, you can't join yet.")
Or combine them:
is_member = True
has_paid = False
if is_member and not has_paid:
print("Please pay your membership fee.")
Combining Multiple Conditions
You can use more than two conditions with and or or. For example:
if age >= 18 and has_ticket and not is_banned:
print("You can enter.")
You can also mix and, or, and not in the same condition. Parentheses can help make your intention clear:
if (is_admin or is_moderator) and not is_banned:
print("Access granted.")
Quiz Question 4
Question: What is the purpose of using parentheses when combining logical operators in Python?
- A) They are required for every logical operator
- B) They make the order of evaluation clear
- C) They change the value of the conditions
Common Mistakes Beginners Make
- Forgetting parentheses: Sometimes, grouping conditions with parentheses makes your code clearer and avoids confusion.
- Mixing up
andandor: Remember,andneeds everything to be true,oronly needs one. - Using
notincorrectly: Make sure you know what condition you’re flipping!
Tips for Reading and Writing Combined Conditions
- Read your conditions out loud. Does it make sense in plain English?
- Use parentheses to group conditions if it helps you understand them.
- Start simple. Combine two conditions first, then try more.
Practice: Test Your Understanding
Let’s try a few quick questions!
-
You can ride the roller coaster if you are at least 12 years old and taller than 140 cm.
Write a condition usingand:age = 13 height = 145 if age >= 12 and height > 140: print("You can ride!") else: print("Sorry, you can't ride.") -
You can have dessert if you finished your dinner or your parent says yes.
Which operator will you use?finished_dinner = False parent_says_yes = True if finished_dinner or parent_says_yes: print("You can have dessert!") else: print("No dessert this time.") -
You want to go outside only if it’s not raining.
How would you write that?is_raining = True if not is_raining: print("Go outside and play!") else: print("Stay indoors.")
Try changing the values and see what happens!
Summary
Logical operators in Python—and, or, and not—let you combine conditions and make smarter decisions in your code. They’re a key part of Python boolean logic and are used in almost every real-world program.
Now that you know how to use python logical operators, try writing your own conditions! Practice combining them in different ways and see how your programs become more powerful. You’re well on your way to thinking like a programmer!
Quiz Answer Key
Question 1
Correct answer: A) It returns True only if both conditions are True.
Explanation: The and operator requires all conditions to be True for the result to be True. If either is False, the result is False.
Question 2
Correct answer: B) Returns True if at least one condition is True
Explanation: The or operator returns True if either or both conditions are True. Only if both are False does it return False.
Question 3
Correct answer: B) Changes it to True
Explanation: The not operator flips the value. If a condition is False, not makes it True.
Question 4
Correct answer: B) They make the order of evaluation clear
Explanation: Parentheses help group conditions and clarify which parts of the condition are checked first, making your code easier to read and understand.