# Python Operators

Python is a general-purpose programming language. It is very easy to work with. It has simple syntax rules which make the code readable by anyone. It is a booming programming language in the market right now. So many people are tending towards learning the language. To get started with learning this language, you have to get strong in basics. Operators are the base foundation for any programming language. Operators are used for arithmetic or logical computation on operands. They are referred to as special symbols that carry out the computation of data. So, when you are learning python, you must first learn about the operators that it supports. If you are a beginner in python coding, this blog will best suit you. In this post, you will be able to learn about all the different operators available in Python. We will also show the syntaxes and examples of the operators along with an explanation. Let us get started.

## Arithmetic operators

To perform calculations like addition, subtraction, division, etc. between two operands, we can use the arithmetic operators.The below table gives an explanation of all the arithmetic operators.

Become a python Certified professional  by learning this HKR!

Here is a small program that performs all the arithmetic operations on two variables. Let’s take two operands as an example, and see what the results are going to be when we use different operators on them.

`a = 10b = 4print(‘a+b = ’, a+b)print(‘a-b = ’, a-b)print(‘a*b = ’, a*b)print(‘a/b = ’, a/b)print(‘a%b = ’, a%b)print(‘a//b = ’, a//b)print(‘a**b = ’, a**b)Here is the output for the above program,a+b = 14a-b = 6a*b = 40a/b = 2.5a%b = 0.4 a//b = 2a**b = 10000`

### Comparison operators

To compare the values of two operands,we can use the comparison operators.The result of these operators is either true or false i.e. a boolean value.Let’s take look at all the available comparison operators.

Here is a program that contains all the comparison operators.

`a = 10;b = 4;print('a>b is ', a>b)print('aprint('a==b is ', a==b)print('a!=b is ', a!=b)print('a>=b is ', a>=b)print('a<=b is ', a<=b)The output for the above program will be as follows.a> b is Trueaa==b is Falsea!=b is Truea>=b is Truea<=b is False`

### Python Training Certification

• Lifetime LMS & Faculty Access
• Real-world & Project Based Learning

### Logical operators

To check if the operands satisfy a specific condition, we can use logical operators.It is mainly used for decision making.The following table explains the logical operators.

Here is the truth table for the ‘and’ operator.

Here is the truth table for the ‘or’ operator.

Here is the truth table for the ‘not’ operator.

If you want to Explore more about Python? then read our updated article - Python Tutorial.

### Bitwise operators

We can use bitwise operators to compare the numbers in binary format.They operate bit by bit on the binary format of the numbers.

Let’s consider a = 10 (0000 1010) and b = 5 (0000 1011) in binary formats as example.

`a = 10b = 5Print('a&b = ', a&b)Print('a|b = ', a|bPrint('~a = ', ~a) Print('a^b = ', a^b)Print('a>>1 = ', a>>1)Print('a<<1 a="" 1="" p="">The output for the above program will be as follows. Let me add the binary format for the output values for a better understanding.a&b = 10 (0000 1010)a|b = 13 (0000 1011)~a = 5 (0000 0101)a^b = 1 (0000 0001)a >> 1 = 5 (0000 0101)a << 1 = 22 (0001 0110)`

### Assignment operators

We can use assignment operators to assign values to variables. We can use the assignment operator along with an arithmetic operator. In this case, the arithmetic operation will be performed first, and then the value will be assigned to the variable.

The below program shows the usage of the assignment operators.

`a = 5print("a = 5 is ", a)a += 20print("a += 20 is ", a)a -= 5print("a -= 5 is ", a)a *= 6print("a *= 6 is ", a)a /= 2print("a /= 2 is ", a)a %= 4print("a %= 4 is ", a)The output for the above program will be as follows.a = 5 is 5a += 20 is 25a -= 5 is 20a *= 6 is 120a /= 2 is 60.0a %= 4 is 0.0`

### Identity operators

To check if two operands share the same memory location or not, we can use the identity operators.It doesn't mean that the two variables are the same, but they are of the same object.These come under the special operators’ category in python.

Here is a program that explains the identity operators.

`a = ["hello", "hi"]b = ["hello", "hi"]c = aprint("a is c = ", a is c)print("a is b = ", a is b)a = [1, 2]b = [1, 2]c = bprint("a is not c = ", a is not c)print("b is not c = ", b is not c)The following is the output for the above program.a is c = Truea is b = Falsea is not c = Trueb is not c = False`

### Membership operators

To check if a value or variable is found in a sequence or not, we can use membership operators.These come under the special operators’ category in python.

Here is a sample program that explains the usage of the membership operators.

`a = ["hello", "welcome"]b = "hello"print("b in a = ", b in a)print("welcome in a =", "welcome" in a)a = [1, 2]b = 3print("b not in a = ", b not in a)print("2 not in a = ", 2 not in a)The output of the above program is as follows.b in a = Truewelcome in a = Trueb not in a = True2 not in a = False`

#### Order of precedence of python operators

An expression might have more than one operator. An order of precedence guides the order in which the operations should get executed. In an expression, the operator with the highest precedence will get executed first, and then the lowest precedence operator will get executed. It is very important to learn the order of precedence of python operators. The following table lists the order of precedence of operators from highest to lowest.

Let’s take a look at an example with a simple program.

`a = 8b = 7c = 4d = 11e = a + b * d / cf = d - a * b + c / aprint(“The value of a + b * d / c is”, e)print(“The value of d - a * b + c / a is”, f)The output for the above program is,The value of a + b * d / c is 27.25The value of d - a * b + c / a is -44.5`

Parenthesis plays an important role in changing the precedence. If you want a certain operation to be performed first in your expression, wrap it in parenthesis. Let’s take the same program mentioned above and see how the output changes when parenthesis is used in the expressions.

`a = 8b = 7c = 4d = 11e = (a + b) * d / cf = (d - a) * b + (c / a)print('The value of (a + b) * d / c is', e)print('The value of (d - a) * b + (c / a) is', f)The output for the above program will be as follows.The value of a + b * d / c is 41.25The value of (d - a) * b + (c / a) is 21.5`

### Python Training Certification

Weekday / Weekend Batches

##### Conclusion

Python operators are the core fundamental concept.It is popular for a variety of operators that it provides.That is the reason python is the most preferable language by most of the developers. In this blog,we have discussed in detail about each of the python operators.

Now that you know about operators,you can start writing programs in python for your applications.Play around with the operators,use them in conditions.Create compound expressions with a combination of operators to master them.Remember the order of precedence and parenthesis to set priority for your calculations.Have fun with python coding!

Related Articles:

1. Python Partial Functions

2. Python Split Method

3. Python Frameworks

4. Python Generators

5. Python List Length

#### Find our upcoming Python Training Certification Online Classes

• Batch starts on 5th Feb 2023, Weekend batch

• Batch starts on 9th Feb 2023, Weekday batch

• Batch starts on 13th Feb 2023, Weekday batch