Operators in Python

In Python, we have a set of special symbols that perform various kinds of operations such as logical operations, mathematical operations, and more. These symbols are called Python operators. For every symbol or operator, there is a unique kind of operation. The values on which the operators perform their respective operations are known as operands. In this module, we will learn all about operators that we need to know in order to get started with them.

Watch this video on Python Operators:

Following is the list of topics that we will cover in this module.

So, without any further delay, let’s get started.

Types of Operators in Python

Depending on the type of operations that the operators perform, they are categorized into the following categories:

  • Arithmetic Operators in Python
  • Relational Operators in Python
  • Assignment Operators in Python
  • Logical Operators in Python
  • Membership Operators in Python
  • Identity Operators in Python
  • Bitwise Operators in Python

Now that we know what the different kinds of operators are in Python, let’s discuss each one of them individually, starting with arithmetic operators.

Become a professional Python Programmer with this complete Python Training in Singapore!

Arithmetic Operators in Python

Arithmetic operators are used to perform various mathematical operations such as addition, subtraction, etc. The following table contains all the arithmetic operations and their descriptions, along with examples.

Arithmetic OperatorOperator NameDescriptionExample
+AdditionPerforms additionI=40, J=20
>>>I+ J
>>>60
SubtractionPerforms subtractionI=40, J=20
>>>I – J
>>>20
*MultiplicationPerforms multiplicationI=40, J=20
>>>I * J
>>> 800
/DivisionPerforms divisionI=30, J=20
>>>I /J
>>> 2.5
%ModulusReturns the remainder after the divisionI=40, J=20
>>>I /J
>>> 0
**ExponentPerforms exponential (power) calculationI=4, J=20
>>>I /J
>>> 204
//Floor DivisionPerforms division, removes the decimal value, and returns the quotient valueI=30, J=20
>>>I//J
>>> 1

Relational Operators in Python

They are also known as comparison operators because they compare the values on both sides of the operator and conclude on the relation between the values. After comparison, it returns the Boolean value, i.e., either true or false. The following table contains different types of comparison operators and their descriptions, along with respective examples.

OperatorOperator NameDescriptionExample
==Equal toIf values of two operands are equal, then it returns true.I = 20, J = 20
(I == J) is True
!=Not Equal toIf values of two operands are not equal, then it returns true.I = 20, J = 20
(I == J) is False
Less thanIf the value of the left operand is less than the value of the right operand, then it returns true.I = 40, J = 20
(I < J) is False
>Greater thanIf the value of the left operand is greater than the value of the right operand, then it returns true.I= 40, J = 20
(I > J) is True
<=Less than or equal toIf the value of the left operand is less than or equal to the value of the right operand, then it returns true.I = 40, J = 20
(I <= J) is False
>=Greater than or equal toIf the value of the left operand is greater than or equal to the value of the right operand, then it returns true.I = 40, J = 20
(I >= J) is True
<> Not equal to (similar to !=)If values of two operands are not equal, then the condition becomes true.I=40, J = 20
(I <> J) is True.

Go for this in-depth job-oriented Python Training in Hyderabad now!

Assignment Operators in Python

Assignment operators are used to assign values to Python Variables. Assignment is sometimes done directly, and sometimes the operator first performs some kind of mathematical operation and then assigns the value to the operand. Following table contains all types of assignment operators with their description and respective examples.

OperatorOperator NameDescriptionExample
=AssignmentIt assigns a value from the right-side operand to the left-side operand.I = 40
It assigns 40 to I
+=Add then assignIt performs addition, and then the result is assigned to the left-hand operand.I+=J
that means I = I + J
-=Subtract then assignIt performs subtraction, and then the result is assigned to the left-hand operand.I-=J
that means I = I – J
*=Multiply the assignIt performs multiplication, and then the result is assigned to the left-hand operand.I*=J
that means I = I * J
/=Divide then assignIt performs division, and then the result is assigned to the left-hand operand.I/=J
that means I = I / J
%=Modulus then assignIt performs modulus, and then the result is assigned to the left-hand operand.I%=J
that means I = I % J
**=Exponent then assignIt performs exponent, and then the result is assigned to the left-hand operand.I**=J
that means I = I ** J
//=Floor division then assignIt performs floor division, and then the result is assigned to the left-hand operand.I//=J
that means I = I // J

