Python Operators - Table of Content

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 Python Training !**

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 = 10

b = 4

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)

print(‘a**b = ’, a**b)

Here is the output for the above program,

a+b = 14

a-b = 6

a*b = 40

a/b = 2.5

a%b = 0.4

a//b = 2

a**b = 10000

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('a

print('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 True

a

a==b is False

a!=b is True

a>=b is True

a<=b is False

- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning

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.

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 = 10

b = 5

Print('a&b = ', a&b)

Print('a|b = ', a|b

Print('~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)

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 = 5

print("a = 5 is ", a)

a += 20

print("a += 20 is ", a)

a -= 5

print("a -= 5 is ", a)

a *= 6

print("a *= 6 is ", a)

a /= 2

print("a /= 2 is ", a)

a %= 4

print("a %= 4 is ", a)

The output for the above program will be as follows.

a = 5 is 5

a += 20 is 25

a -= 5 is 20

a *= 6 is 120

a /= 2 is 60.0

a %= 4 is 0.0

**Top 50 frequently asked Python interview Question and answers !**

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 = a

print("a is c = ", a is c)

print("a is b = ", a is b)

a = [1, 2]

b = [1, 2]

c = b

print("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 = True

a is b = False

a is not c = True

b is not c = False

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 = 3

print("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 = True

welcome in a = True

b not in a = True

2 not in a = False

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 = 8

b = 7

c = 4

d = 11

e = a + b * d / c

f = 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 is,

The value of a + b * d / c is 27.25

The 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 = 8

b = 7

c = 4

d = 11

e = (a + b) * d / c

f = (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.25

The value of (d - a) * b + (c / a) is 21.5

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:**

**Batch starts on 11th Mar 2021, Weekday batch****Batch starts on 15th Mar 2021, Weekday batch****Batch starts on 19th Mar 2021, Fast Track batch**

- Python Operators
- Python Partial Function
- Python Split Method
- Python Frameworks
- Python Generators
- Python List Length
- Running Scripts in Python
- Enumerate () in Python Tutorial
- Python Flask Tutorial
- Elastic Search Commands
- Apache Solr vs Elasticsearch
- Python VS JAVA
- Splunk vs ELK
- Elasticsearch Aggregations
- Elasticsearch Filters
- Elasticsearch Vs Mongodb
- Elasticsearch Pagination

- Alteryx Tools
- Tableau vs Alteryx
- MSBI Incremental load in SSIS
- Tableau Certification
- AWS vs Azure
- Tableau Version History
- Tableau Calculations
- Tableau vs Qlikview
- Tableau Parameters
- Data Blending
- Tableau Maps
- Tableau vs Spotfire
- Tableau Reporting
- Isolation Levels in SQL Server
- SQL Server Data Tools
- Tableau vs Power BI
- Alteryx Certification
- Looker vs Tableau
- Alteryx Vs MuleSoft
- Alteryx Workflow
- Alteryx Macros
- Looker vs Power BI
- Looker Data Sciences
- Alteryx Designer

- Salesforce Career Path
- SALESFORCE VS SERVICENOW
- Salesforce Lightning
- SALESFORCE CERTIFICATIONS
- Azure Data Factory
- Azure App Service
- Azure HDInsight
- Azure Analysis Services
- Microsoft Azure Portal
- Azure Cognitive Services
- Azure Traffic Manager
- Azure Application Insights
- Salesforce Marketing Cloud
- Azure Load Balancer
- Salesforce Governor Limits
- Salesforce Dashboard
- Salesforce Controllers
- Salesforce Apex Classes
- AWS Elasticsearch
- Salesforce Validation
- Process Builder in Salesforce
- Salesforce Architecture

- SCCM Deployment
- Workday Studio
- Workday Architecture
- Difference between SCCM and WSUS
- Trello Vs Jira
- How does Workday work?
- Workday Human Capital Management (HCM) Modules
- SCCM OVERVIEW
- SCCM Client Installation
- SCCM Certification
- Sitecore CMS
- Triggers in Salesforce
- What is Facets
- Oracle Cloud Financials
- Tosca Automation Tool
- Apigee Certification
- SCOM vs SCCM
- Sitecore Powershell
- Oracle Hyperion Essbase
- Sitecore Versions
- What is SCCM and Working
- Trizetto Facets
- Apigee Edge
- What is Sitecore
- What is Apigee