Python is one of the widely used programming languages in today's world. It has been used to write large, commercial-style applications, instead of just Banel ones. World-class companies that are using Python to develop their application and platforms are Google, Facebook, Instagram, Spotify, Quora, Netflix, Dropbox, Industrial Light, magic, YouTube, Google search engine, etc. In this tutorial, we are going to discuss a concept called “Operators” in the Python programming language. Operators are the foundation of any programming language. We have different types of operators in Python, such as Arithmetic, Assignment, Logical, Comparison Bitwise, Identity, and membership operators.
In general, an operator is defined as a Character used in Mathematics or in Programming to execute a specific function. For example, operators are used differently in different fields. Let's check out two of the fields here.
In Mathematics: Let's consider “x” as an operator in mathematics that executes the function called multiplication.
In Programming: Let's consider one of the popular operators used in computer programming languages, which is a Boolean operator used to express whether a statement is true or false.
Want to become a Python-certified professional, then enroll in our “Python Certification Training”. Our hands-on course will help you to achieve excellence in this domain. |
These are the special symbols in Python and are used to execute an Arithmetic or Logical computation. An operator alone cannot perform an activity, it needs an Operand. What is an operand? An Operand is a value that the operator needs to complete a task.
To understand it clearly, let’s consider an example here.
>>>10/2
5
Here, in the above example, we have used a Python Operator called / (Division). 10 and 2 are called as operands. The number 5 is called as output of the above execution.
Related Article: Python Tutorial |
We have multiple operators in Python, and each operator is subdivided into other operators. Let’s list them down and know about each operator in detail.
Related Article: String Formatting |
Arithmetic operators are used for executing the mathematical functions in Python which include, addition, subtraction, multiplication, division, etc.
Let us know the various Arithmetic operators and their purpose in Python from the below table.
Operator | Description | Example |
+Plus | Adds two Operands. | 3+3= 6 |
-Minus | Right-hand Operand is subtracted from the left-hand operand. | 20-10=10 |
* Multiplication | It Multiplies the values on either side of the operands. | 10*10 = 100 |
/ Division | left-hand operand divided by right-hand operand. | 50/5 = 10 |
% Percent | It divides the left-hand operand by the right-hand one and also returns a reminder. | 6/2 = 3 |
// Floor division | Division that results in the whole number being adjusted to the left in the number line. | >>> 5.0 / 2 2.5 >>> 5.0 /2 2.0 |
** Exponent | The left operand is raised to the power of the right | 10 to the power of 30 |
Let’s examine different arithmetic operators with examples
1) Python (+) Addition operator
The addition operator in Python is being used to add two operands (values) in Python programming. Look at the below program to know how it works.
Program
X = 10
Y = 20
# output: x + y = 30
print ( ‘x + y =’, x + y )
When you execute the above program, you will get the below output.
Run
x + y = 30
2) Python (-) Subtraction operator
In python programming, the operator, Subtraction is used to execute a mathematical function that subtracts the right-hand operand from the left-hand one.
Program:
x = 10
y = 20
# output: x - y = - 10
print (‘x - y =’, x - y )
Run
x - y = - 10
3) Python (*) Multiplication operator
It is used in Python programming to multiply the values and thereby to get output. Let's consider the below program to understand the multiplication operator.
Program
x = 10
y = 20
# output: x * y = 200
print (‘x * y =’, x * y )
Run
x * y = 200
4) Python (/) Division operator
The division operator is used to divide the left-hand operand by the right-hand one.
x = 10
y = 20
i.e x/y (10/20 = 0.5 )
Let’s consider it in Python way
Program
x = 10
y = 20
# output: x / y = 0.5
print (‘x / y =’, x / y )
Run
x / y = 0.5
5) Python (%) Percentail operator
The Percentile (%) is the string formatting operator in Python, and it is also called an interpretation operator. It divides the left-hand operand by the right-hand one and also returns a reminder. Ex: x/y
Program
x = 20
y = 10
# output: x % y = 0.
print (‘x % y =’, x % y )
Run
x % y = 0
6)Python (//) Floor division operator
The Floor Operator is used to make a whole number adjusted to the left-hand side of the number line.
Let's check the below program to learn how a floor operator works in Python
Program
x = 50
y = 20
# Output: x // y = 2
print('x // y =',x//y)
Run
x // y = 2
Note:( In the above program, the actual value that we get is 2.5, but to match the requirements of the Floor operator, we had to adjust it to the left side, i,e, 2.5 to 2)
7) Python (**) Exponent operator
The exponent operator is used in Python to perform exponential (power) calculations by using operators. Let's look into this concept with the help of a program.
Program
x = 10
y = 4
# output: x ** y =
print (‘x ** y =’, x ** y )
Run
x ** y = 10,000
The name itself explains that this operator is used to compare different things or values with one another. In Python, the Comparison operator is used to analyze either side of the values and decide the relation between them. The comparison operator is also termed a relational operator because it explains the connection between them.
We have different Comparison (Relational) operators. Let’s list them down and get to know each operator in detail.
Before jumping into the details of comparison, let’s consider two values that could help us understand each one
Assume: x = 20, y = 25
1) Python (==) Equal Comparison Operator
The equal operator is used to compare the two values and to give the result whether they are equal or not. If both the values are equal, then it gives the result as True, if not False.
Let's consider the below program.
Program
x = 20
y = 25
# Output: x == y is False
print('x == y is',x==y)
Run:
x == y is False
2) Python (!=) Not Equal Comparison Operator
This Not equal operator is used to compare two operands and returns a True statement if they are not equal, and False if they are Equal.
Program
x = 20
y = 25
# Output: x != y is True
print('x != y is',x!=y)
Run:
x != y is true
Note: In the above program, the two values are not equal to one another, the Not equal operator is exclusively used to return the True statement if the values are not equal.
3) Python (>) Greater Than Operator
This operator is mainly used in Python to return true if the Left-hand value is greater than the right one. If the left-hand operand is greater than the right-hand operand, it returns True as the output.
Ex: 10 is > 8, then the answer becomes True
10 is > 12, then the answer becomes False
Program:
ex :
x = 20
y = 25
# Output: x > y is False
print('x > y is',x>y)
Run:
x > y is False
4) Python (<) Less Than Operator
This Operator compares the right-hand operator with the left-hand one and if the right-hand one is greater than the left-hand one, it returns True statement.
Ex: 10 is < 12, then the answer becomes True
10 is < 8, then the answer becomes False
Program:
x = 20
y = 25
# Output: x < y is True
print('x < y is',x<y)
Run:
x < y is True
5) Python >= Greater Than or Equal to the operator
The greater than operator is used in Python to evaluate to True if the left-hand operand is >= (greater than or equal) to right-hand one
Program:
x = 20
y = 25
# Output: x >= y is False
print('x >= y is',x>=y)
Run:
x >= y. Hence it is False
6) Python <= Less than or equal to the operator
Less than or equal to the operator returns true if the right-hand operator is greater than or equal to the left-hand one.
Program:
x = 20
y = 25
# Output: x <= y is True
print('x <= y is',x<=y)
Run:
x <=y is True
The assignment operator is used to assign value to the event, property, or variable. We use this operator in Python to assign values to variables. We have multiple Assignment operators. Let’s list them down and know things better.
1) Python (=) Assign value Operator
This operator assigns values to the left-hand operand from the right-side operand.
Example: 5 = 2+3
In the above example, the right-hand operands are 2 & 3 and have contributed to construct value 5.
2) Python (+=) Add AND Operator
It adds the value of the right operand to the value of the left-hand operand.
Let's consider an example; x = 10, x + = 5, which will return the value as 15 by adding the right side value to the left-hand one. Therefore, the answer is x= 15.
3) Python(-=) Subtract AND Operator
Subtract And operator (-=) subtracts the value of the left operand from the right side operand and returns the output.
x = 10, y -= 7, the answer is x = 3
Related Article: Python enumerate |
4) Python (*=) Multiply AND Operator
It multiplies the left-hand operand with the right-hand one and returns the value as output.
Example;
x = 10, x*=5, the answer is x = 50.
5) Python (/=) Divide AND Operator
Divide And operator divides the left-hand operand with the right-hand operand value and gives the result
Example;
x = 10, x/= 5 the result is x = 2
6) Python (%=) Modulus AND Operator
It divides the value of left-hand operand with the right-hand one and the reminder that we get through this task will be placed as a value of x.
Example;
x = 10, x%= 5 the result is x = 0
Related Article: Machine Learning with Python |
7) Python **= Exponent AND operator
It gives us the exponential value of the left-hand operand when raised to the power of the value found on the right-hand side.
Example;
x = 10, x**= 5 the result is x = 1,00,000
8) Python (//=) Floor Division
Floor division divides the value found on the left-hand side with the value of the right-hand operand. The integer value becomes the left-hand Operand value.
Example;
x = 7, x//= 4 the result is x = 1
Logical operators are used in any programming language to make decisions based on multiple conditions. In Python, we use Logical operators to determine whether a condition is True or False by taking Operand values as a base. Let’s consider different logical operators that are used in Python programming.
Related Article: Python List Methods |
1) Python and Logical AND operator
AND operator returns the logical value ‘True’ when two Operands are true; otherwise, it will return False.
Ex: variable A = 10, B = 20,
x = 10
y = 20
# Output: x and y is True
print('x and y is',x and y)
Output:
Ans: A and B is True
Related Article: Python Print |
2) Python or Logical OR operator
In this Logical OR operator, if any one of the two operands are non-zero, then it returns the True condition.
Ex: variable A = 10, B = 20,
Program
x = 10
y = 20
# Output: x or y is True
print('x or y is',x or y)
Output:
Ans: a or b is True
Related Article: Python exception handling |
3) Python not Logical NOT
Logical NOT is used to reverse the logical state of its operands.
Program
x = 10
y = 20
# Output: not x is False
print('not x is',not x)
Output:
Ans: Not (a and b) is false.
Bitwise operators are used in Python to perform the operations on binary numerals or bit patterns. It operates bit by bit.
Related Article: Regular Expression Operations |
1) Python & Binary AND Bitwise Operator
Description: This operator performs bit-by-bit AND operations on the values on either side of the operator.
Example Program
a = 10
b = 4
# Print bitwise AND operation
print(a & b)
Output: 0
Related Article: Frequently Asked Python Interview Questions |
2) Python | Binary OR Bitwise Operator
Description: This operator performs bit-by-bit OR operations on the values on either side of the operator.
Example Program
a = 10
b = 4
# Print bitwise OR operation
print(a | b)
Output: 14
3) Python ^ Binary XOR Bitwise Operator
Description: This operator performs bit-by-bit exclusive OR operations on the values on either side of the operator.
Example Program
a = 10
b = 4
# print bitwise XOR operation
print(a ^ b)
Output: 14
Related Article: Generators & Iterators in Python |
4) Python ~ Binary Ones Complement Bitwise Operator
Description: The one’s complement of a number’s binary is returned.
Example Program:
a = 10
b = 4
# Print bitwise NOT operation
print(~a)
Output: -11
Related Article: Lists concepts in Python |
5) Python << Binary Left shift Bitwise Operator
Description: The value to the left operator is shifted to the left as many times as the value on the right side of the operator.
Example Program
a = 10
b = 4
# print bitwise left shift operation
print(a << 2)
Output: 40
6) Python >> Binary Right shift Bitwise Operator
Description: The value to the left of the operator is shifted to the right as many times as the value on the right of the operator.
a = 10
b = 4
# print bitwise right shift operation
print(a >> 2)
Output: 2
Membership Operators are mainly used in Python to find whether a value or variable is present in a sequence (string, tuple, list, set, and directory). “In” and “Not In” are the two membership operators available in Python.
Let’s get to know each operator in detail.
1) Python In Membership Operator
This In operator is used in Python to evaluate if a particular value is there or not in a sequence. The In operator returns True if it is the specified element found in the list, otherwise, it returns false.
Let’s consider a Python program on how the In operator works.
Program
a = 10
b = 7
list = [1,2,3,4,5,6,7,8,9..)
If (a is in list):
print: line - 1 “a is presented in the list”
else
print: line - 1 “a is not presented in the list”
If (b is in list):
print: line - 2 “b is presented in the list”
else
print: line - 2 “b is not presented in the list”
When you are done with the execution of the above program, you will get the below output:
Line - 1- a is not available in the list. Hence False.
Line - 2- b is available in the list. Hence True
Related Article: Introduction to Python Programming |
2) python Not in Membership Operator
“Not in” operator, the name itself expresses the meaning that something is not inside. It goes through a particular sequence to find whether a value is in the specified list or not. If it finds there is no such value, it returns True, otherwise False.
Let’s consider a Python program on how the Not In operator works:
Program
# Not ‘in’ Operator
x = 23
y = 20
list = [10, 15,20, 30, 35, 40,45, 50];
if (x is not in list)
print: “ x is not available in the list”
else
print: “ x is available in the given list”
if (y is not in list)
print: “ y is not available in the given list”
else
print: “ y is available in the given list”
Run:
Line 1 - x is not there in given list. Hence True.
Line 2 - y is there in given list. Hence False.
We have two identity operators in Python namely is and is not. These are also called as special operators and are used to find if two identical values (or variables) are located on the same part of the memory or not. If two variables are equal, that does not mean they are identical. We have two different operators.
1) Python is an Identity operator
This operator returns a true statement if two identical operands reside in the same memory, otherwise false.
Let's look at the small program on how the 'is' is used in Python.
example
x = 6
if (type(x) is int):
print ("true")
else:
print ("false")
Output
True
2) Python Is not an Identity operator
It will return false if either side of the operator points towards the same thing, otherwise, it returns true.
Let's look at the program to know how Is not operator will work;
x = 4.5
If (type (x) is not int) :
Print (“true”)
else:
print ("false")
Output
True
1) What are the operands that are permitted with logical operators in Python?
Ans: We have three different logical operators that are supported by Python, and they are, and logical And, or logical OR, & not logical NOT.
2) What is the difference between “is” and “ == “ in Python?
Ans: == operator is used in Python to check for value equality of both the operands whereas is an operator is used to express whether the two operands are pointing towards the same object or not.
3) What does == mean in Python?
Ans: == is a Python comparison operator. It is mainly used to check whether the values of two operands are equal or not. If the value is equal, then the condition becomes true.
Related Article: Python For Data Science Tutorial For Beginners |
4) What is operator precedence in Python?
Ans: Operator precedence is a predetermined order used to resolve the problem called multiple operations at a single time. Every part is examined and addressed in a predefined manner. Parentheses are used to override the order of precedence, and some parts are evaluated after others.
5) Which operator has the highest precedence in Python?
Ans: () parentheses have the highest precedence in Python.
6) Which operator has the lowest precedence in Python?
Ans: Logical OR has the lowest precedence in Python.
7)What is Floor Division Python?
Ans: The Floor Operator is used to make a Division that results in the whole number adjusted to the left in the number line.
ex: When 5 is divided by 2, the answer is 2.5
When you apply floor division to it, it becomes 2 (left side adjustment).
Related Article: Top 20 Python Frameworks List |
8)Is there a "not equal" operator in Python?
Ans: Yes! We have Not equal operator in Python, and it is used to compare two operands and returns a True statement if they are not equal, and a False if they are Equal.
9) What does <> mean in Python?
Ans: != or <> Both symbols are used for expressing the not equal.
<> It returns a true statement if both the operands are false, otherwise false.
10)When programming in Python operators with the same precedence are evaluated in which manner?
Ans: When two Python operators have the same precedence, you use associativity to determine the order. Almost all operators have associativity of left to right. For example, multiplication and division have the same precedence. So, the operator on the left will be evaluated first.
In this blog, we have covered all the operators with example programs, and also how they are useful for programming in Python. Each operator is unique and performs a separate set of functions to make Python a successful programming language. This guide provides examples depicting the usage of each operator as well as answering commonly asked questions about Python. I hope this tutorial has helped you in gaining knowledge on different operators and their purpose. Happy learning!
Want to learn Python and to become a Python Expert? Then check out our Python Certification Training Course in your nearby Cities.
This course is are incorporated with Live instructor-led training, Industry Use cases, and hands-on live projects. This training program will make you an expert in Python and help you to achieve your dream job.
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
Name | Dates | |
---|---|---|
Python Training | May 21 to Jun 05 | View Details |
Python Training | May 25 to Jun 09 | View Details |
Python Training | May 28 to Jun 12 | View Details |
Python Training | Jun 01 to Jun 16 | View Details |
Vinod M is a Big data expert writer at Mindmajix and contributes in-depth articles on various Big Data Technologies. He also has experience in writing for Docker, Hadoop, Microservices, Commvault, and few BI tools. You can be in touch with him via LinkedIn and Twitter.
1 / 15
Copyright © 2013 - 2024 MindMajix Technologies