Python : Lab Assignment No.1 (Operators)

Python : Lab Assignment No.1 (Operators)

Ques 1) Write python programs to Implements all the airthmetic operatores like: +,-,/,*,%.

a = 5
b = 2
print("a + b :",(a+b))
print("a - b :",(a-b))
print("a * b :",(a*b))
print("a / b :",(a/b))
print("a % b :",(a%b))

Output :

a + b : 7
a - b : 3
a * b : 10
a / b : 2.5
a % b : 1

Ques 2) Write python programs to Implements all the Relational operatores like: <,>,<=,>=,==,!=.

a = 9
b = 5
print("a > b :",(a>b))
print("a < b :",(a<b))
print("a <= b :",(a<=b))
print("a >= b :",(a>=b))
print("a == b :",(a==b))
print("a != b :",(a!=b))

Output :

a > b : True
a < b : False
a <= b : False
a >= b : True
a == b : False
a != b : True

Ques 3) Write python programs to Implements all the Logical operatores like: and,or,not.

a = 10
b = -10
c = 0

if(a>0 and b>0 and c>0):
    print("All the numbers are greater than 0")
elif(not(a>0 or b>0 or c>0)):
    print("No number is greater than 0")
else:
    print("Atleast one number is greater than 0")

Output :

Atleast one number is greater than 0

Ques 4) Write python programs to Implements all the Assignment operatores like: =,+=,-=,*=,/=,%=,**=,//=

a = 10
a += 2
print(a)
a -= 4
print(a)
a *= 0.5
print(a)
a /= 2
print(a)
a %= 4
print(a)
a **= 3
print(a)
a //= 3
print(a)

Output :

12
8
4.0
2.0
2.0
8.0
2.0

Ques 5) Write python programs to Implements all the Bitwise operatores like:&,|,^,~,<<,>>

a = 10
b = 4
print("a & b :", a&b)
print("a | b :", a|b)
print("a ^ b :", a^b)
print("~a :", ~a)
print("a << 1 :", a<<1)
print("a >> 1 :", a>>1)

Output :

a &  b : 0
a | b : 14
a ^ b : 14
~a : -11
a << 1 : 20
a >> 1 : 5

Ques 6) Write python programs to Implements all the Membership operatores like: in, not in

x = 24
y = 20

l = [10,20,30,40,50]

if(x not in l):
    print("x is not present in list.")
else:
    print("x is present in list.")

if(y in l):
    print("y is present in list.")
else:
    print("y is not present in list.")

Output :

x is not present in list.
y is present in list.

Ques 7) Write a python program for type casting using Implicit and Explicit Type Conversion.

a = 7
b = 3.0
c = a + b
print(type(c),type(a),type(b))
d = "5"
n = int(d)
print(type(n))
m = float(n)
print(type(m))

Output :

<class 'float'> <class 'int'> <class 'float'>
<class 'int'>
<class 'float'>

Ques 8) Write python programs to Implements all the Identity operatores like: is ,is not.

x = 5
y = 5.2

if(type(x) is int):
    print("true")
else:
    print("false")

if(type(y) is not int):
    print("true")
else:
    print("false")

Output :

true
true
Reactions

Post a Comment

0 Comments