Python provides **Python Operators** as most languages do. Every programming language has operators. Operators are necessary to work with **logic **in a program. They tell the compiler or interpreter to perform some **mathematical**, **relational**, or **logical operations** and produce a result. These are special symbols to the language like **+**, **–**, *****, and **/**. They are like functions that take one or more argument and produces the result.

**Python has the following group of operators:**

**Arithmetic operators****Comparison operators****Assignment Operators****Logical Operators****Bitwise Operators****Membership Operators****Identity Operators**

## Arithmetic Operators

**Assume a=10, b=5**

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

+ (Addition) | It is used to add two operands. | a+b=15 |

– (Subtraction) | It is used to subtract the second operand from the first operand. | a-b=5 |

* (Multiplication) | Used to multiply two operands. | a*b=50 |

/ (divide) | Used to divide the first operand by the second and results in the quotient. | a/b=2.0 |

% (reminder) | Returns the remainder after dividing the first operand by the second operand. | a%b=0 |

** (Exponent) | Returns the result after calculating the first operand to the power of the second operand. | a**b=100000 |

// (Floor division) | It returns the floor value of the quotient produced by dividing the two operands. | a//b=2 |

## Comparison operators

**Comparison operators** are simple to understand as they return a boolean value i.e. **True **or **False**.

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

== | Condition becomes True if two operands are equal |

!= | Condition becomes True if two operands are not equal |

<= | Condition becomes True if the first operand is less than or equal to the second operand |

>= | Condition becomes True if the first operand is greater than or equal to the second operand |

> | Condition becomes True if the first operand is greater than the second operand |

< | Condition becomes True if the first operand is less than the second operand |

## Assignment Operators

**Assignment operators** are used to **assign **a calculated value from the right-hand side expression(after calculation) to the left-hand side operand.

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

= | Simply assigns right side expression to left side operand | a=5 |

+= | Adds left operand to right side operand and assigns the value back to left operand | a += b is same as a = a+b |

-= | Decreases left operand value by right operand value and assign value back to left operand | a -= b is same as a = a-b |

*= | Multiply the left operand value with the right operand value and assigns the multiplied value back to the left operand | a *= b is same as a=a*b |

%= | Divides left operand by right operand and return remainder which is assigned to left operand | a %= b is same as a=a%b |

**= | Increases left operand to power of right operand and assign the result to left operand | a **= b is same as a=a**b |

//= | It returns the floor value as it divides the left operand by the right operand and assigns the result to the left operand | a //= b is same as a=a//b |

## Logical Operators

Operator | Explanation |
---|---|

and | Let two expressions be a==>True, b==>True then the expression a and b will be True while if anyone from a and b is False then the expression will be False |

or | Let two expressions be a==>False, b==>False then the expression a and b will be False while if anyone from a and b is True then the expression will be True |

not | if a is True then not a is False and vice-versa |

## Bitwise Operators

The **bitwise operators** are used to perform **binary operations** on operands.

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

& (binary and) | If both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied |

| (binary or) | The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1 |

^ (binary xor) | The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit will be 0 |

~ (negation) | It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1, and vice versa |

<< (left shift) | The left operand value is moved left by the number of bits present in the right operand |

>> (right shift) | The left operand is moved right by the number of bits present in the right operand |

## Membership Operators

Using **Membership Operators**, you can check whether a value is present in a Python data structure or not. It returns **True **if the value is present in the Python data structure else returns **False**.

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

in | It returns True if the value is present in the data structure(list, tuple) |

not in | It returns False if the value is present in the data structure(list, tuple) |

## Identity Operators

The **identity operators** are used to decide whether an element certain class or type.

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

is | It is evaluated to be true if the reference present at both sides points to the same object |

is not | It is evaluated to be true if the reference present on both sides does not point to the same object |

## Python Operator Precedence

We now know all operators and their uses but when they are applied with one another then we should know their **precedence **otherwise the result may not be the desired one.

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

** | The exponent operator is given priority over all the others used in the expression. |

~ + – | The negation, unary plus, and minus. |

* / % // | The multiplication, divide, modules, reminder, and floor division. |

+ – | Binary plus, and minus |

>> << | Left shift. and right shift |

& | Binary and. |

^ | | Binary xor, and or |

<= < > >= | Comparison operators (less than, less than equal to, greater than, greater than equal to). |

<> == != | Equality operators. |

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

isis not | Identity operators |

innot in | Membership operators |

notorand | Logical operators |