Different types of operators in python

Operators means which are used to do perform tasks or operation on values and values. We have different types of operators in python

  1. Arithmetic Operators
  2. Logical Operators
  3. Assignment Operators
  4. Relational Operators
  5. Bitwise Operators
  6. Identity Operators
  7. Membership Operators 
Please find detailed  explanation below 

Arithmetic Operators:

By using Arithmetic operators we can perform basic operations like addition, subtraction, division, multiplications 
" + " Operator:
a=20
b=10
print(a+b)
OUTPUT:30
" - " Operator:
a=20
b=10
print(a-b)
OUTPUT:10
" * " Operator:
a=20
b=10
print(a*b)
OUTPUT:200
" / " Operator:
a=15
b=10
print(a/b)
OUTPUT:1.5
" // " Operator:
it removes values after point 
a=15
b=10
print(a//b)
OUTPUT: 1
" % " Operator:
it gives remainder value
a=15
b=10
print(a%b)
OUTPUT: 5
examples:

find 53?

a=5
print(a ** a)
or
print(5 ** 5)
OUTPUT: 125

Assignment Operators:

assigning 10 to variable a
All arithmetic operators used in assignment operators.
a=10
print(a)
adding some value to 'a' and assigning back to same variable in two ways
a=a+3
print(a)
or 
a+=3
print(a)
OUTPUT:13
a=10
a=a-2
or 
a-=2
print(a)
a=10
a=a/2
or 
a/=2
print(a)
a=10
a=a%2
or 
a%=2
print(a)
a=10
a=a**2
or 
a**=2
print(a)
Relational Operators:

Here it compares or relates between two variables or values
" == " Operator:
a=20
if a==20:
  print("here both vales are equal")
" < " Operator:
a=20
if a<30:
  print("correct")
" >" Operator:
a=20
if a>10:
  print("correct")
" < =" Operator:
a=20
if a<=20:
  print("correct")
" > =" Operator:
a=20
if a>=20:
  print("correct")
" !=" Operator:
a=20
if a!=10:
  print("correct")
Logical Operators:
in general to compare only one condition we go for relational operators
if we want to compare more conditions at a time then we go for logical operators
Compare which one grater value
" and" Operator: 
a=20,b=10,c=5
if a>b and a>c:
  print("a is bigger value")
elif b>a and b>c:
  print("b is bigger value")
else:
  print("c is greater value")
here both conditions true then only it will go for print statement

" or " Operator: 
if any one condition true then it will print true
a=20,b=30,c=5
if a>b and a>c:
  print("a greater than one value")
" not " Operator: 
it will converts  true to false and false to true

a=20,b=10,c=5
if not(a>b and a>c):
  print("a greater than one value")
else:
  print("not operators explanation")
OUTPUT: not operators explanation
a>b and a>c correct so True
not(True) ---- False

Membership Operators:
1.in
2.not in

" in " Operator:
a=20
list1=[6,7,8,9,20,30]
if (a in list1):
  print("a value present in list1")
OUTPUT: a value present in list1
a=40
list1=[6,7,8,9,20,30]
if (a in list1):
  print("a value present in list1")
else:
  print("a value not present in list1")
OUTPUT: a value not present in list1

"not in" Operator:
a=40
list1=[6,7,8,9,20,30]
if (a not in list1):
  print("a value not present in list1")
here 40 is not present in list so not  in operator writes true
OUTPUT: a value  not present in list1

a=20
list1=[6,7,8,9,20,30]
if (a not in list1):
  print("a not value present in list1")
else:
  print("a value  present in list1")
OUTPUT: a value present in list1

Identity Operators:
1.is
2.is not 
compares data types not value
"is "operator:
a=10
b=10
if (a is b):
  print(" two are equal")
else:
  print("Both are not equal")
OUTPUT: two are equal
a=10
b=20
if (a is b):
  print(" two are equal")
else:
  print("Both are not equal")
OUTPUT: two are  equal
a=10
b=10.0
if (a is b):
  print(" two are equal")
else:
  print("Both are not equal")
10.0 is float object so 
OUTPUT: Both are not equal
"is not " Operator:
a=10
b=10.0
if (a is not b):
  print(" two are equal")
else:
  print("Both are not equal")
OUTPUT: two are equal
a=10
b=10
if (a is not b):
  print(" two are equal")
else:
  print("Both are not equal")
OUTPUT: Both are not equal

Bitwise Operator:
1.&
2. |
here it compares values in binary format 
" & " Operator:
4  ----- 0100
5  ----- 0101
--------------
4&5--- 0100 ->4
8 ------ 1000
5  -----  0101
--------------
4&5--- 0000 ->0
" | " Operator:
4  ----- 0100
5  ----- 0101
--------------
4|5---- 0101 ->5
8 ------ 1000
5  -----  0101
--------------
4&5--- 1101 ->13


Comments

  1. I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.

    Data Engineering Services 

    Artificial Intelligence Solutions

    Data Analytics Services

    Data Modernization Services

    ReplyDelete

Post a Comment