Operators in Python
This tutorial will cover the basics and advance Python Operator with explanations and real-world examples. In Programming Language, Operators hold a special purpose of data manipulation and decision making part. By applying these operators you have assign values, compare variables, and also perform mathematical operators on them.
Table of Contents
- Types of Operators in Python
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Identity Operators
- Walrus Operator ( Introduced in Python 3.8 )
Post Update Timeline
Updated on : 20-07-2020
Added new topics such as Identity, Walrus Operator and also updated with new content etc.
Types of Operators in Python
There are different types of Operators that serve a specific purpose and here you’ll learn about six types of operators.
- Arithmetic Operators: This operator performs arithmetical operations of variables.
- Assignment Operators: This operator assigns values to variables.
- Comparison Operators: Compares values of two or more variables and returns a boolean value.
- Logical Operators: Performs logical AND, OR, and NOT operation on variables.
- Identity Operators:
- Bitwise Operators:
- Walrus Operator:
Arithmetic Operators
Operator Symbol | Operator Name | Description | Example |
---|---|---|---|
+ | Addition Operator | Adds values of operands | 5+4+3
|
– | Subtraction Operator | Substracts values from operands | 2-4-6
|
* | Multiplication Operator | Multiplies the value from operands | 5*4
|
/ | Division Operator | Divides the value from a given value | 10/2
|
% | Modulus Operator | Divides the value from a given value | 10%2
|
#Addition a=15 b=12 c=a+b print(c) #Subtraction a=10 b=3 c=a-b print(c) #Multiplication a=7 b=3 c=a*b print(c) #Division a=8 b=2 c=a/b print(c) #Modulus a=15 b=4 c=a%b print(c) #Exponential a=2 b=4 c=a**b # this means 2 to power of 4 (2^4) print(c) #Floor Division a=7 b=2 c=a//b #the division ans will be 3.5 but floor will round the value to 3 print(c) #PYTHON OUTPUT 27 7 21 4.0 3 16 3
Assignment Operators
Assignment Operators are used for assigning values to the variables. Consider value 3
to be assigned to a variable num
then here is the way you would assign it as a = 3
and the equal to the operator is called the assignment operator. List of Assignment Operators
Operator Symbol | Snippets |
---|---|
= | a=7 |
+= | a+=2 or a=a+2 |
-= | a-=2 or a=a-2 |
/= | a/=2 or a=a/2 |
%= | a%=2 or a=a%2 |
*= | a*=2 or a=a*2 |
# (=) Operator a=5 print(a) # (+=) Operator a=2 a+=4 print(a) # (-=) Operator a=5 a-=2 print(a) # (/=) Operator a=8 a/=2 print(a) # (%=) Operator a=15 a%=3 print(a) # (//=) Operator a=9 a//=4 print(a) # (**=) Operator a=2 a**=4 print(a) # (&=) Operator a=2 a&=2 print(a) # this are used the verify if the same variable exists in the memory # (|=) Operator a=8 a|=10 print(a) # (^=) Operator x=1 x^=4 print(x) # (>>=) Operator a=5 a>>=10 # (<<=) Operator a=3 a<<=3 print(a) #PYTHON OUTPUT 5 6 3 4.0 0 2 16 2 10 5 24
Comparison Operators
Comparison Operators are helpful in comparing the values of expressions inside statements. They only return boolean values. Some of the Comparision Operators are:
Operator Symbol | Name | Snippets |
---|---|---|
> | Greater than operator | 7>8 returns False |
< | Less than | 7<8 returns True |
>= | Greater than equals to | 8>=8 returns True |
<= | Less than equals to | 8<=8 returns True |
== | equalsTo Operator | 8==8 returns True |
!= | Not equals to Operator | 7!=8 returns True |
a=5 b=5 # equalsTo (==) Operator if a==b: print('a value is equal to b value') a=5 b=4 # not equalsTo (!=) Operator if a!=b: print('a value is not equal to be value') a=5 b=4 # Greater than (>) Operator if a>b: print('a value is greater the b value') a=5 b=6 # Lesser than Operator if a<b: print('a value is lesser than b value') a=4 b=4 # Greater than equalTo Operator if a>=b: print('a value is greater than and equal to b value') a=5 b=5 # Lesser than equalTo Operator if a<=b: print('a value is lesser than and equal to b value') #PYTHON OUTPUT a value is equal to b value a value is not equal to be value a value is greater the b value a value is lesser than b value a value is greater than and equal to b value a value is lesser than and equal to b value
Logical Operators
Logical Operators perform logical/analytical tasks on expressions and also for chaining multiple expressions together in a statement.
a=5 b=4 c=3 # and logical operator if a>b and a>c: print(a) # or logical operator if b>a or b>c: print(b) #PYTHON OUTPUT 5 4
Bitwise Operators
Bitwise Operator works with bits of operands and unlike other operators, it sees operands as binary digits. The bitwise operator can perform ~
Complement, &
AND, |
OR, <<
Left Shift and >>
Right, Shift operations on its operands.
~ Complement Operator
Complement operator reverses the operands binary digits and returns it number value. Consider the number 5
whose binary value is 0000 0101
and if you perform complement on this than the output will be 1111 1010
and if you do 2’s complement over this digit than you’ll get -6
as output.
print(~5) # returns -6 print(~10) # returns -11
Bitwise AND Operator
This operator does AND operation of binary digits. It works on the truth table which is given below.
A | B | Operation | Output |
---|---|---|---|
0 | 0 | 0*0 | 0 |
1 | 0 | 1*0 | 0 |
0 | 1 | 0*1 | 0 |
1 | 1 | 1*1 | 1 |
Here the output will be 1 when both values are 1. Let’s solve this by an example.
print( 2 & 3 ) #returns 2
Explanation
number Binary Digits 2 0000 0010 3 0000 0011 ------------------ Bitwise AND Operation 0000 0010 Output is 2
Bitwise OR Operator
This operator does the OR operation of binary digits. It works on the truth table which is given below.
A | B | Operation | Output |
---|---|---|---|
0 | 0 | 0+0 | 0 |
1 | 0 | 1+0 | 1 |
0 | 1 | 0+1 | 1 |
1 | 1 | 1+1 | 1 |
Here the output will be 1 when one of them or both values are 1. Let’s solve this by an example.
print( 2 | 3 ) #returns 3
Note
For Bitwise OR use pipe Symbol |.
Explanation
number Binary Digits 2 0000 0010 3 0000 0011 ------------------ Bitwise OR Operation 0000 0011 Output is 3
Bitwise Left Shift
This operator shifts the digits from the right-hand side to the left-hand side from the decimal point. In such case, the left side gains extra digits. Let’s solve this by an example.
print( 10 << 2 ) #returns 40
Explanation
number Binary Digits 10 0000 1010.00 40 0010 1000 Here the zeros from right are moved to left side
Bitwise Right Shift
This operator shifts the digits from the lefthand side to the righthand side from the decimal point. In such case the left side loose extra digits to the righthand side. Let’s solve this by an example.
print( 10 >> 2 ) #returns 2
Explanation
number Binary Digits 10 0000 1010.00 2 0000 0010.1000 Here the zeros from left are moved to right side
Identity Operators
This special Identity Operators are used to validate the two given values that exist in the same memory. The two identity operators are is
and is not
Example
a = 5 b = 5 c = {} d = {} e = [1] f = [1] g = [2] h = g print( a is b ) # True print( c is d ) # False print( e is f ) # False print( g is h ) # True
The a is b
returns True
because they are numbers with the same value. The c is d
, e is f
are dictionary and list so hence stored separately in memory so it returns False
. But g is h
returns True
because h
points to same memory location as g
.
a = -5 b = 5 print( a is not b ) # True a = 3 b = 2 a = b print( a is not b ) # False
Walrus Operator ( Introduced in Python 3.8 )
This operator was introduced in Python Version 3.8. Using this operator we can assign value to other variables inside expression and during conditional checking. Let us look at its syntax and then go further in detail.
Syntax
new_variable := old_variable
The operator is represented using symbol colon followed by equal to sign :=
. The value stored in old_variable
is assigned to new_variable
.
Example
num = 15 (age:=num) print("Ryan's age is {}".format(age)) #PYTHON OUTPUT Ryan's age is 15
Recent Posts




