Whether you are building a simple calculator or training a complex machine learning model, you will constantly need to manipulate data. In Python, operators are the special symbols and keywords that allow you to perform these manipulations. They take pieces of data (called operands) and process them into a new result.
Let’s dive into the essential categories of Python operators that every programmer should master.
Table of Contents
What are Operators?
An operator in Python is a symbol or keyword that performs a specific operation on one or more operands. The values or variables on which operators work are called operands.Syntax:
operand1 operator operand2
Types of Operators in Python
Python provides the following categories of operators:
- Arithmetic Operators: Arithmetic operators are used to perform standard mathematical calculations.
- Assignment Operators: Assignment operators assign values to variables and can also perform operations while assigning the result.
- Comparison (Relational) Operators: Comparison operators compare two values and return either True or False based on the comparison.
- Logical Operators: Logical operators combine or negate Boolean expressions to evaluate logical conditions.
- Bitwise Operators: Bitwise operators perform operations directly on the binary representation of integer values.
- Membership Operators: Membership operators check whether a value exists within a sequence or collection.
- Identity Operators: Identity operators determine whether two variables refer to the same object in memory.
- Walrus Operator: The walrus operator (:=) assigns a value to a variable as part of an expression.
| Operator | Name | How It Works (Example) |
| + | Addition | Adds values (5 + 2 = 7) |
| - | Subtraction | Subtracts values (5 - 2 = 3) |
| * | Multiplication | Multiplies values (5 * 2 = 10) |
| / | Division | Divides values; always returns a float (5 / 2 = 2.5) |
| % | Modulus | Returns the remainder of a division (5 % 2 = 1) |
| // | Floor Division | Divides and rounds down to the nearest whole number (5 // 2 = 2) |
| ** | Exponentiation | Raises the first number to the power of the second (5 ** 2 = 25) |
Example:
# Arithmetic Operators Example
a = 15
b = 4
print("Addition (+):", a + b)
print("Subtraction (-):", a - b)
print("Multiplication (*):", a * b)
print("Division (/):", a / b)
print("Floor Division (//):", a // b)
print("Modulus (%):", a % b)
print("Exponentiation (**):", a ** b)
Output:
Explanation:Addition (+): 19
Subtraction (-): 11
Multiplication (*): 60
Division (/): 3.75
Floor Division (//): 3
Modulus (%): 3
Exponentiation (**): 50625
- + adds two numbers.
- - subtracts one number from another.
- * multiplies two numbers.
- / performs floating-point division.
- // returns the quotient without the decimal part.
- % returns the remainder after division.
- ** raises the first number to the power of the second number.
2. Assignment Operators: Used to assign values to variables, often combining math and assignment into one step.
| Operator | Name | How It Works (Example) |
| = | Assignment | Assigns a value to a variable (x = 5) |
| += | Add & Assign | Adds and assigns (x += 2 is equivalent to x = x + 2) |
| -= | Subtract & Assign | Subtracts and assigns (x -= 2 is equivalent to x = x - 2) |
| *= | Multiply & Assign | Multiplies and assigns (x *= 2 is equivalent to x = x * 2) |
| /= | Divide & Assign | Divides and assigns (x /= 2 is equivalent to x = x / 2) |
| %= | Modulus & Assign | Takes the modulus and assigns (x %= 2 is equivalent to x = x % 2) |
| //= | Floor Div & Assign | Floor divides and assigns (x //= 2 is equivalent to x = x // 2) |
| **= | Exponent & Assign | Raises to a power and assigns (x **= 2 is equivalent to x = x ** 2) |
Example:
# Assignment Operators Example
x = 10
print("Assignment (=):", x)
x += 5
print("After += :", x)
x -= 3
print("After -= :", x)
x *= 2
print("After *= :", x)
x /= 4
print("After /= :", x)
x //= 2
print("After //= :", x)
x %= 3
print("After %= :", x)
x **= 3
print("After **= :", x)
Output:
Explanation:Assignment (=): 10
After += : 15
After -= : 12
After *= : 24
After /= : 6.0
After //= : 3.0
After %= : 0.0
After **= : 0.0
- = assigns a value to a variable.
- += adds and assigns.
- -= subtracts and assigns.
- *= multiplies and assigns.
- /= divides and assigns.
- //= performs floor division and assigns.
- %= computes the remainder and assigns.
- **= performs exponentiation and assigns.
3. Comparison Operators: Comparison Operators are used to compare two values. They always evaluate to either True or False.
| Operator | Name | How It Works (Example) |
| == | Equal to | Returns True if values match (5 == 5 is True) |
| != | Not equal to | Returns True if values are different (5 != 3 is True) |
| > | Greater than | Returns True if the left value is bigger (5 > 3 is True) |
| < | Less than | Returns True if the left value is smaller (3 < 5 is True) |
| >= | Greater than or equal to | Returns True if the left is bigger or equal (5 >= 5 is True) |
| <= | Less than or equal to | Returns True if the left is smaller or equal (4 <= 5 is True) |
Example:
# Comparison Operators Example
a = 10
b = 20
print("Equal (==):", a == b)
print("Not Equal (!=):", a != b)
print("Greater Than (>):", a > b)
print("Less Than (<):", a < b)
print("Greater Than or Equal (>=):", a >= b)
print("Less Than or Equal (<=):", a <= b)
Output:
Explanation:Equal (==): False
Not Equal (!=): True
Greater Than (>): False
Less Than (<): True
Greater Than or Equal (>=): False
Less Than or Equal (<=): True
Comparison operators compare two values and return either True or False.
4. Logical Operators: Logical operators are used to combine conditional expressions.
| Operator | Name | How It Works (Example) |
| and | Logical AND | Returns True if both sides are true (True and False is False) |
| or | Logical OR | Returns True if at least one side is true (True or False is True) |
| not | Logical NOT | Inverts the Boolean result (not True is False) |
Example:
# Logical Operators Example
x = True
y = False
print("AND (and):", x and y)
print("OR (or):", x or y)
print("NOT (not):", not x)
Output:
Explanation:AND (and): False
OR (or): True
NOT (not): False
- and returns True only if both conditions are true.
- or returns True if at least one condition is true.
- not reverses the Boolean value.
5. Bitwise Operators: Bitwise operators are used to perform calculations on the binary (bits) representation of numbers.
| Operator | Name | How It Works |
| & | Bitwise AND | Sets each bit to 1 if both bits are 1 |
| | | Bitwise OR | Sets each bit to 1 if at least one bit is 1 |
| ^ | Bitwise XOR | Sets each bit to 1 if only one of the bits is 1 |
| ~ | Bitwise NOT | Inverts all the bits (turns 1s to 0s and 0s to 1s) |
| << | Left Shift | Shifts bits to the left by pushing zeros in from the right |
| >> | Right Shift | Shifts bits to the right by removing the rightmost bits |
Example:
# Bitwise Operators Example
a = 10 # 1010
b = 6 # 0110
print("AND (&):", a & b)
print("OR (|):", a | b)
print("XOR (^):", a ^ b)
print("NOT (~):", ~a)
print("Left Shift (<<):", a << 2)
print("Right Shift (>>):", a >> 2)
Output:
Explanation:AND (&): 2
OR (|): 14
XOR (^): 12
NOT (~): -11
Left Shift (<<): 40
Right Shift (>>): 2
- & performs bitwise AND.
- | performs bitwise OR.
- ^ performs bitwise XOR.
- ~ inverts all bits.
- << shifts bits to the left.
- >> shifts bits to the right.
6. Membership Operators: Membership operators are used to test whether a value is a member of a sequence (like a string, list, or tuple).
| Operator | Name | How It Works (Example) |
| in | Membership IN | Returns True if the value is found inside ('a' in 'apple' is True) |
| not in | Membership NOT IN | Returns True if the value is missing from inside ('b' not in 'apple' is True) |
Example:
# Membership Operators Example
numbers = [10, 20, 30, 40]
print("20 in numbers:", 20 in numbers)
print("50 in numbers:", 50 in numbers)
print("50 not in numbers:", 50 not in numbers)
print("20 not in numbers:", 20 not in numbers)
Output:
Explanation:20 in numbers: True
50 in numbers: False
50 not in numbers: True
20 not in numbers: False
- in checks whether an element exists in a sequence.
- not in checks whether an element does not exist in a sequence.
7. Identity Operators: Identity operators are used to check if two variables reference the exact same object located in the computer's memory.
| Operator | Name | How It Works (Example) |
| is | Identity IS | Returns True if variables point to the same object |
| is not | Identity IS NOT | Returns True if variables point to different objects |
Example:
# Identity Operators Example
list1 = [1, 2, 3]
list2 = list1
list3 = [1, 2, 3]
print("list1 is list2:", list1 is list2)
print("list1 is list3:", list1 is list3)
print("list1 is not list3:", list1 is not list3)
Output:
list1 is list2: True
list1 is list3: False
list1 is not list3: True
Explanation:
- is checks whether two variables refer to the same object in memory.
- is not checks whether two variables refer to different objects.
8. Walrus Operator: The walrus operator was introduced in Python 3.8. It allows assignment and expression evaluation simultaneously.
It is denoted by :=
Example:
# Walrus Operator working
numbers = [10, 20, 30]
if (n := len(numbers)) 2:
print("Length: ", n)
Output:
Length: 3
Conclusion
Operators are an essential part of Python programming. They enable programmers to perform calculations, compare values, manipulate data, and control program execution efficiently. Python provides a rich set of operators, including arithmetic, assignment, comparison, logical, bitwise, membership, identity, and walrus operators. A thorough understanding of these operators helps developers write cleaner, more efficient, and more powerful Python programs.Frequently Asked Questions
1. What is the difference between == and is?2. How many types of operators are available in Python?
- == (Equality): Checks if the values of two objects are equal.
- is (Identity): Checks if two variables point to the exact same object in memory (i.e., they share the same memory address).
3. What is the purpose of the walrus operator?Python provides eight major categories of operators: arithmetic, assignment, comparison, logical, bitwise, membership, identity, and walrus operators.
4. Why does 10 / 2 equal 5.0 instead of 5?Introduced in Python 3.8, the assignment expression operator - also called the Walrus operator because := looks like eyes and tusks - allows you to assign a value to a variable inside an expression.
5. Can different types of operators be used together in a single expression?In Python 3, the standard division operator (/) always performs true division and returns a floating-point number (a decimal), even if the numbers divide evenly. If you want a whole integer result, use the floor division (//) operator instead.
Yes, Python allows different types of operators to be combined in a single expression. For example, an expression can include arithmetic, comparison, and logical operators together. Python evaluates the expression according to its operator precedence rules, ensuring that the final result is calculated correctly.
3 Comments