Operators in Python

Rina Mondal
5 min readFeb 16, 2024

--

Operators are special symbols or keywords that perform operations on operands. In this blog, we will discuss some common types of operators in Python:

  1. Arithmetic Operator
  2. Comparison Operator
  3. Logical Operator
  4. Assignment Operator
  5. Membership Operator
  6. Identity Operator
  7. Bitwise Operator

Let’s discuss in details:

Arithmetic Operators:

Arithmetic operators are used in programming to perform mathematical operations on numerical values.

-Addition: `+`
- Subtraction: `-`
- Multiplication: `*`
- Division: `/`
- Floor Division: `//` (returns the floor value after division)
- Modulus: `%` (returns the remainder after division)
- Exponentiation: `**` (raises the left operand to the power of the right operand)
# Example:

# Addition
result_addition = 5 + 3
print("Addition:", result_addition) # Output: 8

# Subtraction
result_subtraction = 10 - 4
print("Subtraction:", result_subtraction) # Output: 6

# Multiplication
result_multiplication = 7 * 2
print("Multiplication:", result_multiplication) # Output: 14

# Division
result_division = 15 / 4
print("Division:", result_division) # Output: 3.75

# Floor Division
result_floor_division = 15 // 4
print("Floor Division:", result_floor_division) # Output: 3

# Modulus
result_modulus = 15 % 4
print("Modulus:", result_modulus) # Output: 3

# Exponentiation
result_exponentiation = 2 ** 3
print("Exponentiation:", result_exponentiation) # Output: 8

2. Comparison Operators:

Comparison operators, also known as relational operators, are used in programming to compare the values of two operands. These operators return a boolean value, either true or false, based on whether the comparison is true or false. Here are the common comparison operators:

- Equal to: `==`
- Not equal to: `!=`
- Greater than: `>`
- Less than: `<`
- Greater than or equal to: `>=`
- Less than or equal to: `<=`
# Equal to
result_equal =5== 5
print("Equal to:", result_equal) # Output: True

# Not equal to
result_not_equal = 5 != 3
print("Not equal to:", result_not_equal) # Output: True

# Greater than
result_greater_than = 7 > 3
print("Greater than:", result_greater_than) # Output: True

# Less than
result_less_than = 2 < 6
print("Less than:", result_less_than) # Output: True

# Greater than or equal to
result_greater_equal = 8 >= 8
print("Greater than or equal to:", result_greater_equal) # Output: True

# Less than or equal to
result_less_equal = 4 <= 4
print("Less than or equal to:", result_less_equal) # Output: True

3. Logical Operators:

Logical operators are used in programming to combine or modify logical statements. These operators allow you to perform logical operations on boolean values, expressions, or variables, and they return a boolean result.

- AND: `and`
- OR: `or`
- NOT: `not`
# AND
result_and = (5 > 3) and (4 < 7)
print("AND:", result_and) # Output: True

# OR
result_or = (5 < 3) or (4 < 7)
print("OR:", result_or) # Output: True

# NOT
result_not = not (5 < 3)
print("NOT:", result_not) # Output: True

4. Assignment Operators:

The assignment operator is used in programming to assign a value to a variable. It is denoted by the equals sign (=).

- Assignment: `=`
- Add and assign: `+=`
- Subtract and assign: `-=`
- Multiply and assign: `*=`
- Divide and assign: `/=`
# Assignment
x = 5
print("After assignment, x =", x)

# Add and assign
x += 3 # Equivalent to x = x + 3
print("After add and assign, x =", x)

# Subtract and assign
x -= 2 # Equivalent to x = x - 2
print("After subtract and assign, x =", x)

# Multiply and assign
x *= 4 # Equivalent to x = x * 4
print("After multiply and assign, x =", x)

# Divide and assign
x /= 2 # Equivalent to x = x / 2
print("After divide and assign, x =", x)

5. Membership Operators:

Membership operators are used in programming to test whether a value is a member of a sequence, such as a string, list, tuple, or set. Python provides two membership operators: in and not in.

- `in`: Returns `True` if a value is found in the sequence.
- `not in`: Returns `True` if a value is not found in the sequence.
# Define a list
fruits = ["apple", "banana", "orange", "grape", "kiwi"]

# Check if "banana" is in the list
if "banana" in fruits:
print("Yes, 'banana' is in the list.")

# Check if "watermelon" is not in the list
if "watermelon" not in fruits:
print("Yes, 'watermelon' is not in the list.")

6. Identity Operators:

The “identity” operator in Python is used to check if two variables refer to the same object in memory. Python provides two identity operators: is and is not.

1. is Operator:

  • The is operator returns True if two variables point to the same object in memory.
a = [1, 2, 3]
b = a # b points to the same object as a
print(a is b) # True

2. is not Operator:

  • The is not operator returns True if two variables do not point to the same object in memory.
x = "hello"
y = "world"
print(x is not y) # True, as x and y are different objects

7. Bitwise Operator:

Bitwise operators are operators that perform operations on individual bits of binary numbers. These operators are used in programming languages to manipulate integers at the bit level.

1. AND (`&`): Performs a bitwise AND operation between corresponding bits of two operands. The result is 1 if both bits are 1, otherwise, it’s 0.

2. OR (`|`): Performs a bitwise OR operation between corresponding bits of two operands. The result is 1 if at least one of the bits is 1.

3. XOR (`^`): Performs a bitwise XOR (exclusive OR) operation between corresponding bits of two operands. The result is 1 if the bits are different, otherwise, it’s 0.

4. NOT (`~`): Performs a bitwise NOT operation, which inverts each bit of the operand, turning 0s into 1s and vice versa.

5. Left Shift (`<<`): Shifts the bits of the first operand to the left by a specified number of positions (the second operand), filling the shifted bits with zeros.

6. Right Shift (`>>`): Shifts the bits of the first operand to the right by a specified number of positions (the second operand), filling the shifted bits with zeros or the sign bit.

Bitwise operators are commonly used in tasks such as manipulating device registers, optimizing memory usage, cryptography, and low-level programming. They can also be used in certain algorithms and optimizations where operating at the bit level is advantageous.

8. ‘==’ operator:

‘==’ operator to check for equality in values.

# Create two lists with the same values
list_a = [1, 2, 3]
list_b = [1, 2, 3]

# Check for equality using the == operator
print(list_a == list_b)

#O/t- True, as the values are the same

9. Difference between is operator and ‘==’ operator:

It checks for object identity, meaning it verifies if the two variables point to the exact same object, rather than having the same values. The is operator returns True if the variables reference the same object and False otherwise. The ‘==’ operator compares the values of the objects, not their identities.

# Create two lists with the same values
list_a = [1, 2, 3]
list_b = [1, 2, 3]


# Check for equality using the == operator
print(list_a == list_b)
#O/t- True, as the values are the same


# Check for identity using the is operator
print(list_a is list_b)
#O/t- False, as list_a and list_b are different objects in memory


# Create another reference to list_a
list_c = list_a
# Check for identity using the is operator
print(list_a is list_c)

#O/t- True, as list_a and list_c refer to the same object in memory

Give it :👏👏👏👏:
If you found this guide helpful , why not show some love? Give it a Clap 👏, and if you have questions or topics you’d like to explore further, drop a comment 💬 below 👇. If you appreciate my hard work please follow me. That is the only way I can continue my passion.

--

--

Rina Mondal

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.