Operators are the symbols that tell Python to do something with values: add them, compare them, check a condition, and so on. In total = price + tax, the + and = are operators, and price, tax and total are the operands they work on.
Python has seven groups of operators. This page gives you a quick, working example of each group, then covers operator precedence and the mistakes beginners make most often. Each group also has its own detailed lesson, linked below.
Types of Operators in Python
| Group | Operators | Used for |
|---|---|---|
| Arithmetic | + - * / // % ** |
Maths on numbers |
| Assignment | = += -= *= /= //= %= **= |
Storing and updating values in variables |
| Comparison | == != > < >= <= |
Comparing two values (result is True or False) |
| Logical | and or not |
Combining conditions |
| Bitwise | & | ^ ~ << >> |
Working on the binary bits of integers |
| Membership | in, not in |
Checking if a value is inside a list, string, etc. |
| Identity | is, is not |
Checking if two names point to the same object |
1. Arithmetic Operators
These work the way you expect from maths, with two extras: // (floor division, which drops the decimal part) and ** (power).
a = 17
b = 5
print(a + b) # addition
print(a - b) # subtraction
print(a * b) # multiplication
print(a / b) # division, always gives a float
print(a // b) # floor division
print(a % b) # remainder (modulus)
print(a ** 2) # power
Output:
22
12
85
3.4
3
2
289
Read more: Python arithmetic operators
2. Assignment Operators
= stores a value in a variable. The shortcut forms such as += update the variable using its current value, so score += 5 means score = score + 5.
score = 10
score += 5
print(score)
score *= 2
print(score)
score //= 4
print(score)
Output:
15
30
7
Read more: Python assignment operators
3. Comparison Operators
A comparison always gives a Boolean: True or False. That is why comparisons are used inside if statements and loops.
x = 10
y = 20
print(x == y)
print(x != y)
print(x < y)
print(x >= 10)
Output:
False
True
True
True
Read more: Python comparison operators
4. Logical Operators
and is True only when both sides are True, or is True when at least one side is True, and not flips the result.
age = 19
has_id = True
print(age >= 18 and has_id)
print(age < 13 or age > 60)
print(not has_id)
Output:
True
False
False
Read more: Python logical operators
5. Bitwise Operators
Bitwise operators work on the binary form of whole numbers. Here 6 is 110 in binary and 3 is 011.
p = 6
q = 3
print(p & q) # AND
print(p | q) # OR
print(p ^ q) # XOR
print(~p) # NOT
print(p << 1) # left shift
print(p >> 1) # right shift
Output:
2
7
5
-7
12
3
Read more: Python bitwise operators
6. Membership Operators
in checks whether a value is present in a sequence such as a list, tuple, string or dictionary (for a dictionary it checks the keys).
fruits = ["apple", "mango", "banana"]
print("mango" in fruits)
print("grapes" not in fruits)
print("py" in "python")
Output:
True
True
True
Read more: Membership operators in Python
7. Identity Operators
is checks whether two variables point to the same object in memory, which is different from having the same value. Use == to compare values and is mainly to check for None.
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print(list1 == list2)
print(list1 is list2)
print(list1 is list3)
Output:
True
False
True
list1 and list2 hold equal values but are two separate lists, so is gives False. list3 is just another name for list1, so is gives True.
Read more: Identity operators in Python
Operator Precedence in Python
When an expression has more than one operator, Python decides the order using precedence. Higher rows in this table run first:
| Order | Operators |
|---|---|
| 1 (highest) | () brackets |
| 2 | ** |
| 3 | +x -x ~x (unary) |
| 4 | * / // % |
| 5 | + - |
| 6 | << >> |
| 7 | & |
| 8 | ^ |
| 9 | | |
| 10 | Comparisons, in, not in, is, is not |
| 11 | not |
| 12 | and |
| 13 (lowest) | or |
print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 ** 2)
print(-3 ** 2)
print(10 - 4 - 3)
Output:
14
20
512
-9
3
Two of these surprise most students. 2 ** 3 ** 2 is worked out from the right, as 2 ** 9, so the answer is 512, not 64. And -3 ** 2 is -9 because the power is applied before the minus sign. Write (-3) ** 2 if you want 9. When in doubt, add brackets; they cost nothing and make your code easier to read.
Common Mistakes with Python Operators
- Using
=instead of==.=stores a value,==compares. Writingif marks = 90:is a syntax error. - Expecting
/to give a whole number.7 / 2is3.5. Use7 // 2for3. Careful with negatives:-7 // 2is-4, because floor division always rounds down. - Using
isto compare values.ischecks identity, not equality. Use==for numbers and strings, and keepisforNone, as inif value is None:. - Forgetting that
and/orreturn a value, not always True or False.0 or "default"gives"default", and3 and 5gives5. This is useful for default values, but it can confuse you while debugging.
Practice Questions
Work these out on paper first, then run them to check.
print(15 % 4 + 2 ** 2)print(not (5 > 3 and 2 > 8))print(10 // 3 * 3 + 10 % 3)
Answers: 1) 7, because 15 % 4 is 3 and 2 ** 2 is 4. 2) True, because the bracket is False and not flips it. 3) 10, because 3 * 3 + 1 is 10. This works for any two positive whole numbers a and b: (a // b) * b + a % b == a.
Frequently Asked Questions
How many types of operators are there in Python?
Seven: arithmetic, assignment, comparison, logical, bitwise, membership and identity.
What is the difference between / and // in Python?
/ is true division and always returns a float (7 / 2 is 3.5). // is floor division and rounds down to the nearest whole number (7 // 2 is 3).
What is the difference between == and is?
== checks whether two values are equal. is checks whether two variables refer to the very same object in memory.
Does Python have ++ and — operators?
No. Python has no increment or decrement operator. Use count += 1 or count -= 1 instead.
Which operator has the highest precedence in Python?
Brackets come first, then the power operator **. Among the logical operators, not runs first, then and, then or.