Logical Operators in Python

Logical operators are mainly used for conditional statements. There are three types of logical operators, namely, AND, OR, and NOT. The following table contains all logical operators with their descriptions, as well as their respective examples.

OperatorOperator NameDescriptionExample
andLogical ANDWhen both sides’ conditions are true, the result is true; otherwise false.2<1 and 2<3
False
orLogical ORWhen at least one condition is true, then result is true; otherwise false.2<1 or 2<3
True
notLogical NOTReverse the conditionNot (5>4)
False

We have the perfect professional Python Course in Bangalore for you!

Membership Operators in Python

Membership operators are used to test if a value is available in a sequence or not. It can be any sequence such as (Python String, Python List, Python Set, Python Tuple, and Python Dictionary). There are two types of membership operators, namely, in and not in. The following table contains the description and the respective examples of both membership operators.

OperatorDescriptionExample
inIt returns true if it finds a variable in the sequence; otherwise false.List = [1,2,3,4,5,6,7,8]
i=1
if i in List:
print(‘i is available in list’)
else:
print(‘i is not available in list’)
Output – i is available in list
not inIt returns true if it does not find a variable in the sequence; otherwise false.List = [1,2,3,4,5,6,7,8]
j=10
if j not in List:
print (‘j is not available in list’)
else:
print (‘j is available in list’)
Output – j is not available in list
Bitwise Operator in Python

It performs bit-by-bit operations.
For instance, suppose there are two variables,

I = 10 and J = 20

And their binary values are:

I = 10 = 0000 1010
J = 20 = 0001 0100

Now, let us see how bitwise operator in Python works.

OperatorOperator NameDescriptionExample
&Binary ANDIf both bits are 1, then 1; otherwise 0.I & J
0000 0000
|Binary ORIf one of the bits is 1, then 1; otherwise 0.I | J
0001 1110
^Binary XORIf both bits are the same, then 0; otherwise 1I ^ J
0001 1110
~Binary ComplementIf the bit is 1 then makes it 0, and if the bit is 0 the makes it 1~I
1111 0101
<< Binary Left ShiftThe left operand is moved left by the number of bits specified by the right operand.I << 2
240 i.e. 1111 0000
>> Binary Right ShiftThe left operand is moved right by the number of bits specified by the right operand.I >> 2
15 i.e. 1111

Identity Operators in Python

Identity operators are used to compare the memory addresses of two different objects. The two types of identity operators in Python are is and is not. Following table contains the description of these two operators, along with their respective examples.

OperatorDescriptionExample
isIt returns true if both operands’ identities are the same; otherwise false.I = 20
J = 20
if(I is J):
print (‘I and J have same identity’)
else:
print (‘I and J have not same identity’)
Output – I and J have same identity
is notIt returns true if both operands’ identities are not the same; otherwise false.I = 20
J = 230
if(I is not J):
print (‘I and J have not same identity’)
else:
print (‘I and J have same identity’)
Output – I and J have not same identity

This brings us to the end of this module in python Tutorial. Here we talked about what Python operators are, types of Python Operators, arithmetic operators in Python, relational operators in Python, assignment operators in Python, logical operators in Python, membership operators in Python, bitwise operator in Python, identity operators in Python. Now, if you are interested in knowing how to implement data science concepts with Python, you can go through this blog on Python for Data Science.
Further, check out our offers for the Python certification course. You can also go through this free python Interview Questions prepared by industry experts.

Previous Next