Mastering Logical Operators in Python – A quick visual guide to using and, or, and not in real-world Python conditions.
Logical Operators in Python: Alright, so you’re learning Python — that’s awesome! One important thing you’ll need to understand is how to make decisions in your code. Just like we do in real life.
Let’s say:
See those and and or words? That’s exactly what logical operators do in Python. They help your code say things like:
In Python, we use just three simple logical operators:
andornotThat’s it! But with just these three, you can do a lot.
In this guide, I’m going to teach you how each one works, with simple examples that actually make sense. We’ll try things out like you’re coding along with me.
Ready? Let’s start with and — the one that checks if two things are true at the same time.
Logical operators in Python are special keywords that help your program make decisions. They connect two or more conditions and check whether they’re true or false. Based on that, Python decides what to do next.
You use logical operators when you want your program to check multiple things at once — like whether a user is logged in and has admin rights, or whether a value is too high or too low.
Python has three core logical operators:
and – returns True only if both conditions are trueor – returns True if at least one condition is truenot – flips the result; returns True if the condition is not trueThese are called logical operators because they work with Boolean logic — the part of programming that deals with True and False.
Here’s a quick look at the logical operators in Python symbol and what they mean:
| Operator | Symbol | What It Means |
|---|---|---|
and | and | True if both conditions are true |
or | or | True if at least one is true |
not | not | True if the condition is false |
Simple, right? You’ll use these all the time once you get the hang of them.
Up next, I’ll show you how and works with a few examples, so you can actually see it in action.
and in PythonLet’s start with the and operator.
In everyday language, you might say:
“If I finish my homework and it’s not raining, I’ll go outside.”
In this sentence, you’re only going out if both things happen. That’s exactly what and does in Python. It checks if two (or more) conditions are true at the same time.
Let’s look at a simple example:
x = 5
if x > 2 and x < 10:
print("x is between 2 and 10")
Let me walk you through this line by line:
x = 5: We’re setting a variable x to the value 5.if x > 2 and x < 10::x > 2 → Is 5 greater than 2? Yes! So this is True.x < 10 → Is 5 less than 10? Also yes! So this is True too.if statement is considered True.So Python goes ahead and runs the print() line, giving you this output:
x is between 2 and 10
Let’s change the value of x and see what happens:
x = 12
if x > 2 and x < 10:
print("x is between 2 and 10")
Let’s go through it again:
x = 12, so now x is 12.x > 2 checks if 12 is greater than 2 — and that’s True.x < 10. Since 12 isn’t less than 10, the result is False.Now here’s the key part:
Even though one condition is true, the and operator needs both to be true. Since the second part is false, the whole condition is false.
So what happens?
Python does not run the print() statement, and you get no output.
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("You can enter the event.")
Here:
age >= 18 → 20 is greater than or equal to 18 → Truehas_ticket → is already TrueSince both conditions are true, the message gets printed:
You can enter the event.
But if has_ticket = False, then the condition fails — and you don’t get in.
In short:
Use and when you want all conditions to be true before something happens.
Up next, we’ll look at or, which works a little differently — you only need one condition to be true.
or in PythonThe or operator helps your program make a decision when you only need one condition to be true.
Here’s a simple way to understand it:
“If this happens or that happens, do something.”
If either condition is true, the result is true.
x = 12
if x < 10 or x > 11:
print("x is either less than 10 or greater than 11")
Let’s break it down:
x = 12 → We’re saying x is 12x < 10 → Is 12 less than 10? No. So this is Falsex > 11 → Is 12 greater than 11? Yes. So this is TrueNow, because we’re using or, only one of these conditions needs to be true.
Since the second one is true, the whole condition is considered True.
So Python runs the print() line and shows:
x is either less than 10 or greater than 11
x = 8
if x < 5 or x > 15:
print("x is outside the 5 to 15 range")
x = 8Now both conditions are false, so Python skips the print() line.
You see no output.
Let’s say it’s the weekend or a holiday. You want to sleep in if either one is true.
is_weekend = True
is_holiday = False
if is_weekend or is_holiday:
print("You can sleep in today!")
is_weekend is Trueis_holiday is FalseBecause one of them is True, the message prints:
You can sleep in today!
If both were False, nothing would print.
The key idea is this:
With or, you just need one condition to be true for the whole thing to be true.
not in PythonThe not keyword is used when you want to say “only if something is not true.”
It basically flips the meaning of a condition.
Let me show you:
True, not makes it False.False, not makes it True.Let’s say there’s a rule:
“If you’re not tired, you can go play.”
That means:
In Python, we use not the same way.
is_tired = False
if not is_tired:
print("You can go play!")
What’s going on here?
is_tired = False (you are not tired)not is_tired means: “Is it true that you’re not tired?” → Yes!So Python runs the code and prints:
You can go play!
is_logged_in = True
if not is_logged_in:
print("Please log in.")
else:
print("Welcome back!")
Here:
is_logged_in = True (you are logged in)not is_logged_in → “You are not logged in?” → No, that’s not true.else part and prints:Welcome back!
not?Because sometimes we want to check if something is not happening.
It makes our code easier to read.
Examples:
not raining → it’s dry outsidenot finished → still working on itis_daytime = False
if not is_daytime:
print("It's night time!")
Output:
It's night time!
not True → Falsenot False → TrueThat’s it!
Comparison operators in Python are used to compare values.
They help you ask questions like:
Here are the main comparison operators you’ll use:
| Symbol | Meaning | Example |
|---|---|---|
< | Less than | x < 10 → True if x is less than 10 |
> | Greater than | x > 5 → True if x is more than 5 |
== | Equal to | x == 7 → True if x is equal to 7 |
!= | Not equal to | x != 3 → True if x is not 3 |
<= | Less than or equal to | x <= 8 → True if x is 8 or less |
>= | Greater than or equal to | x >= 6 → True if x is 6 or more |
Now here’s the cool part: you can combine comparison operators using logical operators like and, or, and not.
Let’s say you want to check if a number is between 5 and 15.
That means:
Here’s how you write that in Python:
x = 10
if x > 5 and x < 15:
print("x is in range")
x > 5 → Is 10 greater than 5? Yesx < 15 → Is 10 less than 15? YesTrue, and since we’re using and, the final result is TrueSo Python prints:
x is in range
orage = 17
if age < 13 or age > 65:
print("You get a discount!")
This checks:
Since 17 is neither, nothing gets printed.
But if age = 10, then you’d see:
You get a discount!
You’ll often use both together in if statements to control how your program behaves.
Short answer: No, assignment operators are not logical operators.
But they’re super important, and you’ll often see them used alongside logical and comparison operators—especially inside if statements or loops.
Assignment operators are used to store values in variables or update those values.
Here’s a quick list of common assignment operators:
| Operator | What it does | Example |
|---|---|---|
= | Assigns a value | x = 5 |
+= | Adds and assigns | x += 2 → x = x + 2 |
-= | Subtracts and assigns | x -= 3 → x = x - 3 |
*= | Multiplies and assigns | x *= 4 → x = x * 4 |
/= | Divides and assigns | x /= 2 → x = x / 2 |
%= | Modulo and assigns (remainder) | x %= 3 → x = x % 3 |
So, for example:
x = 10
x += 5
print(x) # Output: 15
You’re just saying: “Take whatever is in x, add 5 to it, and store it back in x.”
Logical operators like and, or, and not are used to check if something is true or false.
Assignment operators, like = or +=, are used to set or update values.
Here’s a simple way to remember:
Here’s how assignment and logical operators often appear in the same code:
x = 7
if x > 5 and x < 10:
x += 1 # This is an assignment operator
print("x is now:", x)
x > 5 and x < 10 → this is a logical checkx += 1 → this is an assignmentThey’re just doing different jobs.
x = 3 # assignment
if x == 3: # comparison
print("x is 3")
Bitwise operators in Python work with bits—the 1s and 0s that make up numbers inside the computer.
Here are the most common bitwise operators:
| Symbol | Name | What it does |
|---|---|---|
& | Bitwise AND | 1 if both bits are 1 |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | 1 if only one bit is 1 (but not both) |
~ | Bitwise NOT | Flips all bits (1 becomes 0, and 0 becomes 1) |
These might sound a bit tricky at first—but don’t worry. You’ll understand them better when you see how they’re different from logical operators.
Here’s the key:
and, or, and not work with True and False (Boolean values).&, |, ^, and ~ work with numbers at the bit level (0s and 1s).Let’s compare them side by side using examples.
# Logical AND
print(True and False) # Output: False
This checks: Are both True? No. So the result is False.
# Bitwise AND
print(1 & 0) # Output: 0
This looks at the bits:
1 in binary = 00010 in binary = 0000Bitwise AND:
0001
& 0000
= 0000 → which is 0
# Logical OR
print(True or False) # Output: True
This says: Is at least one value True? Yes → True.
# Bitwise OR
print(1 | 0) # Output: 1
Bitwise OR:
0001
| 0000
= 0001 → which is 1
print(1 ^ 0) # Output: 1
print(1 ^ 1) # Output: 0
^ means “either 1 or the other is 1, but not both.”1 ^ 0 = 11 ^ 1 = 0x = 2
print(~x) # Output: -3
This one flips the bits of the number. It’s a little advanced because it also uses something called two’s complement, which is how computers handle negative numbers.
But for now, just know that ~x means: flip all the bits in x.
| Type | Works With | Example |
|---|---|---|
| Logical | True, False | True and False → False |
| Bitwise | Numbers (bits) | 1 & 0 → 0 |
You’ll use logical operators more in everyday conditions (if statements).
Bitwise operators are handy when dealing with things like low-level data or binary logic.
in and not in in PythonPython gives us two special membership operators:
innot inThese are used to check if a value exists inside something, like a list, string, or dictionary.
| Operator | Meaning |
|---|---|
in | Checks if a value exists in a sequence |
not in | Checks if a value does NOT exist |
Let’s go through an example together.
fruits = ['apple', 'banana']
if 'apple' in fruits and 'cherry' not in fruits:
print("Apple is there, cherry is not.")
What’s happening here?
fruits that has 'apple' and 'banana'.'apple' in fruits → This checks: Is 'apple' in the list? ✅ Yes'cherry' not in fruits → This checks: Is 'cherry' NOT in the list? ✅ YesTrue, and since we used and, the print() statement runs.Output:
Apple is there, cherry is not.
in and not in?You can use these with:
name = "Alexander"
if 'x' in name:
print("The letter 'x' is in the name!")
student = {'name': 'Lara', 'age': 20}
if 'name' in student:
print("Name key is found!")
You can combine in and not in with and, or, and not to build more complex conditions.
For example:
colors = ['red', 'blue']
if 'red' in colors or 'green' in colors:
print("At least one color is there.")
This prints because 'red' in colors is True.
Logical operators like and, or, and not may seem simple, but they are a big part of writing smart, decision-making code in Python. They help your programs make choices—like whether to run a block of code or not—based on conditions you set.
Here’s a quick recap:
and when both conditions must be true.or when at least one condition should be true.not to reverse a condition (True becomes False, and vice versa).You also learned how logical operators can combine with comparison operators, work inside if statements, and team up with membership operators like in and not in to check if a value exists.
Understanding these basics gives you the power to control how your code behaves. It’s like teaching your program how to think clearly!
If you’re just getting started with Python, practice writing your own conditions. Try mixing and, or, and not with different variables and values. The more you use them, the easier it gets.
Want to explore more Python topics step by step?
👉 Check out more guides at emitechlogic.com for hands-on tutorials
💡 Found this helpful?
Do me a favor — share this post with your friends, colleagues, or on your social media. You never know who might need a quick Python refresher before their next interview.
📸 Instagram: https://www.instagram.com/emi_techlogic/
💼 LinkedIn: https://www.linkedin.com/in/emmimal-alexander/
#PythonQuiz #CodingInterview #PythonInterviewQuestions #MCQChallenge #PythonMCQ #EmitechLogic #UncoveredAI #PythonTips #AIwithEmi #PythonCoding #TechWithEmi
Python uses three logical operators: and, or, and not. These are used to combine or reverse conditions in if statements and other logical expressions.
You use the and operator in Python when both conditions must be true.
Logical operators work with True and False.
Bitwise operators like & and | work on the binary representation of numbers.
Yes, you can use in and not in with logical operators like and and or to check membership and combine multiple conditions in Python.
Python Official Documentation – Boolean Operations
https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
→ A detailed explanation of and, or, and not with Python’s official syntax and rules.
After debugging production systems that process millions of records daily and optimizing research pipelines that…
The landscape of Business Intelligence (BI) is undergoing a fundamental transformation, moving beyond its historical…
The convergence of artificial intelligence and robotics marks a turning point in human history. Machines…
The journey from simple perceptrons to systems that generate images and write code took 70…
In 1973, the British government asked physicist James Lighthill to review progress in artificial intelligence…
Expert systems came before neural networks. They worked by storing knowledge from human experts as…
This website uses cookies.