Skip to content

Python Operators [Everything You Need to Know]

Python Operators | understanding operators with images

Learning a new syntax or other complex stuff in any programming language is great!

But,

It would become useless if you are not able to solve real world problems.

Because,

The Ultimate goal of any programmer is to solve real world problems.

So,

In this article, You are going to learn about one of the fundamental tool through which you can solve any complicated problem within a second.

And,

The tool which I am talking about are Python Operators.

Along with that, You’ll learn about their types, their syntax and you’ll also learn how to use them with examples.

So, Let’s dive right in!


What are Operators in Python?

Python Operators are special symbols (characters) used to perform arithmetic or logical manipulations (calculations) on Operands.

Or,

In other words: Python Operators are special symbols which tells the python interpreter to carry out some mathematical or logical operations (calculations) on Operands.

Now, you might be wondering that,

What is an Operand?

An operand is a value or a variable on which the operator performs the operations.

It means:

  • Operands may be constants
  • Operands may be variables

But,

What are Arithmetic and Logical calculations?

Don’t Worry!

You will learn about Arithmetic and Logical calculations later on this article.

But, Before going into that,

Let’s Understand what actually Operators and Operands are with the help of an example.

Example:

# addition of two numbers (this complete line is a comment in Python, which will not be executed by the Python interpreter)

3 + 5

Or,

x = 3
y = 5

x + y

Output:

     8    

In the above example, The symbol (+) is the operator that performs addition, Values 3 and 5 and variables x and y are the operands and 8 is the output ( i.e., result) of the operation .


Types of Operators in Python

Python language has 7 different types of operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical (Boolean) Operators
  • Bitwise (Binary) Operators
  • Identity Operators
  • Membership Operators

Arithmetic Operators:

Arithmetic Operators are used to perform mathematical operations on numeric values, such as:- Addition, Subtraction, Multiplication, Division and many more…

Operator DescriptionExample
+ Addition: Add two operands or a unary plus2+7 = 9

Unary Plus means: either- +(any variable) or +(any value)
Subtraction: Subtract right hand operand from the left hand operand or a unary minus6-8 = -2

Unary Minus means: either- (any variable) or (any value)
* Multiplication: Multiply two operands3*2 = 6
/ Float Division: Gives quotient when left hand operand is divided by the right one.
The result always has type float.
5/2 = 2.5
// Floor (integer) Division: Gives quotient (but digits after the decimal points are removed) when left hand operand is divided by the right one. 7//2 =3
% Modulus: Gives the remainder of the division of left hand operand by the right one7%3 =1
** Exponent: Left hand operand raised to power of right hand operand4**3 =64

Assignment Operators:

Assignment Operators are used to assign values to variables.

Then,

What does assigning a value to a variable mean?

It simply means that giving or storing a value to a variable.

Example:

x = 2

Here we are assigning 2 to the variable x with the help of an assignment operator (=).

So,

Assigning a value to a variable in Python is so simple, You just have to write the variable name on the left hand side of equal to operator (=) and the value on the right hand side, as shown above.

OperatorDescriptionExample
=Assigns value to right hand side to an operand (i.e., variable) on left siden = 8
m = 2
+=Add right side operand with left side operand and assign the result to left operand n+= m
it is same as:
n = n+m
-=Subtract right side operand with left side operand and assign the result to left operandn-= m
it is same as: n = n-m
*=Multiply right side operand with left side operand and assign the result to left operandn*= m
it is same as: n = n*m
/=Divide left side operand with right side operand and assign the result to left operandn/= m
it is same as: n = n/m
//=Performs Floor division on Operands and assign the result to left operandn//= m
it is same as: n = n//m
%=Takes modulus using left and right side operands and assign result to left operandn%= m
it is same as: n = n%m
**=Calculate exponent (i.e., Left hand operand raised to power of right hand operand) and assign result to left operandn**= m
it is same as: n = n**m
&=Performs Bitwise AND (we will learn about Bitwise Operators later on this article) on both side of operands and assign result to left operandn&= m
it is same as: n = n&m
|=Performs Bitwise OR on operands and assign result to left operandn|= m
it is same as: n = n|m
^=Performs Bitwise XOR on operands and assign result to left operandn^= m
it is same as: n = n^m
>>=Performs Bitwise Right Shift on operands and assign result to left operandn>>= m
it is same as: n = n>>m
<<=Performs Bitwise Left Shift on operands and assign result to left operandn<<= m
it is same as: n = n<<m

Comparison (Relational) Operators:

Comparison Operators are used to compare values on either side of the operator and find out the relationship between them.

These are also called as relational operators.

These operators basically return True or False according to the condition.

Which means:- It return True if the condition is satisfied otherwise it return False.

OperatorDescriptionExample
==Equal to: Returns True, If the values or variables on either side of the operator are equalx = 5
y = 8

x == y
False
!=Not Equal to: Returns True, If the values or variables on either side of the operator are not equalx = 5
y = 8

x != y
True
>Greater than: Returns True, If left hand side operand (value or variable) is greater than the right onex =10
y = 8

x > y
True
<Less than: Returns True, If left hand side operand is less than the right onex = 3
y = 8

x < y
True
>=Greater than or equal to: Returns True, If left hand side operand is greater than or equal to the right onex =10
y = 8

x >= y
True
<=Less than or equal to: Returns True, If left hand side operand is less than or equal to the right onex =10
y = 8

x <= y
False

Logical (Boolean) Operators:

Logical Operators are used to combine True or False values of variables, statements or any other expressions to carry out their resultant truth value.

OperatorDescriptionExample
orLogical OR: Returns True, if either of the operands is truea = 2
b = 5

