Learn more about Python Operators in Detail

Now in this we will learn about Python operator and how can we use Python operators in to perform operation such as addition , subtraction etc.

Python operators

Python Operators

Operators are used in any programming languages such as Java, C#, C++ or Python. so they are used to do or perform operation on values & variables such as we can add two variable i.e adding their values using addition operator see example below:

print(10 + 5)

Output

15

Types of Python Operators

In Python operators are divided into following category:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Identity operators
  6. Membership operators
  7. Bitwise operators

Now let’s discuss each of above operator with Python implementation:

Python Arithmetic Operators

Arithmetic operator are used whenever we need to do calculation like addition , multiplication , subtraction , division etc see below examples for more clarification:

x = 10
y = 2

print(x + y)   #addition
print(x - y)   #subtraction
print(x * y)   #multiplication
print(x / y)   #division
print(x % y)   #modulus
print(x ** y)  #same as 10*10---Exponentiation
print(x // y)  #floor division

#the floor division // rounds the result down to the nearest whole number

Output

12
8
20
5.0
0
100
5

Python Assignment Operators

Assignment operator are used whenever we need to assign some values to our variable or give some value etc, see below examples for more clarification:

a = 5 #Same As a = 5

a += 3 #Same As a = a + 3
print(a)

a -= 3 #Same As a = a - 3
print(a)

a *= 3 #Same As a = a * 3
print(a)

a /= 3 #Same As a = a / 3
print(a)

a%=3 #Same As a = a % 3
print(a)

a//=3 #Same As a = a // 3
print(a)

a **= 3 #Same As a = a ** 3
print(a)

Output

8
5
15
5.0
2.0
0.0
0.0

Python Comparison Operators

Comparison operator are used whenever we need to compare two values , see below examples for more clarification:

x = 5
y = 3

print(x == y)
#Equal operator
# returns False because 5 is not equal to 3

print(x != y)
#Not equal operator
# returns True because 5 is not equal to 3

print(x > y)
#Greater than operator
# returns True because 5 is greater than 3

print(x < y)
#Less than operator
# returns False because 5 is not less than 3

print(x >= y)
#Greater than or equal to operator
# returns True because five is greater, or equal, to 3

print(x <= y)
#Less than or equal to operator
# returns False because 5 is neither less than or equal to 3

Output

False
True
True
False
True
False

Python Logical Operators

Python Logical operator are used whenever we need to are used to combine conditional statements etc, see below examples for more clarification:

x = 5

print(x > 3 and x < 10)
# and operator Returns True if both statements are true
# returns True because 5 is greater than 3 AND 5 is less than 10

print(x > 3 or x < 4)
#or operator Returns True if one of the statements is true
# returns True because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)

print(not(x > 3 and x < 10))
#not operator Reverse the result, returns False if the result is true
# returns False because not is used to reverse the result

Output

True
True
False

Python Identity Operators

Python Identity operator are used whenever we need to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location etc, see below examples for more clarification:

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x


#is operator
print(x is z)

# returns True because z is the same object as x

print(x is y)

# returns False because x is not the same object as y, even if they have the same content

print(x == y)

# to demonstrate the difference betweeen "is" and "==": this comparison returns True because x is equal to y




#is not operator
print(x is not z)

# returns False because z is the same object as x

print(x is not y)

# returns True because x is not the same object as y, even if they have the same content

print(x != y)

# to demonstrate the difference betweeen "is not" and "!=": this comparison returns False because x is equal to y

Output

True
False
True
False
True
False

Python Membership Operators

Python Membership operator are used whenever we need to test if a sequence is presented in an object etc, see below examples for more clarification:

x = ["apple", "banana"]

print("banana" in x)
# in operator
# returns True because a sequence with the value "banana" is in the list


print("pineapple" not in x)
# not in operator
# returns True because a sequence with the value "pineapple" is not in the list

Output

True
True
Python Bitwise Operators

Python Bitwise Operators

Python Bitwise operator are used whenever we need to compare (binary) numbers etc, see below examples for more clarification:

print(6 & 3)



"""
The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0:

6 = 0000000000000110
3 = 0000000000000011
--------------------
2 = 0000000000000010
====================

Decimal numbers and their binary values:
0 = 0000000000000000
1 = 0000000000000001
2 = 0000000000000010
3 = 0000000000000011
4 = 0000000000000100
5 = 0000000000000101
6 = 0000000000000110
7 = 0000000000000111
"""

print(6 | 3)



"""
The | operator compares each bit and set it to 1 if one or both is 1, otherwise it is set to 0:

6 = 0000000000000110
3 = 0000000000000011
--------------------
7 = 0000000000000111
====================

Decimal numbers and their binary values:
0 = 0000000000000000
1 = 0000000000000001
2 = 0000000000000010
3 = 0000000000000011
4 = 0000000000000100
5 = 0000000000000101
6 = 0000000000000110
7 = 0000000000000111
"""


print(6 ^ 3)



"""
The ^ operator compares each bit and set it to 1 if only one is 1, otherwise (if both are 1 or both are 0) it is set to 0:

6 = 0000000000000110
3 = 0000000000000011
--------------------
5 = 0000000000000101
====================

Decimal numbers and their binary values:
0 = 0000000000000000
1 = 0000000000000001
2 = 0000000000000010
3 = 0000000000000011
4 = 0000000000000100
5 = 0000000000000101
6 = 0000000000000110
7 = 0000000000000111
"""

print(~3)



"""
The ~ operator inverts each bit (0 becomes 1 and 1 becomes 0).

Inverted 3 becomes -4:
 3 = 0000000000000011
-4 = 1111111111111100

Decimal numbers and their binary values:
 4 = 0000000000000100
 3 = 0000000000000011
 2 = 0000000000000010
 1 = 0000000000000001
 0 = 0000000000000000
-1 = 1111111111111111
-2 = 1111111111111110
-3 = 1111111111111101
-4 = 1111111111111100
"""

print(3 << 2)



"""
The << operator inserts the specified number of 0's (in this case 2) from the right and let the same amount of leftmost bits fall off:

If you push 00 in from the left:
 3 = 0000000000000011
becomes
12 = 0000000000001100

Decimal numbers and their binary values:
 0 = 0000000000000000
 1 = 0000000000000001
 2 = 0000000000000010
 3 = 0000000000000011
 4 = 0000000000000100
 5 = 0000000000000101
 6 = 0000000000000110
 7 = 0000000000000111
 8 = 0000000000001000
 9 = 0000000000001001
10 = 0000000000001010
11 = 0000000000001011
12 = 0000000000001100
"""

print(8 >> 2)



"""
The >> operator moves each bit the specified number of times to the right. Empty holes at the left are filled with 0's.

If you move each bit 2 times to the right, 8 becomes 2:
 8 = 0000000000001000
becomes
 2 = 0000000000000010

Decimal numbers and their binary values:
 0 = 0000000000000000
 1 = 0000000000000001
 2 = 0000000000000010
 3 = 0000000000000011
 4 = 0000000000000100
 5 = 0000000000000101
 6 = 0000000000000110
 7 = 0000000000000111
 8 = 0000000000001000
 9 = 0000000000001001
10 = 0000000000001010
11 = 0000000000001011
12 = 0000000000001100
"""

Output

2
7
5
-4
12
2

Link: https://Codelikechamp.com

Medium Link: Follow me on Medium

Linkedin Link: Follow me on Linkedin

🤞 Don’t miss any latest posts!

Please subscribe by joining our community for free and stay updated!!!

IF YOU HAVE ALREADY SUBSCRIBED JUST CLOSE THIS FORM !

Leave a Comment

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

Scroll to Top