Operators
Master Python's arithmetic, comparison, logical, assignment, identity, and membership operators, along with operator precedence.
Introduction
Operators are the symbols that let you actually do something with your values — add two numbers, compare two scores, check if a condition is true, or see if an item exists in a list. Python groups operators into several families, each suited to a different kind of task.
In this lesson, you will work through every major operator family in Python: arithmetic, comparison, logical, assignment, identity, and membership, and finish with a look at how Python decides which operator runs first when several appear in one expression.
- How to perform arithmetic, including the difference between / and //.
- How to compare values and combine conditions with and, or, and not.
- How Python's logical operators short-circuit and return operand values.
- The difference between is and == for comparing values.
- How to check membership in a collection using in and not in.
- How operator precedence determines evaluation order.
What is an Operator?
An operator is a special symbol that performs an operation on one or more values, called operands. In 5 + 3, the + is the operator, and 5 and 3 are the operands.
Arithmetic
Perform math: addition, subtraction, multiplication, division, and more.
Comparison
Compare two values and get back True or False.
Logical
Combine multiple conditions using and, or, and not.
Assignment
Assign or update the value stored in a variable.
Identity
Check whether two variables point to the exact same object in memory.
Membership
Check whether a value exists inside a collection.
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | True Division | 5 / 2 | 2.5 |
| // | Floor Division | 5 // 2 | 2 |
| % | Modulus (remainder) | 5 % 2 | 1 |
| ** | Exponent (power) | 5 ** 2 | 25 |
a = 17
b = 4
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)21
13
68
4.25
4
1
83521True Division vs Floor Division
Python has two distinct division operators, and mixing them up is one of the most common beginner errors.
/ True Division
Always returns a float, even if the numbers divide evenly.
print(10 / 2)
# 5.0 — still a float// Floor Division
Divides and rounds down to the nearest whole number.
print(10 // 3)
# 3 — rounded downprint(10 / 2)
print(10 // 2)
print(10 / 3)
print(10 // 3)5.0
5
3.3333333333333335
3Comparison Operators
Comparison operators compare two values and always produce a bool result: True or False.
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | 5 == 5 → True |
| != | Not equal to | 5 != 3 → True |
| > | Greater than | 5 > 3 → True |
| < | Less than | 5 < 3 → False |
| >= | Greater than or equal to | 5 >= 5 → True |
| <= | Less than or equal to | 4 <= 3 → False |
score = 82
print(score == 82)
print(score != 100)
print(score >= 90)True
True
FalseLogical Operators
Logical operators combine multiple conditions. Python has three: and, or, and not.
and
True only if both conditions are true.
or
True if at least one condition is true.
not
Flips a condition — True becomes False and vice versa.
age = 20
has_id = True
print(age >= 18 and has_id)
print(age < 18 or has_id)
print(not has_id)True
True
FalseShort-Circuit Evaluation
A subtlety that surprises many beginners: and and or in Python do not always return True or False. They return one of the actual operand values, and they stop evaluating as soon as the overall result is already decided — this is called short-circuiting.
print(0 and 5)
print(3 and 5)
print("" and "hello")0
5
print(0 or 5)
print(3 or 5)
print("" or "hello")5
3
helloand stops as soon as it finds a falsy value (no need to check further — the result is already false). or stops as soon as it finds a truthy value. This is often used to provide default values, such as name = user_input or "Guest".
Assignment Operators
Assignment operators assign a value to a variable, and the compound forms combine an operation with assignment in one step.
| Operator | Example | Equivalent To |
|---|---|---|
| = | x = 5 | Assign 5 to x |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| //= | x //= 3 | x = x // 3 |
| **= | x **= 3 | x = x ** 3 |
x = 10
x += 5
print(x)
x -= 3
print(x)
x *= 2
print(x)15
12
24Identity Operators
is and is not check whether two variables refer to the exact same object in memory — not just whether their values look equal. This is a different question from ==, which only checks value equality.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b)
print(a is b)
print(a is c)True
False
Truea == b is True because their contents are equal. a is b is False because they are two separate list objects in memory, even though they look the same. c is a is True because c was assigned to point at the exact same object as a.
Membership Operators
in and not in check whether a value exists inside a collection, such as a string, list, or tuple.
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("mango" in fruits)
print("mango" not in fruits)
print("a" in "banana")True
False
True
TrueOperator Precedence
When an expression contains several operators, Python evaluates them in a specific order, called precedence — similar to the order of operations you learned in math class. Operators higher in the table run before operators lower in the table.
| Precedence (high to low) | Operators |
|---|---|
| 1. Parentheses | () |
| 2. Exponent | ** |
| 3. Unary sign | +x, -x |
| 4. Multiplication/Division | *, /, //, % |
| 5. Addition/Subtraction | +, - |
| 6. Comparison | ==, !=, >, <, >=, <= |
| 7. Logical NOT | not |
| 8. Logical AND | and |
| 9. Logical OR | or |
result = 2 + 3 * 4
print(result)
result2 = (2 + 3) * 4
print(result2)14
20You do not need to memorize the entire precedence table. Wrapping expressions in parentheses makes your intent explicit and your code easier to read, even when it is not strictly required.
Common Mistakes
- Using / when you actually need whole-number floor division //.
- Confusing == (equality) with = (assignment).
- Using is to compare values instead of ==, especially for numbers or strings.
- Forgetting that and/or return operand values, not always True or False.
- Assuming operators run strictly left to right without considering precedence.
Best Practices
- Use == to compare values; reserve is for checking against None or identical objects.
- Add parentheses to clarify precedence, even when not strictly necessary.
- Prefer // when you specifically need a whole-number result from division.
- Use in / not in instead of manual loops to check membership.
- Take advantage of short-circuiting for concise default-value patterns.
Frequently Asked Questions
What is the difference between / and //?
/ always performs true division and returns a float. // performs floor division and rounds the result down to the nearest whole number.
Why does 10 // 3 equal 3 and not 3.33?
Floor division discards the remainder and rounds down to the nearest whole number, giving 3.
What is the difference between == and is?
== compares whether two values are equal. is compares whether two variables refer to the exact same object in memory.
Do and and or always return True or False?
No. They return one of the actual operand values based on short-circuit evaluation, which is often True or False, but not always.
Do I need to memorize operator precedence?
Not fully — using parentheses to make your intent explicit is usually clearer and safer than relying on memorized precedence rules.
Key Takeaways
- Arithmetic operators include + - * / // % **, with / for true division and // for floor division.
- Comparison operators always produce a bool result.
- and, or, and not combine conditions and use short-circuit evaluation, returning operand values.
- Assignment operators like += and -= combine an operation with assignment.
- is checks object identity, while == checks value equality.
- in and not in test membership inside collections.
- Operator precedence determines evaluation order — parentheses make intent explicit.
Summary
Operators are the tools you use to calculate, compare, combine conditions, and test relationships between values. You now know Python's full operator toolkit — arithmetic, comparison, logical, assignment, identity, and membership — along with how precedence determines the order expressions are evaluated.
Next, you will learn how to interact with the user directly through input() and control exactly how your programs display output with print().
- You can perform arithmetic and understand / versus //.
- You can combine conditions using and, or, and not.
- You understand the difference between == and is.
- You are ready to learn about user input and output.