Follow @devglan
# Different Python Operators

By Dhiraj,
14 March, 2019
0K

## Arithematic Operators

**Example**
**Output**
## Assignment operators

## Comparison operators

## Logical Operators

**Example**
**Output**
## Identity Operators

**Example**
**Output**
## == and is operator in Python

## Membership Operators

**Output**
## Bitwise Operators

## Conclusion

#### If You Appreciate This, You Can Consider:

#### About The Author

#### Further Reading on Python

Operators are symbols that carry out arithmetic or logical operations. In this tutorial, we will learn about the different operators available in Python. We will try to understand all the operations with suitable example and list down all the operators. Different operators that we will discussing here are

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

These operators are used to perform arithematic operations such as addition, subtraction, multiplication etc. Below are the list of common arithematic operators.

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

+ | Perform addition | add(a, b) | a + b |

- | Perform subtraction | sub(a, b) | a - b |

* | Perform multiplication | mul(a, b) | a * b |

/ | Perform Division. Result in float | truediv(a, b) | a / b |

% | Modulus operator. Returns the remainder | mod(a, b) | a % b |

** | Exponentiation.Left operand raised to the power of right | pow(a, b) | a ** b |

// | Floor division | floordiv(a, b) | a // b |

a = 9 print(9 / 2) print(9 // 2) print(9 % 2) print(9 ** 2)

4.5 1 4 81

Operator | Example | Equivatent To |
---|---|---|

= | a = 5 | a = 5 |

+= | a += 5 | a = a + 5 |

-= | a -= 5 | a = a - 5 |

*= | a *= 5 | a = a * 5 |

/= | a /= 5 | a = a / 5 |

%= | a %= 5 | a = a % 5 |

//= | a //= 5 | a = a // 5 |

**= | a **= 5 | a = a ** 5 |

&= | a &= 5 | a = a & 5 |

|= | a |= 5 | a = a | 5 |

^= | a ^= 5 | a = a ^ 5 |

>>= | a >>= 5 | a = a >> 5 |

<<= | a <<= 5 | a = a << 5 |

Comparison operators are used to compare two different variables. The result is always bool type. Below are the details:

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

== | Equals to. True if left equals to right | eq(a, b) | a == b |

> | Greater than | gt(a, b) | a > b |

< | Less than | lt(a, b) | a < b |

>= | Greater than or equal to | ge(a, b) | a >= b |

<= | Less than or equal to | le(a, b) | a <= b |

Logical operators are used with Boolean (logical) values.

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

and | Returns True if both statements are true | a < 5 and b > 2 |

or | Returns True if one of the statements is true | a < 5 or b > 2 |

not | Reverse the result, returns False if the result is true | not(a < 5 or b > 2) |

a = 5 b = 3 print(a < 5 and b > 2) print(a < 5 or b > 2) print(not(a < 5 or b > 2)) print(5 > a > 2)

False True False False

Check if two variables are located in the same memory location. is and is not are the identity operators in Python.

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

is | Returns true if both variables are the same object | a is b |

is not | Returns true if both variables are not the same object | a is not b |

a = 2 b = 2 c = 'John' d = 'John' e = [1, 2, 3] f = [1, 2, 3] print(a is not b) print(c is d) print(e is f)

False True False

== operator compares the value whereas is operator compares the memory reference. Hence, if two objects having same values then == returns true but that may not be the case with is operator.

x = [1, 2, 3] y = [1, 2, 3] print(x == y) # returns True print(x is y) # returns False

Membership operators are used to check if an object is present in any sequence such as string, list, tuple, set and dictionary

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

in | Returns True if a an object is contained in a seqquence | a in b |

not in | True if a an object is not contained in a seqquence | a not in b |

a = [1, 2, 6] print(1 in a) print(2 not in a)

True False

Bitwise operators are used to compare binary numbers bit by bit.

Operator | Description | Function |
---|---|---|

& | Sets each bit to 1 if both bits are 1 | and_(a, b) |

| | OR. Sets each bit to 1 if one of two bits is 1 | or_(a, b) |

^ | XOR. Sets each bit to 1 if only one of two bits is 1 | xor(a, b) |

~ | NOT.Inverts all the bits | invert(a) |

<< | Zero fill left shift. Shift left by pushing zeros in from the right and let the leftmost bits fall off | lshift(a, b) |

>> | Signed right shift. Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | rshift(a, b) |

In this tutorial, we will learnt about the different operators provided in Python such as Arithmetic, Assignment, Comparison, Logical, Identity, Membership and Bitwise operators.