In Python, operators are symbols or keywords that allow you to perform various operations on variables and values. There are several types of operators in Python, each with its own specific functionality. In this article, we’ll cover everything you need to know about Python operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numeric values. The following table lists the arithmetic operators in Python:
| Operator | Description |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division (returns a float) |
| // | Integer Division (returns an integer) |
| % | Modulo (returns the remainder after division) |
| ** | Exponentiation |
Here’s an example that uses arithmetic operators:
x = 10 y = 3 print(x + y) # Output: 13 print(x - y) # Output: 7 print(x * y) # Output: 30 print(x / y) # Output: 3.3333333333333335 print(x // y) # Output: 3 print(x % y) # Output: 1 print(x ** y) # Output: 1000
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True or False) based on the comparison. The following table lists the comparison operators in Python:
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Here’s an example that uses comparison operators:
x = 10 y = 5 print(x == y) # Output: False print(x != y) # Output: True print(x > y) # Output: True print(x < y) # Output: False print(x >= y) # Output: True print(x <= y) # Output: False
Logical Operators
Logical operators are used to combine multiple Boolean expressions and return a Boolean value based on the result. Short Circuiting: a and b stops if a is False. a or b stops if a is True. The following table lists the logical operators in Python:
| Operator | Description |
|---|---|
| and | Returns True if both expressions are True |
| or | Returns True if either expression is True |
| not | Returns True if the expression is False |
Here’s an example that uses logical operators:
x = 5 y = 10 z = 15 print(x < y and y < z) # Output: True print(x < y or y > z) # Output: True print(not(x < y)) # Output: False
Assignment Operators
Assignment operators are used to assign a value to a variable. The following table lists the assignment operators in Python:
| Operator | Description |
|---|---|
| = | Assigns a value to a variable |
| += | Adds a value to a variable and assigns the result |
| -= | Subtracts a value from a variable and assigns the result |
| *= | Multiplies a variable by a value and assigns the result |
| /= | Divides a variable by a value and assigns the result |
| //= | Performs integer division on a variable and assigns the result |
| %= | Computes the modulus of a variable and assigns the result |
| **= | Raises a variable to a power and assigns the result |
| := | Walrus operator , Evaluates the expression, assigns it to the variable, and returns the value at the same time. |
Unary Operator
Unary operators in Python are operators that work on a single operand. Unlike binary operators (which need two values, like x + y), unary operators only require one value to perform their operation.
| Operator | Meaning | Example | Output |
|---|---|---|---|
+x | Unary plus (returns the value unchanged) | x = 5; print(+x) | 5 |
-x | Unary minus (negates the value, changes sign) | x = 5; print(-x) | -5 |
~x | Bitwise NOT (inverts all bits, equivalent to -(x+1)) | x = 5; print(~x) | -6 |
not x | Logical NOT (returns True if operand is False, and vice versa) | flag = True; print(not flag) | False |
x = 5 print(+x) # 5 x = 5 print(-x) # -5 x = 5 # binary: 0101 print(~x) # -6 flag = True print(not flag) # False
Identity Operator
identity operators are special operators used to check if two values share the same memory location.
| Operator | Description |
|---|---|
| is | returns True if both objects share same memory reference |
| is not | return True if both object does not share same memory reference |
list1 = [1, 2] list2 = [1, 2] print(list1 == list2) # True (values equal) print(list1 is list2) # False (different objects) print(list1 is not list2) # True (different objects)
Membership Operators
This operator check if a value exists in a sequence.
| Operator | Description |
|---|---|
| in | returns True if value/variable is present in the sequence |
| not in | return True if value/variable is not present in the sequence |
nums = [1, 2, 3] print(2 in nums) # True print(5 not in nums) # True
Bitwise Operators
Bitwise operators are used to perform operations on the binary representation of numbers. The following table lists the bitwise operators in Python:
| Operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Bitwise Left Shift |
| >> | Bitwise Right Shift |
Here’s an example that uses bitwise operators:
# Bitwise Operators in Python
a = 10 # binary: 1010
b = 5 # binary: 0101
# AND (&) - bits set in both operands
print("a & b =", a & b) # 0 (1010 & 0101 = 0000)
# OR (|) - bits set in either operand
print("a | b =", a | b) # 15 (1010 | 0101 = 1111)
# XOR (^) - bits set in one operand but not both
print("a ^ b =", a ^ b) # 15 (1010 ^ 0101 = 1111)
# NOT (~) - inverts all bits (two's complement)
print("~a =", ~a) # -11 (inverts 1010 → ...11110101)
# Left Shift (<<) - shifts bits to the left, adds zeros on the right
print("a << 1 =", a << 1) # 20 (1010 << 1 = 10100)
# Right Shift (>>) - shifts bits to the right, discards bits on the right
print("a >> 1 =", a >> 1) # 5 (1010 >> 1 = 0101)
Operator Precedence
Operator precedence refers to the order in which operators are evaluated in an expression. Python follows a specific order of precedence for its operators, which is shown in the following table (from highest to lowest precedence):
| Precedence Level | Operators | Associativity |
|---|---|---|
| Highest | () (parentheses), x[index], x[index:index] (subscription, slicing) | Left to right |
await x | N/A | |
** (exponentiation) | Right to left | |
+x, -x, ~x (unary plus, unary minus, bitwise NOT) | Right to left | |
*, @, /, //, % (multiplication, matrix multiplication, division, floor division, modulus) | Left to right | |
+, - (addition, subtraction) | Left to right | |
<<, >> (bitwise shifts) | Left to right | |
& (bitwise AND) | Left to right | |
^ (bitwise XOR) | Left to right | |
| ` (bitwise OR) | ||
Comparisons: <, <=, >, >=, ==, !=, is, is not, in, not in | Left to right | |
not x (boolean NOT) | Right to left | |
and (boolean AND) | Left to right | |
or (boolean OR) | Left to right | |
if ... else ... (conditional expression) | Right to left | |
lambda (lambda expression) | N/A | |
:= (walrus operator, assignment expression) | Right to left | |
| Lowest | = and augmented assignments (+=, -=, etc.) | Right to left |
When an expression contains multiple operators with different precedence levels, Python evaluates them in the order specified by the table above. Parentheses can be used to group expressions and override the default precedence order.
Operators are an essential part of any programming language, and Python is no exception. By understanding the different types of operators available in Python and how they are used, you can write more complex and efficient code. Make sure to keep operator precedence in mind when writing expressions with multiple operators to ensure that your code executes as expected.
The codes mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information.







