In python, operators can perform different operations like logical, comparison, arithmetic, etc., on the defined variables and values. For example, the addition (+) operator is useful to perform the addition operation on defined operands.

In python, operators are categorized into different groups. Those are

- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
- Identity Operators

In python, arithmetic operators perform the necessary arithmetic calculations like addition, subtraction, division, etc., on the defined variables and values.

The following table lists the different types of arithmetic operators in python.

Operator | Name | Description | Example (a = 10, b = 3) |
---|---|---|---|

+ | Addition | It adds two operands. | a + b = 13 |

- | Subtraction | It subtracts two operands. | a - b = 7 |

* | Multiplication | It multiplies two operands. | a * b = 30 |

/ | Division | It divides numerator by de-numerator. | a / b = 3.3333 |

% | Modulus | It will return the remainder as a result. | a % b = 1 |

// | Floor division | It returns the division result by adjusting to the left. | a // b = 3 |

** | Exponent | It returns the exponentiation result (left operand will increase to power of right). | a ** b = 1000 |

Following is the example of using arithmetic operators in the python programming language.

a = 10

b = 3

print("a + b =",a+b)

print("a - b =",a-b)

print("a * b =",a*b)

print("a / b =",a/b)

print("a % b =",a%b)

print("a // b =",a//b)

print("a ** b =",a**b)

b = 3

print("a + b =",a+b)

print("a - b =",a-b)

print("a * b =",a*b)

print("a / b =",a/b)

print("a % b =",a%b)

print("a // b =",a//b)

print("a ** b =",a**b)

If you observe the above example, we used arithmetic operators (+, -, *, /, %, **, //) to perform different operations on defined operands based on our requirements.

When you execute the above python program, you will get the result as shown below.

a + b = 13

a - b = 7

a * b = 30

a / b = 3.3333333333333335

a % b = 1

a // b = 3

a ** b = 1000

a - b = 7

a * b = 30

a / b = 3.3333333333333335

a % b = 1

a // b = 3

a ** b = 1000

In python, comparison operators are useful to compare variables and values to determine whether the variables equal or not, etc., based on our requirements. The comparison operators will return either `True`

or `False`

based on the defined condition.

The following table lists the different types of comparison operators in python.

Operator | Name | Example (a = 6, b = 3) |
---|---|---|

== | Equal to | a == b (False) |

> | Greater than | a > b (True) |

< | Less than | a < b (False) |

>= | Greater than or Equal to | a >= b (True) |

<= | Less than or Equal to | a <= b (False) |

!= | Not Equal to | a != b (True) |

Following is the example of using comparison operators in the python programming language.

a = 6

b = 3

print("a == b =",a == b)

print("a > b =",a > b)

print("a < b =",a < b)

print("a >= b =",a >= b)

print("a <= b =",a <= b)

print("a != b =",a != b)

b = 3

print("a == b =",a == b)

print("a > b =",a > b)

print("a < b =",a < b)

print("a >= b =",a >= b)

print("a <= b =",a <= b)

print("a != b =",a != b)

If you observe the above example, we used comparison operators (==, >, <, >=, <=, !=) to perform different operations on the defined operands based on our requirements.

When you execute the above python program, you will get the result as shown below.

a == b = False

a > b = True

a < b = False

a >= b = True

a <= b = False

a != b = True

a > b = True

a < b = False

a >= b = True

a <= b = False

a != b = True

In python, logical operators are useful to combine operands or conditional statements based on our requirements. The logical operators will work with Boolean expressions (`True`

or `False`

) and return Boolean values.

The following table lists the different types of logical operators in python.

Operator | Description | Example (a = True, b = False) |
---|---|---|

and | It returns True if both operands are True. | a and b (False) |

or | It returns True if either of the operands is True. | a or b (True) |

not | It will reverse the result. If the operand is True, it will return False and vice-versa. | not(a and b) (True) |

Following is the example of using logical operators in the python programming language.

a = 15

b = 10

# AND operator

result = (a <= b) and (a > 10)

print("AND Operator: ", result)

# OR operator

result = (a >= b) or (a < 5)

print("OR Operator: ", result)

# NOT operator

result = not((a <= b) and (a > 10))

print("NOT Operator: ", result)

b = 10

# AND operator

result = (a <= b) and (a > 10)

print("AND Operator: ", result)

# OR operator

result = (a >= b) or (a < 5)

print("OR Operator: ", result)

# NOT operator

result = not((a <= b) and (a > 10))

print("NOT Operator: ", result)

If you observe the above example, we used logical operators (and, or, not) with conditional statements to evaluate the expressions based on our requirements.

When you execute the above python program, you will get the result as shown below.

AND Operator: False

OR Operator: True

NOT Operator: True

OR Operator: True

NOT Operator: True

In python, bitwise operators will work on bits, and these are useful to perform bit by bit operations such as Bitwise AND (&), Bitwise OR (|), Bitwise Exclusive OR (^), etc., on operands. We can perform bit-level operations on **Boolean** and **integer** data.

For example, we have two variables, a = 10, b = 20, and the binary format of these variables will be as shown below.

a = 10 (00001010)

b = 20 (00010100)

b = 20 (00010100)

When we apply **Bitwise OR (|)** operator on these parameters, we will get the result as shown below.

00001010

00010100

-----------

00011110 = 30

00010100

-----------

00011110 = 30

The following table lists the different types of bitwise operators in python.

Operator | Name | Description | Example (a = 0, b = 1) |
---|---|---|---|

& | Bitwise AND | It compares each bit of the first operand with the corresponding bit of its second operand. If both bits are 1, then the result bit will be 1; otherwise, the result bit will be 0. | a & b (0) |

| | Bitwise OR | It compares each bit of the first operand with the corresponding bit of its second operand. If either of the bit is 1, then the result bit will be 1; otherwise, the result bit will be 0. | a | b (1) |

^ | Bitwise Exclusive OR (XOR) | It compares each bit of the first operand with the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, then the result bit will be 1; otherwise, the result bit will be 0. | a ^ b (1) |

~ | Bitwise Not | It operates on only one operand, and it will invert each bit of operand. It will change bit 1 to 0 and vice versa. | ~(a) (1) |

<< | Bitwise Left Shift) | It shifts the number to the left based on the specified number of bits. The zeroes will be added to the least significant bits. | b << 2 (100) |

>> | Bitwise Right Shift | It shifts the number to the right based on the specified number of bits. The zeroes will be added to the least significant bits. | b >> 2 (001) |

Following is the example of using bitwise operators in the python programming language.

x = 5

y = 10

result = x & y;

print("Bitwise AND: ", result)

result = x | y

print("Bitwise OR: ", result)

result = x ^ y;

print("Bitwise XOR: ", result)

result = ~x;

print("Bitwise Not: ", result)

result = x << 2;

print("Bitwise Left Shift: ", result)

result = x >> 2

print("Bitwise Right Shift: ", result)

y = 10

result = x & y;

print("Bitwise AND: ", result)

result = x | y

print("Bitwise OR: ", result)

result = x ^ y;

print("Bitwise XOR: ", result)

result = ~x;

print("Bitwise Not: ", result)

result = x << 2;

print("Bitwise Left Shift: ", result)

result = x >> 2

print("Bitwise Right Shift: ", result)

If you observe the above code, we used different bitwise operators (&, |, ^, ~, <<, >>) to perform different operations on defined operands.

When you execute the above python program, you will get the result as shown below.

Bitwise AND: 0

Bitwise OR: 15

Bitwise XOR: 15

Bitwise Not: -6

Bitwise Left Shift: 20

Bitwise Right Shift: 1

Bitwise OR: 15

Bitwise XOR: 15

Bitwise Not: -6

Bitwise Left Shift: 20

Bitwise Right Shift: 1

In python, assignment operators can assign a new value to the operand, and these operators will work with only one operand.

The following table lists the different types of assignment operators in python.

Operator | Name | Example |
---|---|---|

= | Equal to | a = 10 |

+= | Addition Assignment | a += 10 Equals to a = a + 10 |

-= | Subtraction Assignment | a -= 10 Equals to a = a - 10 |

*= | Multiplication Assignment | a *= 10 Equals to a = a * 10 |

/= | Division Assignment | a /= 10 Equals to a = a / 10 |

%= | Modulo Assignment | a %= 10 Equals to a = a % 10 |

**= | Exponent Assignment | a **= 10 Equals to a = a ** 10 |

//= | Floor Division Assignment | a //= 10 Equals to a = a // 10 |

&= | Bitwise AND Assignment | a &= 10 Equals to a = a & 10 |

|= | Bitwise OR Assignment | a |= 10 Equals to a = a | 10 |

^= | Bitwise Exclusive OR Assignment | a ^= 10 Equals to a = a ^ 10 |

>>= | Right Shift Assignment | a >>= 2 Equals to a = a >> 2 |

<<= | Left Shift Assignment | a <<= 2 Equals to a = a << 2 |

In python, membership operators are useful to find whether the specified value exists in a sequence of objects or not.

The following table lists the different types of membership operators in python.

Operator | Description | Example |
---|---|---|

in | It will return True if the value is found in the sequence object. | a in b |

not in | It will return True if the value is not found in the sequence object. | a not in b |

Following is the example of using membership operators in the python programming language.

a = [30, 10, 20, "tutlane"]

b = "Welcome to Tutlane"

print("com" in b)

print(50 in a)

print("lane" not in a)

b = "Welcome to Tutlane"

print("com" in b)

print(50 in a)

print("lane" not in a)

If you observe the above example, we used membership operators (`in`

, `not in`

) to verify whether the specified values exist in the defined objects (a, b) or not.

When you execute the above python program, you will get the result as shown below.

True

False

True

False

True

In python, identity operators are useful to compare whether the variables are pointing to the same object and the same memory location or not.

The following table lists the different types of identity operators in python.

Operator | Description | Example |
---|---|---|

is | It will return True if both variables refer to the same object. | a is b |

is not | It will return True if both variables do not refer to the same object. | a is not b |

Following is the example of using identity operators in the python programming language.

a = [30, 10, 20]

b = [30, 10, 20]

c = a

print(a is b)

print(a is c)

print(a is not b)

b = [30, 10, 20]

c = a

print(a is b)

print(a is c)

print(a is not b)

If you observe the above example, we created two variables (**a**, **b**) with the same content and another variable (**c**) by referring to the existing variable (**a**). We used identity operators (`is`

, `is not`

) to verify whether the defined variables (**a**, **b**, **c**) referring the same object or not.

When you execute the above python program, you will get the result as shown below.

False

True

True

True

True

If you observe the above result, **a is b** condition return **False** because the **a** is not the same object as **b**, even though they have the same content. Another condition **a is c** return True because **a** is the same object as **c**.