Operators are symbols that perform actions on values. In 5 + 3, the + is the operator, and 5 and 3 are the operands. This lesson covers every group of Python operators with examples and output, and ends with the order in which Python evaluates them.
Try every example in our online Python compiler.
Arithmetic operators
Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 17 + 5 | 22 |
– | Subtraction | 17 – 5 | 12 |
* | Multiplication | 17 * 5 | 85 |
/ | Division (always a float) | 17 / 5 | 3.4 |
// | Floor division | 17 // 5 | 3 |
% | Modulus (remainder) | 17 % 5 | 2 |
** | Power | 17 ** 2 | 289 |
a, b = 17, 5
print(a + b, a - b, a * b, a / b, a // b, a % b, a ** 2)
print(-17 // 5, -17 % 5, 2 ** 10, 7 / 2, 8 / 2)Output:
22 12 85 3.4 3 2 289
-4 3 1024 3.5 4.0Notice that / always gives a float (8 / 2 is 4.0). Floor division // rounds down to the nearest whole number, so -17 // 5 is -4, not -3. The remainder with % follows the same rule, so -17 % 5 is 3.
The divmod() function gives the quotient and the remainder together:
print(divmod(17, 5))
(3, 2)Comparison operators
Comparison operators compare two values and give True or False.
Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
a, b = 17, 5
print(a == b, a != b, a > b, a < b, a >= 17, a <= 5)
print(1 < 2 < 3, 1 < 2 > 3)Output:
False True True False True False
True FalsePython lets you chain comparisons, so 1 < 2 < 3 means 1 < 2 and 2 < 3.
Logical operators
Operator | Meaning |
|---|---|
and | True if both conditions are True |
or | True if at least one condition is True |
not | Reverses the result |
print(True and False, True or False, not True)
print(5 > 3 and 2 > 1, 5 > 3 and 2 > 4, 5 < 3 or 2 > 1)Output:
False True False
True False TrueAssignment operators
The operator = stores a value. Combined operators such as += update a variable in one step.
x = 10
x += 5
print(x)
x -= 3
print(x)
x *= 2
print(x)
x //= 5
print(x)
x **= 2
print(x)
x %= 7
print(x)Output:
15
12
24
4
16
2x += 5 means x = x + 5.
Membership operators
in and not in check whether a value is present in a string, list, tuple, set or dictionary (keys).
print('a' in 'cat', 3 in [1, 2, 3], 'z' not in 'cat')Output:
True True True
Identity operators
is checks whether two names refer to the same object in memory. == checks whether the values are equal.
l1 = [1, 2]
l2 = [1, 2]
l3 = l1
print(l1 == l2, l1 is l2, l1 is l3)Output:
True False Truel1 and l2 have the same values, but they are two different lists. l3 is another name for l1.
Bitwise operators
Bitwise operators work on the binary digits of integers.
Operator | Name |
|---|---|
& | AND |
| | OR |
^ | XOR |
~ | NOT |
<< | Left shift |
>> | Right shift |
print(5 & 3, 5 | 3, 5 ^ 3, ~5, 5 << 1, 5 >> 1)Output:
1 7 6 -6 10 25 is 101 in binary and 3 is 011, so 5 & 3 is 001, which is 1.
Operator precedence
When an expression has several operators, Python evaluates them in a fixed order. From highest to lowest:
- Brackets ( )
- Power **
- Multiplication, division, floor division and modulus (* / // %)
- Addition and subtraction (+ -)
- Comparisons
- not, then and, then or
print(2 + 3 * 4, (2 + 3) * 4)
print(2 ** 3 ** 2, -2 ** 2)
print(10 - 4 - 3, 100 / 10 / 5)Output:
14 20
512 -4
3 2.0Power is evaluated from right to left, so 2 ** 3 ** 2 is 2 ** 9, which is 512. When in doubt, use brackets to make the order clear.
Common mistakes
- Using = instead of ==. x = 5 stores a value, while x == 5 compares.
- Expecting // to round towards zero. It rounds down, so negative results differ.
- Comparing decimals with ==. Decimal numbers can have tiny errors.
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(round(0.1 + 0.2, 2) == 0.3)
0.30000000000000004
False
True- Using is to compare values. Use == for values, and is only for identity checks such as x is None.
Practice questions
- What is the value of 7 // 2, 7 % 2 and 7 / 2?
- What is the value of 2 + 3 * 4 ** 2?
- Check whether a number is even using the % operator.
- Write a condition that is True when a number is between 10 and 20.
- What is the difference between == and is?
Summary
- Arithmetic operators: + – * / // % **
- Comparison operators give True or False, and Python allows chaining.
- Logical operators are and, or and not.
- Use ==, not is, to compare values. Use brackets when the order is not obvious.
