Operators are used for operations on various variables and constants. In another words, each Operator can hold two values on each side and it returns a value and this value depends on the type of Operator used, e.g., 10*2 = 20 (Here the sign * is an Operator), but the value will change if we use a โ+โ operator here. There are many types of different operators in Python that are supported, letโs have a look: –

ย Operators*Arithmetic*ย Operators*Logical*ย Operators*Bitwise*ย Operators*Identity*ย Operators*Membership*ย Operators*Assignment*Operators (Also known as Comparison Operators)*Relational*

We are going to take a look at each type one by one.

## Arithmetic Operators

Arithmetic operators helps to carry out mathematical operations are called Arithmetic Operators, e.g., +ย (Addition), – (Subtraction), * (Multiplication), etc.ย Here is a table of all Arithmetic Operators that are used in Python: –

Letโs see how these operators are used in Python –

Operator Name | Use of the Operator |
---|---|

Addition, + |
This operator adds two or more values or variables. |

Subtraction, - |
This operand subtracts the value of the right hand side from the value present in the left hand side. |

Multiplication, * |
Multiplies 2 or more variables. |

Division, / |
Divides an operand(numerator) by another operand(denominator) to return the quotient. |

Exponent, ** |
The right-side value becomes the exponent of the value that is on the left side. |

Modulus, % |
Returns the remainder of the division. |

Floor Division, // |
This operator divides the left side value by the value on the right side and then returns the smallest and closest integer, meaning that output will always be an integer and not a float. |

## Example

## Logical Operators

Logical Operators are very useful in codes where there are test cases present. Logical Operators include โandโ, โorโ, and โnotโ. Like most of the Operators, Logical Operators also have two operands except โnotโ, it can only perform operations only on a single operator. This is how these Operators work: –

**Operator Truth Tables**

Letโs assume there are two inputs namely โXโ and โYโ and an output named as โZโ.

**Truth table for โandโ operator**

X | Y | Z |
---|---|---|

False | False | False |

False | True | False |

True | False | False |

True | True | True |

**Truth Table for โorโ operator**

X | Y | Z |
---|---|---|

False | False | False |

False | True | True |

True | False | True |

True | True | True |

**Truth Table for โnotโ operator**

X | Not X |
---|---|

False | True |

True | False |

These operators are useful when their conditions present in a code. False means that the conditions are not satisfied and the code has to stop itself from executing and True means that the condition(s) has been satisfied and the code can continue its execution. Letโs take an example based on the truth tables above –

## Example

## Bitwise Operators

These operators are used to compare two binary digits. As we already know, 1 means True and 0 means False. Bitwise operators treat the operand values as binary digits, and the operation on binary numbers is performed only on one bit per time from both operands (if the operator has two operands), hence the name.

Operator | Name and Number of Operands | Description |
---|---|---|

& | AND (Two Operands) | Sets each bit to 1 if both bits are 1 |

| | OR (Two Operands) | Sets each bit to 1 if either bit is 1 |

~ | NOT (One Operand) | Calculates onesโ complement of the bit. |

^ | XOR (Two Operands) | Sets each bit to 1 if only one bit is 1 |

>> | Right Shift (Two Operands) | Shifts bytes to the right side and adds zeros to the left side. Number of zeros is equal to the right-side operand. |

<< | Left Shift (Two Operand) | Shifts bytes to the left side and adds zeros to the right side. Number of zeros is equal to the left side operand |

## Example

## Identity Operators

Identity operators are used to check whether two operands are pointing to the same memory location or not. If two operands are equal, this does not mean that they also belong to the same object and refer to the same memory location.

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

is | It will show True in the output If both operands are identical objects |

is not | It will show True in the output if the operands are unidentical objects. |

## Example

## Membership Operators

These operators are used to check whether an operand is present in a Sequence or not. The sequence can be List, String, etc.

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

in | This will return True if the operand is present in the Sequence |

not in | This will return True if the operand is not present in the Sequence. |

## Example

## Assignment Operators

These operators are used to assign values to some variable.

Operator and its Name | Description |
---|---|

=, Equal | This operator transfers the value of the right operand to the left |

+=, Add AND | This adds the operand on the right hand side to the left-side operand and assigns the final result to the left hand side operand. |

-=, Subtract AND | This subtracts the right hand side operand from the left hand side operand and assigns the final result to the left hand side operand. |

/=, Divide AND | It performs the divides the left operand by the left operand and assigns the final result to the left side operand. |

*=, Multiply AND | It multiplies both the operands and the final value is assigned to the left hand side operand. |

//=, Floor Division AND | It performs normal division first and then converts the float integer to the smallest and the closest integer and then assigns this value to the left hand side operand. |

**=. Exponent AND | This operator assigns the final value of the exponent calculation to the left hand side operand. |

%=, Modulus AND | It calculates and assigns the remainder of the division to the left hand side operator. |

## Example

## Relational/Comparison Operators

Using Relational operators, you can compare two values. They can either return True or False depending upon the test case.

Operator and its Name | Syntax |
---|---|

==, Equal | x == y |

>, Greater than | x > y |

<, Smaller than | x < y |

!=, Not Equal | x != y |

>=, Greater than or Equal to | x >= y |

<=, Smaller than or Equal to | x <= y |