Learning a new syntax or other complex stuff in any programming language is great!
It would become useless if you are not able to solve real world problems.
The Ultimate goal of any programmer is to solve real world problems.
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.
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.
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.
- Operands may be constants
- Operands may be variables
What are Arithmetic and Logical calculations?
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.
# addition of two numbers (this complete line is a comment in Python, which will not be executed by the Python interpreter) 3 + 5
x = 3 y = 5 x + y
“ 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 are used to perform mathematical operations on numeric values, such as:- Addition, Subtraction, Multiplication, Division and many more…
|+||Addition: Add two operands or a unary plus||2+7 = 9|
Unary Plus means: either- +(any variable) or +(any value)
|–||Subtraction: Subtract right hand operand from the left hand operand or a unary minus||6-8 = -2|
Unary Minus means: either- –(any variable) or –(any value)
|*||Multiplication: Multiply two operands||3*2 = 6|
|/||Float Division: Gives quotient when left hand operand is divided by the right one.|
The result always has type
|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 one||7%3 =1|
|**||Exponent: Left hand operand raised to power of right hand operand||4**3 =64|
Assignment Operators are used to assign values to variables.
What does assigning a value to a variable mean?
It simply means that giving or storing a value to a variable.
x = 2
Here we are assigning 2 to the variable x with the help of an assignment operator (=).
|=||Assigns value to right hand side to an operand (i.e., variable) on left side||n = 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 operand||n-= m|
it is same as: n = n-m
|*=||Multiply right side operand with left side operand and assign the result to left operand||n*= m|
it is same as: n = n*m
|/=||Divide left side operand with right side operand and assign the result to left operand||n/= m|
it is same as: n = n/m
|//=||Performs Floor division on Operands and assign the result to left operand||n//= m|
it is same as: n = n//m
|%=||Takes modulus using left and right side operands and assign result to left operand||n%= 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 operand||n**= 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 operand||n&= m|
it is same as: n = n&m
||=||Performs Bitwise OR on operands and assign result to left operand||n|= m|
it is same as: n = n|m
|^=||Performs Bitwise XOR on operands and assign result to left operand||n^= m|
it is same as: n = n^m
|>>=||Performs Bitwise Right Shift on operands and assign result to left operand||n>>= m|
it is same as: n = n>>m
|<<=||Performs Bitwise Left Shift on operands and assign result to left operand||n<<= 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.
|==||Equal to: Returns ||x = 5|
y = 8
x == y
|!=||Not Equal to: Returns ||x = 5|
y = 8
x != y
|>||Greater than: Returns ||x =10|
y = 8
x > y
|<||Less than: Returns ||x = 3|
y = 8
x < y
|>=||Greater than or equal to: Returns||x =10|
y = 8
x >= y
|<=||Less than or equal to: Returns ||x =10|
y = 8
x <= y
Logical (Boolean) Operators:
Logical Operators are used to combine
False values of variables, statements or any other expressions to carry out their resultant truth value.
|or||Logical OR: Returns |
|and||Logical AND: Returns |
|not||Logical NOT: Returns |
This operator simply reverse the result.
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.
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:
Bitwise operators consider operands (integers) as sequences of binary digits (i.e.,
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.
Let’s consider two Variable named X and Y which consists of two integer values as: X =
12 and Y =
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.
|&||Bitwise AND: Returns 1, if both the bits are 1 otherwise returns 0||X & Y|
||||Bitwise OR: Returns 1, if either of the bits are 1 otherwise returns 0||X | Y|
|~||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|
|^||Bitwise XOR: Returns 1, if one of the bit is 1 and other is 0 otherwise returns 0||X ^ Y|
|>>||Bitwise Right Shift: Returns ||a = 9|
b = 2
a >> b
|<<||Bitwise Left Shift: Returns ||a = 9|
b = 2
a << b
Let’s Understand all the above Operators one by one with examples:
|X||Y||X & Y|
|X||Y||X | Y|
|X||Y||X ^ Y|
Bitwise Right Shift:
Bitwise Left Shift:
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.
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.
|is||Returns True, if both the Operands are equal (identical) and both have the same memory location|
|is not||Returns True, if either the Operands or the memory location is not same|
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
In the 2nd (i.e., is not) example, The Output is again: True,
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.
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 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
set or a
dictionary that you will learn about in later tutorials.
There are two membership Operators in Python:
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, … ]
If we want to check who is not belonging to the family X then we can use either
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.
in Operator returns
True then we will conclude that the person belongs to the family X otherwise not belonging to the family X.
"Harry" in X False # it means Harry is not belonging to the family X.
in Operator returns
True then we will conclude that the person does not belongs to the family X otherwise belonging to the family X.
"Harry not in X True # it means Harry is not belonging to the family X.
|Returns ||lst = [ 2, 3.14, 9 ]|
So, That’s it from my side
Now, I’d like to hear from you:
Did I Miss Anything?
Is this post helpful for you?
Let me know by leaving a quick comment below.