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 platform 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 Python programing language. Operators are the foundation of any programming language. We have different types of operators in Python, such as Arithmetic, Assignment, Logical, Comparison (relational), Bitwise, Identity, and membership operators.
Before going into the details of Python Operators, let’s have a glance over some of the essential topics which could help us in a better understanding of the entire Operators topic.
What is an Operator in general?
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” is an operator in mathematics which executes the function called multiplication.
In Programming: Let's consider one of the popular operators used in computer programing languages, which is a Boolean operator used to express whether a statement is true or false.
What is meant by an Operator in Python?
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 over here.
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.
Types of operators in Python:
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.
- Arithmetic operators
- Comparison operators
- Assignment operators
- Logical operators
- Bitwise operators
- Membership operators
- Special operators
- Identity operators
- Membership operators
Related Article: String Formatting
Arithmetic operators are used for executing the mathematical functions in Python which includes, addition, subtraction, multiplication, division, etc.
Let us know the various Arithmetic operators and their purpose in Python from the below table.
|+Plus||Adds two Operands.||3+3= 6|
|-Minus||Right hand Operand 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 right hand one and also returns reminder.||6/2 = 3|
|// Floor division||Division that results into whole number adjusted to the left in the number line.||>>> 5.0 / 2
>>> 5.0 /2
|** Exponent||Left operand is raised to the power of right||10 to the power of 30|
Let’s examine different arithmetic operators with examples
1) Python (+) Addition operator
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.
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.
x + y = 30
2) Python (-) Subtraction operator
In python programing, the operator, Subtraction is used to execute a mathematical function which subtracts the right hand operand from the left hand one.
x = 10 y = 20 # output: x - y = - 10 print (‘x - y =’, x - y )
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.
x = 10 y = 20 # output: x * y = 200 print (‘x * y =’, x * y )
x * y = 200
4) Python (/) Division operator
Division operator is used to divide the left hand operand by right hand one.
x = 10
y = 20
i.e x/y (10/20 = 0.5 )
Let’s consider it in python way
x = 10 y = 20 # output: x / y = 0.5 print (‘x / y =’, x / y )
x / y = 0.5
5) Python (%) Percentail operator
The Percentile (%) is the string formatting operator in python, and it is also called as a interpretation operator. It divides the left hand operand by right hand one and also returns reminder. Ex: x/y
x = 20 y = 10 # output: x % y = 0. print (‘x % y =’, x % y )
x % y = 0
6)Python (//) Floor division operator
The Floor Operator is used to make a whole number adjusted to the left hand side in the number line.
Let's check the below program to know how a floor operator works in Python
x = 50 y = 20 # Output: x // y = 2 print('x // y =',x//y)
x // y = 2
Note:( In the above program, 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
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.
x = 10 y = 4 # output: x ** y = print (‘x ** y =’, x ** y )
x ** y = 10,000
Python Comparison Operators
The name itself is explaining 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 decides the relation between them. Comparison operator is also termed as 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.
- == Equal
- != Not Equal
- <> Two Operands are not equal
- > Greater Than
- < Less Than
- >= Greater Than or Equal to
- <= Less Than or Equal to
Before jumping into the details of comparison, let’s consider two values which could help us in understanding each one
Assume: x = 20, y = 25
1) Python (==) Equal Comparison Operator
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 below program.
x = 20 y = 25 # Output: x == y is False print('x == y is',x==y)
x == y is False
2) Python (!=) Not Equal Comparison Operator
This Not equal operator is used to compare two operands and returns True statement if they are Not equal, and False if they are Equal.
x = 20 y = 25 # Output: x != y is True print('x != y is',x!=y)
Subscribe to our youtube channel to get new updates..!
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 right one. If left hand operand is greater than 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
x = 20 y = 25 # Output: x > y is False print('x > y is',x>y)
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
x = 20 y = 25 # Output: x < y is True print('x < y is',x<y)
x < y is True
5) Python >= Greater Than or Equal to operator
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
x = 20 y = 25 # Output: x >= y is False print('x >= y is',x>=y)
x >= y. Hence it is False
6) Python <= Less than or equal to operator
Less than or equal to operator returns true if the right hand operator is greater than or equal to left hand one.
x = 20 y = 25 # Output: x <= y is True print('x <= y is',x<=y)
x <=y is True
Python Assignment Operators
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.
- = Assign Value
- += Add AND
- -= Subtract AND
- *= Multiply AND
- /= Divide AND
- %= Modulus AND
- **= Exponent AND
- //= Floor Division
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 right operand to the value of 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 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.
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
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.
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 in the right hand side.
x = 10, x**= 5 the result is x = 1,00,000
8) Python (//=) Floor Division
Floor division divides the value found in the left hand side with the value of right hand operand. The integer value becomes the left hand Operand value.
x = 7, x//= 4 the result is x = 1
Python logical Operators
Logical operators are used in any programming language to make decision based on multiple conditions. In python, we use Logical operators to determine whether a condition is True or False by taking Operand values as base. Let’s consider different logical operators that are used in python programming.
- And - Logical AND
- Or - Logical OR
- Not - Logical NOT
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)
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,
x = 10 y = 20 # Output: x or y is True print('x or y is',x or y)
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.
x = 10 y = 20 # Output: not x is False print('not x is',not x)
Ans: Not (a and b) is false.
Python Bitwise operators
Bitwise operators are used in python to perform the operations on binary numerals or bit patterns. It operates bit by bit.
- & Binary AND
- | Binary OR
- ^ Binary XOR
- ~ Binary Ones Complement
- << Binary Left Shift
- >> Binary Right Shif
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.
a = 10 b = 4 # Print bitwise AND operation print(a & b)
2) Python | Binary OR Bitwise Operator
Description: This operator performs bit by bit OR operations on the values on either side of the operator.
a = 10 b = 4 # Print bitwise OR operation print(a | b)
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.
a = 10 b = 4 # print bitwise XOR operation print(a ^ b)
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.
a = 10 b = 4 # Print bitwise NOT operation print(~a)
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.
a = 10 b = 4 # print bitwise left shift operation print(a << 2)
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)
Python Membership operators
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.
Related Article: Introduction to Python Programming
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 the specified element found in the list, otherwise it returns false.
Let’s consider a python program on how In operator works.
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 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
2) python Not in Membership Operator
“Not in” operator, the name itself is expressing the meaning that something is not inside. It goes through a particular sequence to find weather 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 Not In operator works:
# 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”
Line 1 - x is not there in given list. Hence True. Line 2 - y is there in given list. Hence False.
Python Identity operators
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) 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 Identity operator
This operator returns 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.
x = 6 if (type(x) is int): print ("true") else: print ("false")
2) Python Is not Identity operator
It will return false if either side of the operator pointing 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")
Python Operators FAQ’s
1) What are the operands that are permitted with logical operators in Python?
Ans: We have three different logical operators which 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 has the highest precedence in python.
6) Which operator has the lowest precedence 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 True statement if they are Not equal, and False if they are Equal.
9) What does <> mean in Python?
Ans: != or <> both symbols are used for expressing the not equal.
<> It returns 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 an 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!
If you are interested to learn Python and to become an Python Expert? Then check out our Python Certification Training Course at your near Cities.
These courses 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.