Skip to content
beginner

Logical Operators in Python

You already know how to make a single decision with an if statement. Real programs rarely stop there. A login form checks that a user exists and that the…

Published 2026-05-11Updated 2026-09-158 min read
Library interior with bookshelves and armchairs. Ideal for education and reading themes.
Library interior with bookshelves and armchairs. Ideal for education and reading themes. Photo by Zetong Li on Pexels.

You already know how to make a single decision with an if statement. Real programs rarely stop there. A login form checks that a user exists and that the password matches. A game lets you play when you are old enough or when a parent approves. Python gives you three small words—and, or, and not—to combine those conditions into one decision.

In this article, you'll learn what each logical operator does, how Python actually evaluates a combined condition, and where beginners usually trip up.

What Are Logical Operators?

Comparison table showing that and is true only when both conditions are true, or is true when at least one condition is true, and not reverses a condition’s truth value.
Use this quick reference to predict whether a combined Python condition evaluates to True or False.

Logical operators are Python keywords that combine conditions. In an if statement, the whole expression is interpreted as True or False, and that decides which branch runs.

Python has three logical operators:

  • and — true only when both conditions are true
  • or — true when at least one condition is true
  • not — flips a condition to its opposite

You will use these almost every time you write an if statement that depends on more than one thing. If you need a refresher on the basics of decision making first, review how if, elif, and else choose a branch.

The and Operator

and is strict. It returns True only when every condition on both sides is true. One false condition makes the whole expression false.

age = 20
has_ticket = True

if age >= 18 and has_ticket:
    print("You can enter the concert.")
else:
    print("Sorry, you can't enter.")
You can enter the concert.

Change has_ticket to False and the message flips to the else branch. and needs all the green lights before you move.

Knowledge check

Check your understanding

Answer this question before you continue.

A concert program uses `if age >= 18 and has_ticket:`. When will the `if` branch run?
Single Choice

Focus: Determine when an `and` condition evaluates to true.

The or Operator

or is forgiving. It returns True when at least one condition is true, even if the other is false. It only returns False when every condition is false.

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.")
You can play video games!

Here is_weekend is false, but finished_homework is true, so the whole condition passes. With or, one green light is enough.

Knowledge check

Check your understanding

Answer this question before you continue.

What does this code print? `is_weekend = False` `finished_homework = True` `if is_weekend or finished_homework:` ` print("You can play video games!")` `else:` ` print("No games")`
Output Prediction

Focus: Predict the result of an `or` condition when exactly one condition is true.

The not Operator

not is the simplest of the three: it flips a condition. True becomes False, and False becomes True.

is_raining = False

if not is_raining:
    print("Let's go outside!")
else:
    print("Better stay indoors.")
Let's go outside!

Because is_raining is False, not is_raining evaluates to True, and the outside message prints. not is your way of saying "if this is not the case."

Knowledge check

Check your understanding

Answer this question before you continue.

If `is_raining = False`, which condition is true and allows the outside branch to run?
Single Choice

Focus: Use `not` to express the opposite of a condition.

How Python Evaluates a Combined Condition

Here is the part most tutorials skip, and it is the part that will save you from confusion later.

Python reads a combined condition from left to right and stops as soon as it knows the answer. This is called short-circuiting. With and, the moment one condition is false, Python stops—the rest cannot change the result. With or, the moment one condition is true, Python stops—the rest cannot change the result.

You can see this happen with a tiny experiment:

def check(label):
    print(f"checking {label}")
    return True

if check("first") or check("second"):
    print("done")
checking first
done

Because the first condition is already true, Python never calls check("second"). The or is satisfied, so the right side never runs. That is not a quirk—it is how Python saves work and why you can safely guard an operation on the right side of and.

Common Mistake: Mixing Up and and or

Beginners often reach for or when they mean and. The rule to remember: and demands everything, or accepts anyone. If you want a user to be logged in and have permission, and is correct. If you want to let in either an admin or a moderator, or is correct.

Common Mistake: Skipping Parentheses

Short-circuiting answers how far Python evaluates. A separate question is how the expression is grouped in the first place. That grouping is decided by operator precedence before any evaluation happens.

When you mix and with or, Python applies a fixed order: not binds tightest, then and, then or. That means a or b and c is read as a or (b and c), not as (a or b) and c. The two groupings are not the same, and you can prove it by choosing values where they disagree.

Say the rule is: a user may enter if they are an admin or a moderator, and they are not banned. Write that policy with parentheses first:

is_admin = False
is_moderator = True
is_banned = True

## Intended rule: (is_admin or is_moderator) and not is_banned
if (is_admin or is_moderator) and not is_banned:
    print("Access granted.")
else:
    print("Access denied.")
Access denied.

Now drop the parentheses and let Python apply its default grouping, which reads the expression as is_admin or (is_moderator and not is_banned):

is_admin = False
is_moderator = True
is_banned = True

## Default grouping: is_admin or (is_moderator and not is_banned)
if is_admin or is_moderator and not is_banned:
    print("Access granted.")
else:
    print("Access denied.")
