
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
print(10 + 5)
Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or two variables:
sum1 = 100 + 50 # 150 (100 + 50)
sum2 = sum1 + 250 # 400 (150 + 250)
sum3 = sum2 + sum2 # 800 (400 + 400)
Arithmetic operators are used with numeric values to perform common mathematical operations:
| Operator | Name | Example | |
|---|---|---|---|
| + | Addition | x + y | |
| - | Subtraction | x - y | |
| * | Multiplication | x * y | |
| / | Division | x / y | |
| % | Modulus | x % y | |
| ** | Exponentiation | x ** y | |
| // | Floor division | x // y |
Here is an example using different arithmetic operators:
x = 15
y = 4
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x ** y)
print(x // y)
Python has two division operators:
/ - Division (returns a float)// - Floor division (returns an integer)Division always returns a float:
x = 12
y = 5
print(x / y)
Floor division always returns an integer. It rounds DOWN to the nearest integer:
x = 12
y = 5
print(x // y)
Assignment operators are used to assign values to variables:
| Operator | Example | Same As | Try it |
|---|---|---|---|
| = | x = 5 | x = 5 | |
| += | 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 **= 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 | |
| := | print(x := 3) | x = 3 print(x) |
Python 3.8 introduced the :=operator, known as the "walrus operator". It assigns values to variables as part of a larger expression:
Comparison operators are used to compare two values:
| Operator | Example | Try it |
|---|---|---|
| = | x = 5 | |
| += | x += 3 | |
| -= | x -= 3 | |
| *= | x *= 3 | |
| /= | x /= 3 | |
| %= | x %= 3 | |
| //= | x //= 3 | |
| **= | x **= 3 | |
| &= | x &= 3 | |
| |= | x |= 3 | |
| ^= | x ^= 3 | |
| >>= | x >>= 3 | |
| <<= | x <<= 3 | |
| := | print(x := 3) |
Comparison operators return True or False based on the comparison:
Python allows you to chain comparison operators:
Logical operators are used to combine conditional statements:
| Operator | Description | Example | Try it |
|---|---|---|---|
| and | Returns True if both statements are true | x < 5 and x < 10 | |
| or | Returns True if one of the statements is true | x < 5 or x < 4 | |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Test if a number is greater than 0 and less than 10:
Test if a number is less than 5 or greater than 10:
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
| Operator | Description | Example | Try it |
|---|---|---|---|
| is | Returns True if both variables are the same object | x is y | |
| is not | Returns True if both variables are not the same object | x is not y |
The is operator returns True if both variables point to the same object:
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
# returns True because z is the same object as x
print(x is y)
# returns False because x is not the same object as y, even if they have the same content
print(x == y)
# to demonstrate the difference between "is" and "==": this comparison returns True because x is equal to y
The is not operator returns True if both variables do not point to the same object:
x = ["apple", "banana"]
y = ["apple", "banana"]
print(x is not y)
# returns True because x is not the same object as y, even if they have the same content
is and ==x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)
print(x is y)
Membership operators are used to test if a sequence is presented in an object:
| Operator | Description | Example | Try it |
|---|---|---|---|
| in | Returns True if a sequence with the specified value is present in the object | x in y | |
| not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
Check if "banana" is present in a list:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
Check if "pineapple" is NOT present in a list:
fruits = ["apple", "banana", "cherry"]
print("pineapple" not in fruits)
The membership operators also work with strings:
text = "Hello World"
print("H" in text)
print("hello" in text)
print("z" not in text)
Bitwise operators are used to compare (binary) numbers:
| Operator | Name | Description | Example | Try it |
|---|---|---|---|---|
| & | AND | Sets each bit to 1 if both bits are 1 | x & y | Try it » |
| | | OR | Sets each bit to 1 if one of two bits is 1 | x | y | Try it » |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y | Try it » |
| ~ | NOT | Inverts all the bits | ~x | Try it » |
The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0:
print(6 & 3)
The binary representation of 6 is 0110
The binary representation of 3 is 0011
Then the & operator compares the bits and returns 0010, which is 2 in decimal.
The | operator compares each bit and set it to 1 if one or both is 1, otherwise it is set to 0:
print(6 | 3)
The binary representation of 6 is 0110
The binary representation of 3 is 0011
Then the | operator compares the bits and returns 0111, which is 7 in decimal.
Operator precedence describes the order in which operations are performed.
Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:
print((6 + 3) - (6 + 3))
The precedence order is described in the table below, starting with the highest precedence at the top:
| Operator | Description | Try it |
|---|---|---|
| () | Parentheses | Try it » |
| ** | Exponentiation | Try it » |
| +x -x ~x | Unary plus, unary minus, and bitwise NOT | Try it » |
| * / // % | Multiplication, division, floor division, and modulus | Try it » |
| + - | Addition and subtraction | Try it » |
| << >> | Bitwise left and right shifts | Try it » |
| & | Bitwise AND | Try it » |
| ^ | Bitwise XOR | Try it » |
| | | Bitwise OR | Try it » |
If two operators have the same precedence, the expression is evaluated from left to right.
Addition + and subtraction - has the same precedence, and therefore we evaluate the expression from left to right:
print(5 + 4 - 7 + 3)