One of the essential features of Python is its operators, which enable you to perform a wide range of operations on data.
In this blog post, we will explore the different types of operators in Python and provide examples to illustrate their usage.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus. Here are the arithmetic operators in Python:
# Addition
x = 2
y = 3
z = x + y
print(z) # Output: 5
# Subtraction
x = 5
y = 3
z = x - y
print(z) # Output: 2
# Multiplication
x = 2
y = 3
z = x * y
print(z) # Output: 6
# Division
x = 6
y = 2
z = x / y
print(z) # Output: 3.0
# Modulus
x = 7
y = 3
z = x % y
print(z) # Output: 1
Assignment Operators
Assignment operators are used to assign values to variables. Here are the assignment operators in Python:
# Simple assignment
x = 5
print(x) # Output: 5
# Add and assign
x += 3
print(x) # Output: 8
# Subtract and assign
x -= 2
print(x) # Output: 6
# Multiply and assign
x *= 2
print(x) # Output: 12
# Divide and assign
x /= 3
print(x) # Output: 4.0
# Modulus and assign
x %= 3
print(x) # Output: 1.0
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True or False). Here are the comparison operators in Python:
# Equal to
x = 5
y = 5
print(x == y) # Output: True
# Not equal to
x = 5
y = 6
print(x != y) # Output: True
# Greater than
x = 5
y = 3
print(x > y) # Output: True
# Less than
x = 5
y = 7
print(x < y) # Output: True
# Greater than or equal to
x = 5
y = 5
print(x >= y) # Output: True
# Less than or equal to
x = 5
y = 5
print(x <= y) # Output: True
Logical Operators
Logical operators are used to combine two or more conditions and return a Boolean value. Here are the logical operators in Python:
# And operator
x = 5
print(x > 3 and x < 7) # Output: True
# Or operator
x = 5
print(x > 3 or x < 4) # Output: True
# Not operator
x = 5
print(not(x > 3 and x < 7)) # Output: False
Identity Operators
Identity operators are used to compare the memory location of two objects. There are two identity operators in Python:
# is operator
x = [1, 2, 3]
y = [1, 2, 3]
print(x is y) # Output: False
# is not operator
x = [1, 2, 3]
y = [1, 2, 3]
print(x is not y) # Output: True
Membership Operators
Membership operators are used to check whether a value is present in a sequence, such as a string, list, or tuple. Here are the membership operators in Python:
# in operator
x = [1, 2, 3]
print(2 in x) # Output: True
# not in operator
x = [1, 2, 3]
print(4 not in x) # Output: True
Bitwise Operators
Bitwise operators are used to perform bitwise operations on binary numbers. Here are the bitwise operators in Python:
# Bitwise AND
x = 5
y = 3
print(x & y) # Output: 1
# Bitwise OR
x = 5
y = 3
print(x | y) # Output: 7
# Bitwise XOR
x = 5
y = 3
print(x ^ y) # Output: 6
# Bitwise NOT
x = 5
print(~x) # Output: -6
# Bitwise left shift
x = 5
print(x << 2) # Output: 20
# Bitwise right shift
x = 5
print(x >> 2) # Output: 1
Ternary Operator
The ternary operator is a shorthand way of writing an if-else statement in Python. It has the following syntax:
value_if_true if condition else value_if_false
Here’s an example:
x = 5
print("Even" if x % 2 == 0 else "Odd") # Output: Odd
In this example, the ternary operator checks if x
is even or odd, and returns the string "Even"
if it’s even, and "Odd"
if it’s odd.
Operator Overloading
Python allows you to overload operators for your own classes. This means you can define what the +
or -
operators mean when applied to instances of your own classes. Here’s an example:
# point.py
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = p1 + p2
p4 = p2 - p1
print(p3.x, p3.y) # Output: 4 6
print(p4.x, p4.y) # Output: 2 2
In this example, we define a Point
class and overload the +
and -
operators to perform addition and subtraction on Point
objects.
Operator Precedence
When you use multiple operators in an expression, Python follows a specific order of precedence to determine the order in which to evaluate the operators. The operator precedence order is as follows (decreasing in order in the following Table):
Operator | Symbols |
---|---|
Parentheses | () |
Exponentiation | ** |
Negation | - |
Multiplication, Division, Modulus | *, /, % |
Addition, Subtraction | +, - |
Bitwise Left shift, Bitwise Right shift | <<, >> |
Bitwise AND | & |
Bitwise XOR | ^ |
Bitwise OR | | |
Comparison operators | <, <=, >, >=, ==, != |
Logical NOT | not |
Logical AND | and |
Logical OR | or |
Here’s an example that shows how operator precedence works in Python:
# Example
x = 2 + 3 * 4
print(x) # Output: 14
In this example, Python first evaluates the multiplication operation (3 * 4) and then performs the addition operation (2 + 12) to get a final result of 14.