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 operators. Following is the list of topics that we will cover in this module, in case you need to jump to a specific one.

So, without any further delay, let’s get started.
Watch this video on Python operators.

Python Operators 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

Types of Operators in Python

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

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

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

Arithmetic Operators

Arithmetic Operators are used to perform various mathematical operations such as addition, subtraction. Following table contains all the arithmetic operations and their description along with the examples.

Arithmetic OperatorsOperator NameDescriptionExample
+AdditionPerform AdditionI=40, J=20
>>>I+ J
>>>60
SubtractionPerform SubtractionI=40, J=20
>>>I – J
>>>20
*MultiplicationPerform MultiplicationI=40, J=20
>>>I * J
>>> 800
/DivisionPerform DivisionI=30, J=20
>>>I /J
>>> 2.5
%ModulusReturn the remainder after DivisionI=40, J=20
>>>I /J
>>> 0
**ExponentPerforms exponential (power) calculationI=4, J=20
>>>I /J
>>> 204
//Floor DivisionPerform division remove the decimal value and return Quotient valueI=30, J=20
>>>I//J
>>> 1

Relational Operators

It is also known as comparison operator because they compare the values on both sides of the operator and conclude out 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 the respective examples.

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

Assignment Operators

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

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

Logical Operators

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

OperatorOperator NameDescriptionExample
andLogical ANDWhen Both side condition is true the result is true otherwise false2<1 and 2<3
False
orLogical ORWhen at least one condition is true then result is true otherwise false2<1 or 2<3
True
notLogical NOTReverse the conditionNot (5>4)
False

Membership Operators

Membership operators are used to test if a value is available in a sequence or not. It can be any sequence such as string, list, set, tuple and dictionary. There are two types of membership operators namely, in and not in. The following table contains the description as well as their respective examples of both of the membership operators.

OperatorDescriptionExample
inIt returns true if it finds a variable in the sequence otherwise returns falseList = [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 returns falseList = [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 Operators

It performs bit by bit operation.
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 operators perform.

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

 

Identity Operators

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 operand ‘s identity is same otherwise falseI = 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 operand ‘s identity is not same otherwise falseI = 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. Next module highlights type conversion in Python. See you there!
Further, you can check our offers for Python Certification course.

1 thought on “Python Operators”

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
18 − 4 =