Table of Contents
Operators in Python
Programs on Operators in python gives practical implementation of arithmetic, assignment, bit wise, membership, logical, identity and comparison operators.
Operators are special symbols that represent calculations and values which operator uses are called operands.
Arithmetic Operator
Practical a : Program to implement Arithmetic operator in python.
""" Author : ITVoyagers (itvoyagers.in) Date :20th August 2020 Description : Program to implement Arithmetic operator in python. """ #arithmetic operator in python a=int(input("Enter any number")) b=int(input("Enter any number")) #addition print("1.value of a+b",a+b) #subtraction print("2.value of a-b",a-b) #multiplication print("3.value of a*b",a*b) #division print("4.value of a/b",a/b) #exponent print("5.value of a**b",a**b) #floor print("6.value of a//b",a//b)
OUTPUT

Assignment Operator
Practical b : Program to implement Assignment operator in python.
""" Author : ITVoyagers (itvoyagers.in) Date :20th August 2020 Description : Program to implement Assignment operator in python. """ #assignment operator in python a=int(input("Enter any number")) b=int(input("Enter any number")) a+=b print("1.value of a+=b",a) a-=b print("2.value of a-=b",a) a*=b print("3.value of a*=b",a) a/=b print("4.value of a/=b",a) a**=b print("5.value of a**=b",a) a//=b print("6.value of a//=b",a)
OUTPUT

Comparison or Relational Operator
Practical c : Program to implement Comparison or Relational operator in python.
""" Author : ITVoyagers (itvoyagers.in) Date :20th August 2020 Description : Program to implement Comparison or Relational operator in python. """ #Comparison / Relational operator in Python a=int(input("Enter any number")) b=int(input("Enter any number")) #equal to if a==b: print("a and b are equal") else: print("a and b are not equal") #greater than if(a>b): print("a is greater than b") else: print("a is not greater than b") #greater than equal to if(a>=b): print("a is greater than or equal to b") else: print("a is not greater than or equal to b") #less than if(a<b): print("a is less than b") else: print("a is not less than b") #less than equal to if(a<=b): print("a is less than or equal to b") else: print("a is not less than or equal to b") #not equal to if(a!=b): print("a and b are not equal") else: print("a and b are equal")
OUTPUT

Logical Operator
Practical d : Program to implement Logical operator in python.
""" Author : ITVoyagers (itvoyagers.in) Date :20th August 2020 Description : Program to implement Logical operator in python. """ #logical operators in python t=True f=False print("value of t is : ",t,"value of f is : ",f) print("t and f is",t and f) print("t or f is",t or f) print("not t is",not t) x=1 y=1 print("value of x is : ",x,"value of y is : ",y) print("x and y is",x and y) print("x or y is",x or y) print("not x is",not x) x=0 y="" print("value of x is : ",x,"value of y is : ",y) print("x and y is",x and y) print("x or y is",x or y) print("not y is",not y)
OUTPUT

Bitwise Operator
Practical e : Program to implement Bitwise operator in python.
""" Author : ITVoyagers (itvoyagers.in) Date :20th August 2020 Description : Program to implement Bitwise operator in python. """ #Bitwise operators in Python a=int(input("Enter any number")) b=int(input("Enter any number")) print('a=',a,':',bin(a),'b=',b,':',bin(b)) c=0 c=a&b print("result of &(AND) is",c,':',bin(c)) c=a|b print("result of |(OR) is",c,':',bin(c)) c=a^b print("result of ^(XOR) is",c,':',bin(c)) c=~a print("result of ~(COMPLEMENT) is",c,':',bin(c)) c=a<<2 print("result of <<(LEFT SHIFT) is",c,':',bin(c)) c=a>>2 print("result of >>(RIGHT SHIFT) is",c,':',bin(c))
OUTPUT

Membership Operator
Practical f : Program to implement Membership operator in python.
""" Author : ITVoyagers (itvoyagers.in) Date :20th August 2020 Description : Program to implement Membership operator in python. """ #Membership operators in Python a=10 b=20 print("Value of a is : ",a ,"Value of b is : ",b) my_iterable=[1,2,3,4,5] print("My iterable consist of", my_iterable) if(a in my_iterable): print('a is in the list') else: print('a is not in the list') if(b not in my_iterable): print('b is not in the list') else: print('b is in the list') c=b/a if(c in my_iterable): print('c is in the list') else: print('c is not in the list')
OUTPUT

Identity Operator
Practical g : Program to implement Identity operator in python.
""" Author : ITVoyagers (itvoyagers.in) Date :20th August 2020 Description : Program to implement Identity operator in python. """ #Identity operators in Python a=int(input ("Enter a number")) b=int(input ("Enter a number")) print("a=",a,":",id(a),'b=',b,":",id(b)) if(a is b): print("a and b have same identity") else: print("a and b do not have same identity") if(id(a) == id(b)): print("a and b have same identity") else: print("a and b do not have same identity") if(a is not b): print("a and b do not have same identity") else: print("a and b have same identity")
OUTPUT

NOTE: Location will be same for same value with different variables.
For other python practicals:
For other python basics related posts:
For other advanced python related posts: