
Python Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
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:
Example
sum1 = 100 + 50 # 150 (100 + 50)
sum2 = sum1 + 250 # 400 (150 + 250)
sum3 = sum2 + sum2 # 800 (400 + 400)
Python Arithmetic Operators
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 |
Example
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)
Division in Python
Python has two division operators:
/- Division (returns a float)//- Floor division (returns an integer)
Example
Division always returns a float:
x = 12
y = 5
print(x / y)
Example
Floor division always returns an integer. It rounds DOWN to the nearest integer:
x = 12
y = 5
print(x // y)
Python Assignment Operators
Assignment Operators
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) |
The Walrus Operator
Python 3.8 introduced the :=operator, known as the "walrus operator". It assigns values to variables as part of a larger expression:
Example
count = len(numbers)
if count > 3:
print(f"List has {count} elements")
if (count := len(numbers)) > 3:
print(f"List has {count} elements")
Python Comparison Operators
Comparison Operators
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) |
Examples
Comparison operators return True or False based on the comparison:
Chaining Comparison Operators
Python allows you to chain comparison operators:
Python Logical Operators
Logical 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) |
Examples
Test if a number is greater than 0 and less than 10:
Test if a number is less than 5 or greater than 10:
Python Identity Operators
Identity Operators
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 |
Examples
Example
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
Example
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
Difference Between is and ==
- is - Checks if both variables point to the same object in memory
- == - Checks if the values of both variables are equal
Example
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)
print(x is y)
Python Membership Operators
Membership Operators
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 |
Examples
Example
Check if "banana" is present in a list:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
Example
Check if "pineapple" is NOT present in a list:
fruits = ["apple", "banana", "cherry"]
print("pineapple" not in fruits)
Membership in Strings
The membership operators also work with strings:
Example
text = "Hello World"
print("H" in text)
print("hello" in text)
print("z" not in text)
Python Bitwise Operators
Bitwise Operators
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 » |
Examples
Example
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.
Example
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.
Python Operator Precedence
Operator Precedence
Operator precedence describes the order in which operations are performed.
Example
Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:
print((6 + 3) - (6 + 3))
Precedence Order
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 » |
Left-to-Right Evaluation
If two operators have the same precedence, the expression is evaluated from left to right.
Example
Addition + and subtraction - has the same precedence, and therefore we evaluate the expression from left to right:
print(5 + 4 - 7 + 3)