## 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.

## 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

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 Operators Operator Name Description Example + Addition Perform Addition I=40, J=20 >>>I+ J >>>60 – Subtraction Perform Subtraction I=40, J=20 >>>I – J >>>20 * Multiplication Perform Multiplication I=40, J=20 >>>I * J >>> 800 / Division Perform Division I=30, J=20 >>>I /J >>> 2.5 % Modulus Return the remainder after Division I=40, J=20 >>>I /J >>> 0 ** Exponent Performs exponential (power) calculation I=4, J=20 >>>I /J >>> 204 // Floor Division Perform division remove the decimal value and return Quotient value I=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.

 Operator Operator Name Description Example == Equal to If the values of two operands are equal, then then it returns true. I = 20, J = 20 (I == J) is True != Not Equal to If the values of two operands are not equal, then it returns true. I = 20, J = 20 (I == J) is False < Less than If the value of left operand is less than the value of right operand, then it returns true I = 40, J = 20 (I < J) is False > Greater than If the value of left operand is greater than the value of right operand, then it returns true I= 40, J = 20 (I > J) is True <= Less than or equal to If the value of left operand is less than or equal to the value of right operand, then it returns true I = 40, J = 20 (I <= J) is False >= Greater than or equal to If 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 true I=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.

 Operator Operator Name Description Example = Assignment It assigns value from right side operand to left side operand I = 40 It assigns 40 to I += Add then assign It performs addition and then result is assigned to left hand operand I+=J that means I = I + J -= Subtract then assign It performs subtraction and then result is assigned to left hand operand I-=J that means I = I – J *= Multiply the assign It performs multiplication and then result is assigned to left hand operand. I*=J that means I = I * J /= Divide then assign It performs division and then result is assigned to left hand operand I/=J that means I = I / J %= Modulus then assign It performs modulus and then result is assigned to left hand operand I%=J that means I = I % J **= Exponent then assign It performs exponent and then result is assigned to left hand operand I**=J that means I = I ** J //= Floor division then assign It performs floor division and then result is assigned to left hand operand I//=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.

 Operator Operator Name Description Example and Logical AND When Both side condition is true the result is true otherwise false 2<1 and 2<3 False or Logical OR When at least one condition is true then result is true otherwise false 2<1 or 2<3 True not Logical NOT Reverse the condition Not (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.

 Operator Description Example in It returns true if it finds a variable in the sequence otherwise returns 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 in It returns true if it does not find a variable in the sequence otherwise returns 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 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.

 Operator Operator Name Description Example & Binary AND If both bits are 1 then 1 otherwise 0 I & J 0000 0000 | Binary OR If one of the bit is 1 then 1 otherwise 0 I | J 0001 1110 ^ Binary XOR If both bit are same, then 0 otherwise 1 I ^ J 0001 1110 ~ Binary Complement If bit is 1 the make it 0 and if bit is 0 the make it 1 ~I 1111 0101 << Binary Left Shift The left operands is moved left by the number of bits specified by the right operand. I << 2 240 i.e. 1111 0000 >> Binary Right Shift The 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.

 Operator Description Example is It returns true if both operand ‘s identity is 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 not It returns true if both operand ‘s identity is not 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. 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”

1. Pranaya kumari says:

Super