Python Operators

Hey Folks, in this module we will learn Python Operators and their types with their order of precedence. This is the most important basic concept which is necessary to be known. So let’s start.

Python operators

Python operators are used to perform various operations on different sets of variables and values. These operators once operate any two values at a time.

The use of multiple operators in a single set of equations leads to operator precedence.
Each operator has its own associativity as well as precedence. This order of precedence gives priority to each operator according to its level of precedence.

Precedence and Associativity

Some important python supported operator precedence and associativity.

ORDER OF
PRECEDENCE
OPERATORSASSOCIATIVITYDESCRIPTION
1()Left to rightParentheses
2**Right to leftExponentiation
3~, +,-Right to leftComplement, unary plus (positive), and minus (negative)
4*, /, %, //Left to rightMultiply, divide, modulo, and floor division
5+, –Left to rightAddition and subtraction
6>>, <<Left to RightRight and Left bitwise shift
7&Left to RightBitwise ‘AND’
8^, |Left to RightBitwise exclusive ‘OR’ and regular ‘OR’
9< =, <, >, >=Left to RightComparison operators
10<>, ==, !=Left to RightEquality operators
11=, %=, /=,*=, //=, +=,-=, **=Right to LeftAssignment operators
12Is, is notLeft to RightIdentity operators
13in, not inLeft to RightMembership operators
14or
not
and
Left to Right
Right to Left,
Left to Right
Logical operators

Type of Operators in Python

There are various operators used to perform various tasks in less time complexity, These are as follows:

Python Operators

Arithmetic Operators in Python

These operators are basically used to perform different mathematical operations.
For Example, we use the ‘+'(addition) operator for adding two or more values.
a = a ‘+’ c

To know arithmetic operators in depth. Let take two integer variables a and b of values
a= 10 and b= 20 of integer data type.

OperatorDescriptionExample
AdditionAn Addition operation(+) is used to add two or more values of the same or different data type.print(a+b)
Output: 30
MultiplicationThis operator, also known as multiplication operator(*), is which is also known as multiplication operator(*) used to multiply two or more values of the same or different data types.print(a*b)
Output: 200
DivisionThis operator, known as Division operator (/) used to divide the value on the left side which is dividend with the value on the right side i.e. divisor. It gives Quotient as a result.print(b / a)
Output: 2
ModulusThis operator is used to find the modulus of a given value. Modulus is the remainder that is formed during division. (%) operator helps to find out modulus during the division of the left side value with the value on the right side.print(b % a)
Output: 0
Floor DivisionThis operator is used for giving non-decimal values through division. It is represented as (//).print(a//b)
Output: 0
ExponentThis operator is used to perform an Exponential operation. It is represented by (a ** b) where “a” is the base number and “b” is the exponential number.print(2**3)
Output: 8

Comparison (Relational) Operators in Python

This operator is used to perform an operation in which some conditions are required which need to be satisfied, then further operation will be executed. It will always be either True(1) or False(0).

There are various conditional operators which are as follows:
Let’s take values X=2 and Y=4 of integer data type.

OperatorsSyntaxDescriptionResult
>X > YY is smaller than XFalse
<X < YY is larger than XTrue
! =X != YX is not equal to YTrue
==X == YX is equal to YFalse
> =X >= YY is smaller or equal to XFalse
< =X < = YY is larger or equal to XTrue

Assignment Operators in Python

This is a type of operator used to assign the value to variables. This is basically used to override the value of the operand. These are special Signs used for carrying logical, bitwise
Computation. These operators are as follows:

Let take A=5 and B=4 of integer data type.

OperatorsSyntaxDescriptionResult
+=a+=b (a=a+b)This operator is used to perform addition operation then assign value to left side operanda=9
=c=a+b
d=a
The operator used for assigning right expression to left side operandc=9
d=5
-=a-=b (a=a-b)The operator used to perform subtraction operation and then assign value to left operanda=1
*=a*=c (a=a*c)An operator used to perform a multiplication operation and then assign value to the left operanda=20
/=a/=b (a=a/b)An operator used to perform division operation and then assign value to the left operanda=1.25
//=a//=b (a=a//b)The operator used to perform floor division operation and then assign value to left operanda=1
%=a %=c (a=a % b)An operator used to perform modulus operation and then assign value to the left operanda=1
**=a**=b (a= a**b)The operator used to perform the exponential operation and then assign value to the left operanda=625

Logical Operators in Python

The operator which used to perform logical operations like AND, OR, NOT. Mostly used in different arithmetic operations and logical computations. These are as follows:
Let x=2 and y=3 of integer data type.

OperatorsSyntaxDescriptionResult
ANDX and YThe statement will be true when Both X and Y are truex false
NOTNOT YReciprocal of Y, i.e. if Y is true, then the statement is False or vice-versa.If X=true
Then, Not X:
True
ORX OR YThe statement will be true when either X or Y is truex True

Identity Operators in Python

This operator is used to identify and compare the values of two objects. There are two identity operators, which are:

OperatorDescriptionSyntaxExampleOutput
Is not:It returns false when both objects are unequal.A is not BA=[‘a’,’b’,’d’]
B=[‘a’,’b’,’d]
C=B
print(C is not B)
print(A is not B)
False
True(as both objects are different even if they have the same element)
Is:It returns True when both objects have the same values.A is BA=[‘a’,’b’,’d]
B=[‘a’,’b’,’d]
C=B
print(C is B)
print(A is B)
True(Object B is assigned to object C, therefore both objects are equal)
False

Bitwise Operators in Python

This operator is used for performing various logical binary computations.
These operators are as follows:

NameOperatorDescription
OR|It set the value 1 when both bits are valued 1 or 0
AND&It sets a value of 1 when either one of the bit values is 1.
XOR^It sets a value of 1 when one of the bit values is 1.
NOT~If the value is set as 1, then the output will be 0 or vice -versa.
Right Shift Bitwise Operator>>It will shift binary value from right to left
Left Shift Bitwise Operator<<It will shift the binary value from left to right

Membership Operators in Python

This operator is used to verify that a sequence of elements is present in the object, List, Tuple, or string.

There are two membership operators.

NameDescriptionExampleExample
inThis operator will return true when the element is present in the object.A=[‘a’,’b’,’d’]
print(‘a’ in A)
True(element ‘a’ is present in object A)
not inThis operator will return true when the element is not present in the object.A=[‘a’,’b’,’d’]
print(‘c’ not in A)
True(element ‘c’ is not present in object A)

In this module, we studied the use of Python operators with their precedence order. Hope you enjoy reading our content. To get a clear vision of Python’s importance, please check out How to Become a Python Developer. Happy Learning 🙂

Previous articlePython Namespace and Scope
Next articleTypes of Number Data Types in Python
I’m Alimam Miya, a passionate educator and blogger dedicated to providing accurate and reliable educational content. With experience in programming, technical education, and competitive exam preparation, I simplify complex topics into easy-to-learn resources. Through my blogs, I share free study materials, courses, and updates on exams like GATE, CAT, IIT-JEE, NEET, IPMAT, CLAT, and more to help students in their learning journey