Access granted.

Same values, same operators, different output. The parentheses changed which branch ran. Without them, Python grouped the expression its own way and let the moderator through despite the ban. When in doubt, add the parentheses—they make the grouping you intend visible to both Python and the next person who reads your code.

Tip: Read Conditions Out Loud

A combined condition should make sense as an English sentence. Read (is_admin or is_moderator) and not is_banned as "is an admin or a moderator, and is not banned." If the sentence sounds wrong, the condition probably is too.

Knowledge check

Check your understanding

Answer this question before you continue.

What is printed by this code? `def check(label):` ` print(f"checking {label}")` ` return True` `if check("first") or check("second"):` ` print("done")`
Output Prediction

Focus: Predict which parts of an `or` expression Python evaluates using short-circuiting.

When Not to Use Logical Operators

Logical operators combine conditions, but they are not the right tool for every comparison. Use comparison operators like ==, >, and < when you are testing a single relationship between values. Use and, or, and not when you need to join two or more of those comparisons into one decision.

Also, do not confuse logical operators with bitwise operators like & and |. Those work on the individual bits of numbers and are a different topic. For combining conditions, stick with and, or, and not.

There is a second boundary worth knowing: a condition that is hard to say aloud is a sign you should split it. If you need three ands and an or in one line, give the pieces names.

is_old_enough = age >= 18
has_guardian = parent_approved
is_allowed = is_old_enough or has_guardian

if is_allowed and not is_banned:
    print("Access granted.")

Named variables turn a wall of logic into a sentence you can read and debug. If each failure needs a different message, split the decision into separate if branches instead of one long condition.

Practice: Build Your Own Conditions

Try writing these three conditions yourself before you look at the answers. Type each one, run it, and predict the output first.

1. Ride the roller coaster. You can ride if you are at least 12 years old and taller than 140 cm.

2. Earn dessert. You get dessert if you finished your dinner or your parent says yes.

3. Decide to go outside. Go outside only if it is not raining.

Once you have attempted all three, compare your code with the solutions below.

Solution 1: Ride the roller coaster

age = 13
height = 145

if age >= 12 and height > 140:
    print("You can ride!")
else:
    print("Sorry, you can't ride.")
You can ride!

Solution 2: Earn dessert

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 can have dessert!

Solution 3: Decide to go outside

is_raining = True

if not is_raining:
    print("Go outside and play!")
else:
    print("Stay indoors.")
Stay indoors.

Now change the values and watch the output change. That is the fastest way to build a feel for how each operator behaves.

Next Steps

Your immediate next move: write a small program that asks for a username and password, and use and to check both before printing "Welcome." Then add a rule that rejects a banned user with not. That single exercise ties together everything in this article.

Keep this decision rule in your pocket: and demands everything, or accepts anyone, not flips the verdict, and parentheses make the grouping explicit. When you are ready to move on, see how conditions drive repetition in loops, or learn how to exit a loop partway through with break.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

A user should enter only when they are an admin or moderator and are not banned. Which expression explicitly matches that rule?
Question 1 of 2Misconception Check

Focus: Recognize why parentheses are needed to express the intended grouping of mixed `and` and `or` conditions.

Which summary correctly matches the three logical operators taught in the article?
Question 2 of 2Single Choice

Focus: Select the logical operator that matches a stated condition rule.

References

  1. operator — Standard operators as functions — Python 3.14.7 documentationdocs.python.org
Practical resource

Want a more structured Python path?

Use the Python Starter Pack to turn scattered tutorials into a focused practice path.

View the bundle
Coming soon

Python Starter Pack

A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.

$9
PDF BundleTopic PackPythonBeginner
  • 5 curated chapters
  • Enhanced PDF edition with bundle-only learning guidance
  • Offline-friendly format for focused review
  • Source article links for future online updates

Coming soon

Free Python bundle

Get the LearnPyFast Python for Artificial Intelligence Starter Bundle

Build a Python foundation you can actually use. The Python for Artificial Intelligence Starter Pack brings together a guided path through setup, core programming concepts, data structures, files, JSON, APIs, debugging, and practical projects—so you can move quickly from running your first program to understanding and building useful software.

You’ll receive the bundle by email. You can unsubscribe anytime.

No spam. You can unsubscribe anytime. See our Privacy policy.

Related sites

Continue beyond Python

Explore related Worldmonger sites when you want to move from Python basics into JavaScript or LLM application building.

JavaScript tutorialstutorial

LearnJSFast

Beginner-friendly JavaScript tutorials for practical web development and self-taught developers.

JavaScriptFrontendWeb development
Visit LearnJSFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

High-tech laboratory equipment with computer system in lab setting.
beginner
7 min read

For Loops in Python

A for loop is how Python repeats an action for every item in a collection. You describe the action once, and Python runs it for each value in turn. That…

Read tutorial
Two programmers working together with focus on coding in a modern, tech-savvy office environment.
beginner
8 min read

If Statements in Python

An if statement is how a Python program turns a condition into a decision: check something, then run one block of code or skip it. The whole skill is…

Read tutorial