OPERATORS IN PYTHON


In any programming language, operators are the symbols that are used to perform mathematical and logical operations in an expression.

In the expression:
a = b + c
a,b,c => operands
=, + => operators 

Following are the operators used in python programming:

  • Arithmetic operators
  • Relational Operators
  • Logical operators
  • Assignment operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations, following are the arithmetic operators available in python:

Addition (+): Adds two operands.

a = 5
b = 3
result = a + b
print(result)  # Output: 8

Subtraction (-): Subtracts the right operand from the left operand.

a = 10
b = 7
result = a - b
print(result)  # Output: 3

Multiplication (*): Multiplies two operands.

a = 4
b = 6
result = a * b
print(result)  # Output: 24

Division (/): Divides the left operand by the right operand (always returns a float).

a = 10
b = 2
result = a / b
print(result)  # Output: 5.0

Floor Division (//): Divides the left operand by the right operand and returns the quotient as an integer (rounds down to the nearest integer).

a = 10
b = 3
result = a // b
print(result)  # Output: 3

Modulus (%): Returns the remainder of the division of the left operand by the right operand.

a = 10
b = 3
result = a % b
print(result)  # Output: 1

Exponentiation (**): Raises the left operand to the power of the right operand.

a = 2
b = 3
result = a ** b
print(result)  # Output: 8

Relational Operators

Relational operators are the comparison operators and are used to compare the value of given operands. The relational expression always evaluates to Boolean values: True or False. Following are the relational operators in python:

Equal to (==): Checks if the values of two operands are equal.

a = 5
b = 5
result = a == b
print(result)  # Output: True

Not equal to (!=): Checks if the values of two operands are not equal.

a = 5
b = 3
result = a != b
print(result)  # Output: True

Greater than (>): Checks if the left operand is greater than the right operand.

a = 7
b = 5
result = a > b
print(result)  # Output: True

Less than (<): Checks if the left operand is less than the right operand.

a = 3
b = 5
result = a < b
print(result)  # Output: True

Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.

a = 7
b = 5
result = a >= b
print(result)  # Output: True

Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

a = 3
b = 5
result = a <= b
print(result)  # Output: True

Logical Operators

Logical operators are used to perform operations on Boolean values or relational expression. The logical expression always evaluates to the Boolean values: True or False. Following are the logical operators in python programming:

Logical AND (and): Returns True if both operands are True, otherwise False.

a = True
b = False
result = a and b
print(result)  # Output: False

Logical OR (or): Returns True if at least one of the operands is True, otherwise False.

a = True
b = False
result = a or b
print(result)  # Output: True

Logical NOT (not): Returns True if the operand is False, and False if the operand is True.

a = True
result = not a
print(result)  # Output: False

Assignment operators

Assignment operators are used to assign value on the right side of = operators to the variable on the left side of = operators in python. basically , only =  is assignment operator. We can create shorthand notations for all the arithmetic operators as follows:

a = 10

value 10 is assigned to the variable a

we can write shorthand notation as follows:
for example:
a = 2
a = a+2 can be written as, a+=2
we can do same for all available arithmetic operators

Example code for assignment operators:

# Assignment operator (=)
x = 5
print(x)  # Output: 5

# Addition assignment (+=)
x += 3
print(x)  # Output: 8

# Subtraction assignment (-=)
x -= 2
print(x)  # Output: 6

# Multiplication assignment (*=)
x *= 4
print(x)  # Output: 24

# Division assignment (/=)
x /= 3
print(x)  # Output: 8.0

# Floor division assignment (//=)
x //= 2
print(x)  # Output: 4.0

# Modulus assignment (%=)
x %= 3
print(x)  # Output: 1.0

# Exponentiation assignment (**=)
x **= 2
print(x)  # Output: 1.0

Identity operators

Identity operators are used to check whether we are referring to the same object or in simple sense the same memory location. In python is  and is not are identity check operators. Here is how we can use identity operators:

is Operator: Returns True if both operands refer to the same object, otherwise False.

# Explanation: Using 'is' operator to check if two variables refer to the same object.
x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is y)  # Output: False, as x and y refer to different objects.
print(x is z)  # Output: True, as x and z refer to the same object.

is not Operator: Returns True if both operands do not refer to the same object, otherwise False.

# Explanation: Using 'is not' operator to check if two variables do not refer to the same object.
x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is not y)  # Output: True, as x and y do not refer to the same object.
print(x is not z)  # Output: False, as x and z refer to the same object.

Membership Operators

Membership operators are used to test whether a value is present in a sequence (such as a string, list, tuple, or set). Python has two membership operators: in and not in. Here's an explanation:

in Operator: Returns True if the value is found in the sequence, otherwise False.

# Explanation: Using 'in' operator to check if a value is present in a sequence.
my_list = [1, 2, 3, 4, 5]

print(3 in my_list)  # Output: True, as 3 is present in the list.
print(6 in my_list)  # Output: False, as 6 is not present in the list.

not in Operator: Returns True if the value is not found in the sequence, otherwise False.

# Explanation: Using 'not in' operator to check if a value is not present in a sequence.
my_string = "hello world"

print("h" not in my_string)  # Output: False, as 'h' is present in the string.
print("z" not in my_string)  # Output: True, as 'z' is not present in the string.

Bitwise Operator

Bitwise operators are used to perform bitwise operations on integers. Python supports several bitwise operators, including bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>). Here's a code snippet demonstrating the use of all bitwise operators:

# Explanation: Using bitwise operators to perform bitwise operations on integers.
a = 10  # Binary representation: 1010
b = 6   # Binary representation: 0110

# Bitwise AND (&): Performs a bitwise AND operation.
print("Bitwise AND:", a & b)  # Output: 2 (Binary representation: 0010)

# Bitwise OR (|): Performs a bitwise OR operation.
print("Bitwise OR:", a | b)   # Output: 14 (Binary representation: 1110)

# Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation.
print("Bitwise XOR:", a ^ b)  # Output: 12 (Binary representation: 1100)

# Bitwise NOT (~): Performs a bitwise NOT operation (inverts the bits).
print("Bitwise NOT for a:", ~a)  # Output: -11 (Binary representation: ...1111111111111111111110101)

# Left shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
print("Left shift for a:", a << 1)  # Output: 20 (Binary representation: 10100)

# Right shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
print("Right shift for a:", a >> 1)  # Output: 5 (Binary representation: 0101)