Python Operators

Table Of Contents

  1. What Is A Python Operator?
  2. Types Of Python Operators.
  3. Arithmetic operators.
  4. Assignment operators.
  5. Comparison operators.
  6. Logical operators.
  7. Identity operators.
  8. Membership operators.
  9. Bitwise operators.

(1) What Is A Python Operator?

  • Python Operators in general are used to perform operations on different values and variables.
  • These are standard symbols used for the purpose of logical and arithmetic operations.
  • Operators are the pillars of a program on which the logic is built in a specific programming language.
  • In the example below, we use the + operator to add together two values:

Example

a = 30

b = 40

sum = a + b

print(sum)
Output:

70

(2) Types Of Python Operator?

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

(3) Arithmetic Operator

  • Arithmetic operators are used for doing mathematical operations on numeric values.
  • The arithmetic operations are given in the below table.

Example

#Example-1
a = 8; b = 7
print(a+b)

#Example-2
c = 9; d = 5
print(c-d)

#Example-3
d = 5; e = 4
print(d*e)

#Example-4
f = 8; g = 4 
print(f/g)

#Example-5
h = 7; i = 3 
print(h%i)

#Example-6
j = 2; k = 5 
print(j**k)

#Examlpe-7
l = 15; m = 2 
print(l//m)
Output:
15
4
20
2 
1 
32
7

(4) Assignment Operator

  • Assignment operators are used to assigning values to variables.
  • The assignment operations are given in the below table.

Example

#Example-1
a = 5
a += 6
print(a)

#Example-2
b = 6
b -= 3
print(b)

#Example-3
c = 3 
c *= 2 
print(c)

#Example-4
d = 10
d /= 2 
print(d)

#Example-5
e = 8
e %= 2 
print(e)

#Example-6
f = 7
f //= 2 
print(f)
Output:
11
3
6
0
3

(5) Comparison Operator

  • Comparison operators are used to comparing two values.
  • The Comparison operations are given in the below table.

Example

#Example-1
a = 5
b = 6
print(a==b)

#Example-2
c = 8
print(c != 6)

#Example-3
d = 10
e = 15
print(e > d)

#Example-4
f = 45
g = 14
print(g < f)

#Example-4
h = 7
i = 4
print(h>=i)

#Example-5
j = 4 
k = 6
print(j <= k)
Output:
False
True
True
True
True
True

(6) Logical Operator

  • Logical operators are used to combining conditional statements like Logical AND, Logical OR, and Logical NOT operations.

  • The Logical operations are given in the below table.

Example

#Example-1
a = 10
b = 15
if a<5 and b >5:
   print('True')
else:
   print('False')

#Example-2
c = 8 
d = 7 
if c<5 or d>5:
   print('True')
else:
   print('False')

#Example-3
e = 25
f = 45
if not(e>5 and f>5):
   print('True')
else:
   print('False')
Output:
False
True
False

(7) Identity Operators

  • Identity operators compare the memory locations of two objects. Whether they are pointing to the same memory location or not.
  • is and is not are the identity operators both are used to check if two values are located on the same part of the memory.
  • Two variables that are equal do not imply that they are identical.

Example

x1 = 10
y1 = 10
x2 = 'World'
y2 = 'World'
x3 = [9,8,7]
y3 = [9,8,7]

print(x1 is not y1)

print(x2 is y2)

print(x3 is y3)
Output:
False
True
False

Note:

  • Here, we see that x1 and y1 are integers of the same values, so they are equal as well as identical.
  • The same is the case with x2 and y2 (strings).
  • But x3 and y3 are lists. They are equal but not identical.
  • It is because the interpreter locates them separately in memory although they are equal.

(8) Membership Operators

  • ‘in’ and ‘not in’ are the membership operators in Python.
  • They are used to test whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).
  • If the value is present in the data structure, then the resulting value is true otherwise it returns false.
  • In a dictionary, we can only test for the presence of a key, not the value.

Example

x = 'Hello world'

y = {1:'a',2:'b'}

print('H' in x)

print('hello' not in x)

print(1 in y)

print('a' in y)
Output:
True
True
True
False

(9) Bitwise Operators

  • Bitwise operators convert the values into binary format and do the operation.
  • Bitwise operator works on bits and performs bit-by-bit operation, hence the name.

Example

a = 60

b = 13

#Binary Format

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

a << 2 = 240 (means 1111 0000)

a >> 2 = 15 (means 0000 1111)

Leave a Reply

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