**Python Operators**

In Python programming, operators are used to perform operations on operands (i.e. variables and values).

Python categorizes the operators in the following groups:

- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Identity operators
- Membership operators
- Bit-wise operators

**1. Arithmetic Operators: **

These operators are used with numeric values to perform common mathematical operations.

Operator | Meaning | Example |
---|---|---|

+ | Add two operands | x + y |

– | Subtract right operand from the left | x – y |

* | Multiply two operands | x * y |

/ | Divide left operand by the right one (always results into float) | x / y |

% | Modulus – remainder of the division of left operand by the right | x % y |

// | Floor division – division that results into whole number adjusted to the left in the number line | x // y |

** | Exponent – left operand raised to the power of right | x**y |

**Example 1**

# Arithmetic operators in Python

x = 27

y = 4

print(x+y) # addition

print(x-y) # subtraction

print(x*y) # multiplication

print(x/y) # division (float)

print(x%y) # modulus

print(x//y) # division (int)

print(x**y) # exponent

**Output:**

31

23

108

6.75

3

6

531441

**2. Assignment Operators: **

These operators are used to assign values to variables.

Operator | Example | Equivalent to |
---|---|---|

= | x = 5 | x = 5 |

+= | x += 5 | x = x + 5 |

-= | x -= 5 | x = x – 5 |

*= | x *= 5 | x = x * 5 |

/= | x /= 5 | x = x / 5 |

%= | x %= 5 | x = x % 5 |

//= | x //= 5 | x = x // 5 |

**= | x **= 5 | x = x ** 5 |

&= | x &= 5 | x = x & 5 |

|= | x |= 5 | x = x | 5 |

^= | x ^= 5 | x = x ^ 5 |

>>= | x >>= 5 | x = x >> 5 |

<<= | x <<= 5 | x = x << 5 |

**Example 2**

# Assignment operators in Python

x = 5

x = x + 5 # x += 5

print(x)

x = x - 5 # x -= 5

print(x)

x = x * 5 # x *= 5

print(x)

x = x / 5 # x /= 5

print(x)

x = x % 5 # x %= 5

print(x)

**Output:**

10

5

25

5.0

0.0

**3. Relational Operators: **

These operators are used to compare two values.

Operator | Meaning | Example |
---|---|---|

> | Greater that – True if left operand is greater than the right | x > y |

< | Less that – True if left operand is less than the right | x < y |

== | Equal to – True if both operands are equal | x == y |

!= | Not equal to – True if operands are not equal | x != y |

>= | Greater than or equal to – True if left operand is greater than or equal to the right | x >= y |

<= | Less than or equal to – True if left operand is less than or equal to the right | x <= y |

**Example 3**

# Relational operators in Python

a = 10

b = 5

print(a>b) # True

print(a<b) # False

print(a==b) # False

print(a!=b) # True

print(a>=b) # True

print(a<=b) # False

**Output:**

True

False

False

True

True

False

**4. Logical Operators: **

These operators are used to combine conditional statements.

Operator | Meaning | Example |
---|---|---|

and | True if both the operands are true | x and y |

or | True if either of the operands is true | x or y |

not | True if operand is false (complements the operand) | not x |

**Example 4**

# Logical operators in Python

print("Logical AND : ", (10>5) and (9<7)) # False

print("Logical OR : ", (10>5) or (9<7)) # True

print("Logical NOT : ", not(9<7)) # True

**Output:**

Logical AND : False

Logical OR : True

Logical NOT : True

**5. Identity Operators: **

These operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

Operator | Meaning | Example |
---|---|---|

is | True if the operands are identical (refer to the same object) | x is True |

is not | True if the operands are not identical (do not refer to the same object) | x is not True |

**Example 5**

# Identity operators in Python

print("Identity operators:-")

x = True

print(x is True) # True

print(x is not True) # False

**Output:**

Identity operators:-

True

False

**6. Membership Operators: **

These operators are used to test if a sequence is present in an object.

Operator | Meaning | Example |
---|---|---|

in | True if value/variable is found in the sequence | 5 is x |

not in | True if value/variable is not found in the sequence | 5 not in x |

**Example 6**

# Membership operators in Python

print("Membership operators:-")

arr = [10, 20, 30, 40, 50]

x = 40

print(x in arr) # True

x = 60

print(x not in arr) # True

**Output:**

Membership operators:-

True

True

**7. Bit-wise Operators: **

These operators are used to compare binary numbers.

For example, in the table below, let `x` = 10 (`0000 1010`

in binary) and `y` = 4 (`0000 0100`

in binary)

Operator | Meaning | Example |
---|---|---|

& | Bit-wise AND | x& y = 0 (`0000 0000` ) |

| | Bit-wise OR | x | y = 14 (`0000 1110` ) |

~ | Bit-wise NOT | ~x = -11 (`1111 0101` ) |

^ | Bit-wise XOR | x ^ y = 14 (`0000 1110` ) |

>> | Bit-wise right shift | x>> 2 = 2 (`0000 0010` ) |

<< | Bit-wise left shift | x<< 2 = 40 (`0010 1000` ) |

**Example 7**

# Bit-wise operators in Python

x = 10

y = 4

print(x & y) # Bit-wise AND: 0

print(x | y) # Bit-wise OR: 14

print(~x) # Bit-wise NOT: -11

print(x ^ y) # Bit-wise XOR: 14

print(x>>2) # Bit-wise right shift: 2

print(x<<2) # Bit-wise left shift: 40

**Output:**

0

14

-11

14

2

40