In Python programming, different calculations can be carried out between values and variables using **python** **operators**. Operators help us to perform logical or/and arithmetic operations in the code. In this article, we are going to look at 6 different types of operators:

- Arithmetic Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Comparison Operators

## Arithmetic Operators

Arithmetic operators are helpful in performing basic mathematical operations. The table below illustrates some of the basic arithmetic operators that are used in day-to-day programming:

Operator | Information |

+ | Addition between variables |

– | Subtraction between variables |

/ | Divides two variables |

* | Multiplication between two variables |

** | Double asterisk is used to find the power to a number |

% | Modulus operator returns the remainder when the first variable is divided by the second |

### Try It Yourself

## Logical Operators

Logical operators are often used to check the state of variables or the existence of the same. Some of the most commonly used operators are listed in the table below.

Operator | Description |

logical and | returns True if both the variables are True |

logical or | returns True if any of the variables is True |

logical not | returns the opposite of the condition that the variable holds |

### Try it yourself

<iframe src=”https://trinket.io/embed/python/54516c1fde” width=”100%” height=”356″ frameborder=”0″ marginwidth=”0″ marginheight=”0″ allowfullscreen></iframe>

## Bitwise Operators

Bitwise operators are used when you work directly on ports and sockets of a microcontroller and/or embedded systems. These operators may look like basic logical operators, however they should not be interpreted as such. One must note that bitwise operators, as the name suggests, work on bits. This means that the decimal numbers are first converted into binary and then the operation is performed.

Operators | Description |

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise XOR |

<< | Bitwise left shift |

>> | Bitwise right shift |

~ | Bitwise NOT |

To understand bitwise operators, let us consider an example. Let us say that we are implemented bitwise AND between variables *a = 2 *and *b = 6*. These two variables are first converted in binary. Hence *a* will 00000010 and b will be 00000110. The bitwise AND operator compares every bit of *a* and *b*. It returns 1 if and only if both the bits are 1. So,

a | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |

b | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |

output | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |

Converting the output into decimal again, we ultimately get the answer of a & b as 2. The working principle of the other bitwise operators is similar. Feel free to go ahead and try it yourself.

### Try it yourself

## Assignment Operators

Assignment operators are help you to code efficiently and are not used to perform any function per se. For instance, if you want to update the value of a variable by adding 10 to it, you can do it simply by typing *x = x + 10* or use an assignment operator, which will then be *x += 10*. As seen, in assignment operator, you mention the symbol of arithmetic operation first, followed by the ‘=’ sign. Similarly, if you wish to multiply a variable by itself, you can do it as *x *= x*. As mentioned earlier, assignment operators are not unique functions, but are used to update variables. Hence, all the other operators seen above like arithmetic and bitwise operators can be used as assignment operators. An example of such would be *x >>= x*, where you operate bitwise right shift to the variable *x* on itself. Go ahead an try out some examples yourself.

## Comparison Operators

Comparison operators are dominantly used in *if…else* statements in Python programming. They control the flow of code and allow to run a code, only if the criteria between two variables are met. For example, if your have two variables *a *and *b* and what to run some line of code, only if *a* is greater than *b*, you can use the comparison operator *a >b*. Hence, comparison operator is used to compare the values of the variables involved. The table below illustrates comparison operators that are used in day-to-day programming:

Operators | Description |

> | greater than |

< | less than |

== | equals to |

!= | not equal to |

>= | greater than or equal to |

<= | less than or equal to |

Go head and try out some of the examples in the editor below:

## Conclusion

In this article, we saw some of the operators that make programming efficient and are used to perform useful operations. This article covered only a handful of operators that are commonly used in Python, however there are plenty of other operators. If you use some operator regularly, feel free to mention it down in the comments and share your experience.