Description: Operators in Python are symbols or keywords used to perform specific operations on variables and values.Python provides a rich set of operators to handle various types of tasks like arithmetic, comparison, logical operations, membership and more.
Step-by-Step Process
Arithmetic operators: It is used to perform mathematical calculations. Operators used : +, -, *, /, %, **, //
Logical operators: It is used to combine conditional statements. Operators used : AND, OR, NOT
Assignment operators: It is used to assign values to variables. Operators used: =, +=, -=, *=, /=, **=, //=, %=
Comparison operator: It is used to compare two values and return a boolean (True or False) Operators used: ==, !=, <, >, <=, >=
Bitwise operator: It is used to perform operations on binary numbers. Operators used: &, `, ^, ~, >>, <<
Membership operator: It is used to check if a value is in a sequence. Operators used: in, not in
Identity operator: It is used to compare the memory locations of two objects. Operators used: is, is not
Sample Code
# Arithmetic Operators
print("Arithmetic Operators:")
a = 10
b = 3
print("Addition (a + b):", a + b)
print("Subtraction (a - b):", a - b)
print("Multiplication (a * b):", a * b)
print("Division (a / b):", a / b)
print()
# Assignment Operators
print("Assignment Operators:")
x = 5
x += 2
print("x += 2:", x)
y = 5
y *= 2
print("Y *3:", y)
print()
print("Comparison Operators:")
p=5
q=10
print("p == q:", p == q)
print("p != q:", p != q)
print("p > q:", p > q)
print("p < q:", p < q)
print()
# Logical Operators
print("Logical Operators:")
r=10
s=3
print("a > 5 and b < 5:", r > 5 and s < 5)
print("a > 15 or b < 5:", r > 15 or s < 5)
print("not (a > b):", not (r > s))
print()
# Membership Operators
print("Membership Operators:")
my_list = [1, 2, 3, 4, 5]
print("2 in my_list:", 2 in my_list)
print("6 not in my_list:", 6 not in my_list)
print()
# Identity Operators
print("Identity Operators:")
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print("x is z:", x is z)
print("x is y:", x is y)
print("x is not y:", x is not y)