An operator in Python is a symbol or keyword, such as +, ==, and or in, that combines or tests values and produces a result. Python groups them into arithmetic, comparison, assignment, logical, bitwise, identity and membership operators.
The values an operator works on are its operands. In 3 + 4, the operator is + and the operands are 3 and 4. All the snippets below can be pasted into a Python prompt. Lines starting with >>> show example output.
All Python operators at a glance
| Type | Operators | Example |
|---|---|---|
| Arithmetic | + - * / // % ** | 7 // 2 gives 3 |
| Comparison | == != < <= > >= | 3 < 5 gives True |
| Assignment | = += -= *= /= //= %= **= and the bitwise forms | x += 1 |
| Logical | and or not | not True gives False |
| Bitwise | & | ^ ~ << >> | 5 & 3 gives 1 |
| Identity | is, is not | x is None |
| Membership | in, not in | "gg" in "eggs" gives True |
Arithmetic operators
>>> 7 + 2, 7 - 2, 7 * 2
(9, 5, 14)
>>> 7 / 2
3.5
>>> 7 // 2
3
>>> 7 % 2
1
>>> 2 ** 10
1024
Three of these trip people up:
/always gives a float. Use//when you want a whole-number quotient.//rounds toward minus infinity, not toward zero. The docs list(-1)//2as-1and1//(-2)as-1. So-7 // 2is-4.%takes the sign of the second operand. The reference says the result is "the same sign as its second operand (or zero)", and thatx == (x//y)*y + (x%y)always holds.
>>> -7 // 2, -7 % 2
(-4, 1)
>>> 7 // -2, 7 % -2
(-4, -1)
>>> divmod(-7, 2)
(-4, 1)
divmod(x, y) returns the pair (x // y, x % y) in one call. Several arithmetic operators also work on non-numbers: "ab" + "cd" joins strings and [0] * 3 repeats a list into [0, 0, 0]. To insert computed values into text, see Python f-strings.
Python has no ++ or --. Write x += 1.
Comparison operators and chaining
Comparison operators return True or False. They can be chained: x < y <= z means x < y and y <= z, except that y is evaluated only once.
>>> age = 25
>>> 18 <= age < 65
True
A chain such as x < y > z is legal, and it implies no comparison between x and z.
Assignment and augmented assignment
A single = binds a name to a value. The augmented forms combine an operation with the assignment. The language reference lists them as +=, -=, *=, @=, /=, //=, %=, **=, >>=, <<=, &=, ^= and |=.
x += 1 is similar to x = x + 1 but not identical. The target is evaluated only once, and "when possible" the operation happens in place, so a mutable object such as a list is modified instead of replaced:
>>> a = [1]
>>> b = a
>>> a += [2]
>>> b
[1, 2]
>>> a = a + [3]
>>> b
[1, 2]
With +=, the list was extended in place, so b, which points at the same list, sees the change. With a = a + [3], a new list is built and only a is rebound.
Logical operators: and, or, not
They short-circuit. In x or y, y is evaluated only if x is false. In x and y, y is evaluated only if x is true.
The result is one of the operands, not necessarily a bool. x or y returns x if x is true, else y. x and y returns x if x is false, else y.
>>> name = ""
>>> name or "anonymous"
'anonymous'
>>> "a" and "b"
'b'
>>> not "foo"
False
Only not always returns True or False. It also binds less tightly than comparison operators, so not a == b means not (a == b), while a == not b is a syntax error.
Bitwise operators
These work on the binary digits of integers. The docs say results are computed as if in two's complement with infinitely many sign bits.
>>> 5 & 3 # 101 and 011
1
>>> 5 | 3
7
>>> 5 ^ 3
6
>>> ~5
-6
>>> 1 << 3
8
>>> 16 >> 2
4
A left shift by n equals multiplying by pow(2, n). A right shift by n equals floor division by pow(2, n). A negative shift count raises ValueError. For true or false values, use and, or and not instead of &, | and ~. The docs note that applying ~ to a bool is deprecated since Python 3.12 and will raise an error in Python 3.16.
Identity operators: is vs ==
== asks whether two values are equal. is asks whether both sides are the same object, as reported by id(). is and is not cannot be customised and never raise exceptions.
>>> a = [1, 2]
>>> b = [1, 2]
>>> a == b
True
>>> a is b
False
Use is and is not for singletons such as None: if result is None:. Do not use is to compare numbers or strings. Whether two equal literals are the same object is an implementation detail of the interpreter.
Membership operators: in and not in
in and not in work on containers: lists, tuples, sets, dictionaries (which test keys), strings (which test substrings), and any class that implements __contains__(). They have the same priority as comparison operators.
>>> 3 in [1, 2, 3]
True
>>> "gg" in "eggs"
True
>>> "k" in {"k": 1}
True
>>> 4 not in (1, 2, 3)
True
Operator precedence in Python
When an expression mixes operators, higher-precedence ones bind first. From loosest to tightest, following the grammar in the language reference:
| Level (loosest first) | Operators |
|---|---|
| 1 | lambda |
| 2 | if / else conditional expression |
| 3 | or |
| 4 | and |
| 5 | not |
| 6 | comparisons, in, not in, is, is not |
| 7 | |, then ^, then & |
| 8 | <<, >> |
| 9 | +, - |
| 10 | *, @, /, //, % |
| 11 | unary +x, -x, ~x |
| 12 | ** |
| 13 | await, then calls, subscripts and attribute access |
Two consequences are worth remembering. and binds tighter than or, so a or b and c means a or (b and c). And ** binds tighter than a unary operator on its left but looser than one on its right, so -1 ** 2 is -(1 ** 2), which is -1, while 2 ** -1 is 0.5.
When in doubt, add parentheses. They cost nothing and show your intent.
Common mistakes
- Writing
=where==is meant.if x = 3:is a syntax error. Compare with==. - Expecting
/to give an integer.10 / 2is5.0, not5. Use//. - Using
isfor values.x is 1000may work on one build and fail on another. Use==. - Assuming
orreturns a bool.name or "anonymous"returns a string. That is useful as a default, but surprising if you expectedTrue. - Relying on
-7 // 2being-3. It is-4, because floor division rounds down.
Frequently asked questions
- What is the difference between / and // in Python?
- The / operator always does true division and returns a float, so 7 / 2 is 3.5. The // operator does floor division: it rounds the result toward minus infinity, so 7 // 2 is 3 and -7 // 2 is -4.
- What is the difference between == and is in Python?
- The == operator compares values and can be customised by a class. The is operator tests object identity: it is true only when both names point at the same object. Use == for values and is only for singletons such as None.
- Does Python have ++ and -- operators?
- No. Python has no increment or decrement operators. Use the augmented assignments x += 1 and x -= 1 instead.
- What does the % operator do in Python?
- It returns the remainder of a division, and the result takes the sign of the second operand (or is zero). So 7 % 2 is 1, while 7 % -2 is -1. It satisfies x == (x // y) * y + (x % y).
- Do and and or return True and False?
- Not necessarily. Both return one of their operands. x or y returns x if x is true, otherwise y. x and y returns x if x is false, otherwise y. Only not always returns a bool.