DEV Community

Cover image for Python Foundation with Data Structures & Algorithms - Part 03
NirmaniWarakaulla
NirmaniWarakaulla

Posted on

Python Foundation with Data Structures & Algorithms - Part 03

Operators in Python

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Python language is rich in built-in operators and provides following types of operators.

Types of Operators

We have the following types of operators in Python programming −

Arithmetic operators
Assignment operators
Relational/Comparison Operators
Logical Operators
Bitwise Operators
Identity Operator
Membership Operator

Arithmetic Operator

Arithmetic operators are used with numeric values to perform common mathematical operations:

12.08.2021_17.12.51_REC

 a=int(input("Enter First Number:"))
 b=int(input("Enter First Number:"))
 print("Addition:",a+b)
 print("Subtraction:",a-b)
 print("Multiplication:",a*b)
 print("Division",a/b)
 print("Floor Division:",a//b)
 print("Modulus",a%b)
 print("Exponentiation:",a**b)
Enter fullscreen mode Exit fullscreen mode

Enter First Number:5
Enter First Number:2
Addition: 7
Subtraction: 3
Multiplication: 10
Division 2.5
Floor Division: 2
Modulus 1
Exponentiation: 25

Python Assignment Operators

Assignment operators are used to assign values to variables:

12.08.2021_17.15.46_REC

 a = 10
 print(a)
Enter fullscreen mode Exit fullscreen mode

10

 a = 10
 b = 20
 a = a + b
 print(a)
Enter fullscreen mode Exit fullscreen mode

30

 a = 10
 b = 20
 a += b
 print(a)
Enter fullscreen mode Exit fullscreen mode

30

Python Comparison/Relational Operators

Comparison operators are used to compare two values:

12.08.2021_17.18.24_REC

a=10
b=20
c = a < b
print(c)
Enter fullscreen mode Exit fullscreen mode

True

 a=10
 b=20
 c = a > b
 print(c)
Enter fullscreen mode Exit fullscreen mode

False

 a=10
 b=20
 c = a <= b
 print(c)
Enter fullscreen mode Exit fullscreen mode

True

 a=10
 b=20
 c = a >= b
 print(c)
Enter fullscreen mode Exit fullscreen mode

False

 a=10
 b=20
 c = a == b
 print(c)
Enter fullscreen mode Exit fullscreen mode

False

 a=10
 b=20
 c = a != b
 print(c)
Enter fullscreen mode Exit fullscreen mode

True

Python Logical Operators

Logical operators are used to combine conditional statements:

12.08.2021_17.23.08_REC

Logial AND

 x = 1
 print(x < 5 and x < 10)
Enter fullscreen mode Exit fullscreen mode

True

 print(10 and 20)
Enter fullscreen mode Exit fullscreen mode

20

Logical OR

 x = 1
 print(x < 5 or x > 4)
Enter fullscreen mode Exit fullscreen mode

True

 print(10 or 20)
Enter fullscreen mode Exit fullscreen mode

10

Logical NOT

 x = 1
 print(not(x < 5 or x > 4))
Enter fullscreen mode Exit fullscreen mode

False

 a=0
 print(not a)
Enter fullscreen mode Exit fullscreen mode

True

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

12.08.2021_17.28.57_REC

   a=10
   b=6
   print("Bitwise AND:",a&b)
   print("Bitwise OR:",a|b)
   print("Bitwise XOR:",a^b)
   print("Left Shift:",a<<2)
   print("Right Shift:",a>>2)
   print("1's Complement:",~a)
Enter fullscreen mode Exit fullscreen mode

Bitwise AND: 2
Bitwise OR: 14
Bitwise XOR: 12
Left Shift: 40
Right Shift: 2
1's Complement: -11

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

12.08.2021_17.31.12_REC

  l1=[1,2,3]
  l2=[1,2,3]
  print(l1==l2)
  print(l1 is l2)
Enter fullscreen mode Exit fullscreen mode

True
False

  l3=l2
  print(l2)
  print(l3)
  print(l2==l3)
  print(l2 is l3) 
Enter fullscreen mode Exit fullscreen mode

[1, 2, 3]
[1, 2, 3]
True
True

   del l2

   print(l3)
Enter fullscreen mode Exit fullscreen mode

[1, 2, 3]

Membership Operators

Membership operators are used to test if a sequence is presented in an object:

in - Evaluates to true if it finds a variable in the specified sequence
not in - Evaluates to true if it does not finds a variable in the specified sequence

12.08.2021_17.34.44_REC

  L=[12,353,545,2,45,67,89,23]
  n=2
  print(n in L)
Enter fullscreen mode Exit fullscreen mode

True

  print(n not in L)
Enter fullscreen mode Exit fullscreen mode

False

Top comments (0)