a > 1 or b < 6
True
andLogical AND: Returns True, if both the operands are truea = 28
b = 10

a < 30 and b >5
True
notLogical NOT: Returns False, if the operand is True (i.e., complements of the operand).
This operator simply reverse the result.
a = True

not a
False

Bitwise (Binary) Operators:

Bitwise Operators are used to perform bitwise calculations on integers.

Yes, Only integers Python Bitwise Operators work only on integers.

These are also called as Binary Operators.

Now,

You might be wondering that what does bitwise calculation means?

It simply means that operations (calculations) that involves working with individual bits,

Bits are the smallest units of data in a computer. Each bit has a single binary value: 0 or 1.

Bitwise operators consider operands (integers) as sequences of binary digits (i.e., 0 and 1),

The integers are first converted into binary format and then operations are performed bit by bit, Then, the result is returned back in decimal (integers) format.

Example:

Let’s consider two Variable named X and Y which consists of two integer values as: X = 12 and Y = 13.

And,

we know that, binary of 12 = 0b1100 and binary of 13 = 0b1101,

And, If you don’t know How to convert a decimal number into binary then, You can visit this video.

OperatorDescriptionExample
&Bitwise AND: Returns 1, if both the bits are 1 otherwise returns 0X & Y

Output: 12 (0b1100)
|Bitwise OR: Returns 1, if either of the bits are 1 otherwise returns 0X | Y

Output: 13 (0b1101)
~Bitwise NOT: Reverse the bit, It means if one of the bit is 0 it will return 1 and if one of the bit is 1 it will return 0.~X
Output: -13

~Y
Output: -14
^Bitwise XOR: Returns 1, if one of the bit is 1 and other is 0 otherwise returns 0X ^ Y
Output: 1
>>Bitwise Right Shift: Returns a with the bits shifted to the right by b places. This is the same as //’ing a by 2**b.a = 9
b = 2

a >> b
Output:
2 (0b10)
<<Bitwise Left Shift: Returns a with the bits shifted to the left by b places (and new bits on the right-hand-side are zeros). This is the same as multiplying a by 2**b.a = 9
b = 2

a << b
Output:
36 (0b100100)

Let’s Understand all the above Operators one by one with examples:

Bitwise AND:

X YX & Y
100
000
010
111

Bitwise OR:

XYX | Y
101
000
011
111

Bitwise NOT:

X~X
10
01

Bitwise XOR:

XYX ^ Y
101
011
000
110

Bitwise Right Shift:

Bitwise Right Shift Operator in Python

Bitwise Left Shift:

Bitwise Left Shift Operator in Python

Identity Operators:

As the name indicates, Identity is related to the term identicalness, sameness which simply means that the quality or fact of [several specified things (here it is Operands )] being the same,

As well as, Identity is also related to id ( i.e., address) of the memory locations where the data (values) are stored in the memory.

So,

Basically,

Identity Operators are used to check whether two Operands (i.e., values or variables) are equal and also if they stored at the same memory location ( i.e., both have same memory address) or not.

OperatorDescriptionExample
is Returns True, if both the Operands are equal (identical) and both have the same memory locationa = 2
b = 2

a is b

Output: True
is notReturns True, if either the Operands or the memory location is not same x = (5,6,7)

y = (5,6,7)

x is not y

Output:
True

In the above example, We can see that the 1st (i.e., is operator) example gives the output as: True,

Because, the value of a and b are equal as well as they both have the same memory location (i.e., both the values are stored at the same memory address).

Let’s Check, It is true or not:

a = 2
b = 2

a is b 
True

id(a)    # id is used for printing the memory address of the variable (here it is a)
1443428288

id(b) 
1443428288

And,

In the 2nd (i.e., is not) example, The Output is again: True,

Because,

However, both the variables have the same values but they do not have stored at the same memory locations, which means they both have the different id (i.e., address) in the memory location.

Let’s Check:

x = (5,6,7)
y = (5,6,7)

x is not y
True

id(x)
62687368

id(y)
62686504    # id is not same as that of the id of x 

Membership Operators:

Membership Operators are used to check whether a value or a variable is a member of a particular sequence or not.

The sequence might be a string (i.e., sequence of characters), a list, a tuple, a set or a dictionary that you will learn about in later tutorials.

There are two membership Operators in Python:

  • in
  • not in

Let’s understand the above concept with the help of a real world example:

Suppose, a person named “John” belongs to a family named X and another person named “Harry” does not belongs to the family X.

You can think of families as a list of family members.

Here, We can write the list of John’s family members as: [ John, John’s Father, John’s Mother, John’s Brother, John’s Sister, … ]

And,

If we want to check who is not belonging to the family X then we can use either in or not in Operator.

I know you are thinking that we already know about who belongs to the family X and who’s not.

Then, What is the need for checking?

It’s just for the sake of better understanding about the membership operators that it is working or not.

So,

If the in Operator returns True then we will conclude that the person belongs to the family X otherwise not belonging to the family X.

Example:

"Harry" in X
False    # it means Harry is not belonging to the family X.

Or,

If the not in Operator returns True then we will conclude that the person does not belongs to the family X otherwise belonging to the family X.

Example:

"Harry not in X
True     # it means Harry is not belonging to the family X.
OperatorDescriptionExample
inReturns True if a value or a variable is found in the sequencename = JavaScript

Java in name
True
not inReturns True if a value or a variable is not found in the sequencelst = [ 2, 3.14, 9 ]
3.1479 not in lst
True

So, That’s it from my side

Now, I’d like to hear from you:

Did I Miss Anything?

Or,

Is this post helpful for you?

Let me know by leaving a quick comment below.


Share this Post

Leave a Reply

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