#### Python Operators

Python operators are the symbol that are used to perform some operation on single or multiple operands and produces a result.

There are following types of operators in Python:

- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators

##### Python Arithmetic Operators

Python Arithmetic operators are used to perform arithmetic/mathematical operations on the operands.

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

+ | Addition | 10 + 8 = 18 |

- | Subtraction | 10 – 8 = 2 |

* | Multiplication | 10 * 8 = 80 |

/ | Division | 10 / 8 = 1 |

% | Modulus (remainder) | 10 % 8 = 2 |

** | Exponent | 10**2 = 100 |

// | Floor division | -14/3 = -5 |

##### Python Relational Operators

Python comparison operator compares the two operands and return boolean true or false accordingly.

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

== | Is equals to | 10 == 5 (false) |

!= | Not equals to | 10 != 5 (true) |

> | Greater than | 10 > 10 (false) |

>= | Greater than or equals to | 10 >= 10 (true) |

< | Less than | 10 < 10 (false) |

<= | Less than or equals to | 10 <= 10 (true) |

<> | Not equals to | 10 <> 5 (true) |

##### Python Assignment Operators

Python assignment operators are used to assign value to variables.

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

= | Assign | var a = 10; a = 5; Now a = 5 |

+= | Add and assign | var a = 10; a += 5; Now a = 15 |

-= | Subtract and assign | var a = 10; a -= 5; Now a = 5 |

*= | Multiply and assign | var a = 10; a *= 5; Now a = 50 |

/= | Divide and assign | var a = 10; a /= 5; Now a = 2 |

%= | Modulus and assign | var a = 10; a %= 5; Now a = 0 |

**= | Exponent and assign | var a = 10; a **= 2; Now a = 100 |

//= | Floor Division and assign | var a = 10; a //= 5; Now a = 2 |

##### Python Logical Operators

Python logical operators combine two or more conditions and return boolean true or false accordingly.

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

and | Logical AND | (5 == 10 and 10 ==10) = false |

or | Logical OR | (5 == 10 or 10 ==10) = true |

not | Logical Not | not (5 == 10) = true |

##### Python Bitwise Operators

Python bitwise operators perform the bitwise operation on single or multiple operands and return the result.

Bit operators work on 32 bits numbers.

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

& | Bitwise AND | 5 == 10 & 20 == 20 (false) |

| | Bitwise OR | 5 == 10 | 20 == 20 (true) |

^ | Bitwise XOR | 5 == 10 ^ 20 == 20 (true) |

~ | Bitwise NOT | (~5) = (-5) |

<< | Bitwise Left Shift | (2 << 1) = 4 |

>> | Bitwise Right Shift | (2 >> 1) = 1 |

##### Python Membership Operators

Python membership operators are used to test for the membership of the value in a data structure. It returns true if the value is present in the data structure otherwise it returns false.

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

in | tests for presence of the variable | a in b; returns true if a is present in b |

not in | tests for absence of the variable | a not in b; returns true if a is absent in b |

##### Python Identity Operators

Python identity operators are used to compare the memory locations of 2 objects.

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

is | compares for same reference at both sides | a is b; returns true if id(a) and id(b) are equals |

is not | compares for different reference at both sides | a is not b; returns true if id(a) and id(b) are not equals |

##### Python Operator Precedence

The python operator precedence helps us to find which operator should be evaluated first. The operator precedence table is as follows:

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

** | Exponent |

~, +, - | Negation, unary plus and minus |

*, /, %, // | Multiplication, divide, modulus and floor division |

+, - | Binary plus and minus |

>>, << | Right shift and left shift |

& | Binary And |

^, | | Binary Xor and Or |

<, <=, >, >= | Comparison operators |

<>, ==, != | Equality operators |

=, +=, -=, *=, /=, %=, **=, //= | Assignment operators |

is, is not | Identity operators |

in, in not | Membership operators |

not and or | Logical operators |