Oop Assignment
Oop Assignment
"Name": "Rupak",
"Dept": "CSE",
"ID": 4751
}
print(dict)
{'Name': 'Rupak', 'Dept': 'CSE', 'ID': 4751}
dict = {
"Name": "Rupak",
"Dept": "CSE",
"ID": 4751,
"ID": 799
}
print(dict)
{'Name': 'Rupak', 'Dept': 'CSE', 'ID': 799}
dict = {
"Name": "Rupak",
"Dept": "CSE",
"ID": 4751,
"ID": 799
}
print(len(dict))
dict = {
"Name": "Rupak",
"Dept": "CSE",
"ID": 4751,
"ID": 799
}
print(dict)
dict["Name"] = "Rupak1"
print(dict)
dict = {
"Name": "Rupak",
"Dept": "CSE",
"ID": 4751,
"ID": 799
}
print(dict.values())
print(dict.keys())
print(dict.items())
dict_values(['Rupak', 'CSE', 799])
dict_keys(['Name', 'Dept', 'ID'])
Ques-04: What is the significance of the self parameter in Python class methods?
Ans : The “self” parameter plays a crucial role in Python. It provides a way to
access the attributes and metods of the class in which it is used. It allows an
object’s methods to access and manipulate the object’s instance variables and
other methods. It must be the first parameter of any method in the class. Also,
“self” helps to differentiate instance attributes from local vcariables.
#Rupak
#ID-221-15-4751
#Creating an Object
obj = cat()
Rock is a mammal
Tommy is also a mammal
My name is Rock
My name is Tommy
# class attribute
attr1 = "mammal"
# Instance attribute
def __init__(self, name):
self.name = name
def speak(self): print("My
name is {}".format(self.name))
# Driver code
# Object instantiation
Rock = Dog("Rock")
Tommy = Dog("Tommy")
# Accessing class methods
Rock.speak()
Tommy.speak()
My name is Rock
My name is Tommy
#Inheritance in Python
# Python code to demonstrate how parent constructors
# are called.
def display(self):
print(self.name)
print(self.idnumber)
def details(self): print("My
name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
#Polymorphism in Python
class Bird:
def
type(self):
print("There are many types of birds.")
def fly(self):
print("Most of the birds can fly but some cannot.")
class sparrow(Bird):
def
fly(self):
print("Sparrows can fly.")
class ostrich(Bird):
def fly(self):
print("Ostriches cannot fly.")
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
obj_bird.type()
obj_bird.fly()
obj_spr.type()
obj_spr.fly()
obj_ost.type()
obj_ost.fly()
#Encapsulation in Python
# Calling constructor of
# Base class
Base.__init__(self)
print("Calling private member of base class: ")
print(self.__c)
# Driver code
obj1 = Base()
print(obj1.a)
Hello World
April 30, 2024
Area: 20 Perimeter: 18
[ ]:
1
April 30, 2024
[ ]:
1
April 30, 2024
[ ]:
1
class Shape: def
calculate_area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def calculate_area(self):
pi = 3.141592653589793
return pi * self.radius ** 2
rectangle = Rectangle(5, 4)
circle = Circle(3)
--------------------------------------------------------------------
-------
TypeError Traceback (most recent call last)
Cell In[6], line 4
2 x=3
3 y="Hello"
----> 4 x+y z=
5 print(z)
--------------------------------------------------------------------
-------
ZeroDivisionError Traceback (most recent call last)
Cell In[9], line 4
2 x=4
3 y=0
----> 4 x/y z=
5 print('z')
--------------------------------------------------------------------
-------
NameError Traceback (most recent call last)
Cell In[10], line 3
1 x=3
2 y="Hello"
----> 3 print(z)
[ ]:
1
Cat sound: Meow
[ ]:
print("Man city ")
Man city
def func():
print("Forca Barca")
func()
Forca Barca
a=1 b="Hi"
c="Hello"
print(a,b,c)
1 Hi Hello
print(b+c)
HiHello
print(a,b)
1 Hi
def func(x,y):
print(x , " is " , y)
func("Am", "jam")
Am is jam
def func(*x): print(x[0] ,
" is " , x[1])
func("Am", "jam")
Am is jam
def func_arb(*x1): print(x1[0] + "," + x1[1], "and",
x1[2], "are bestus")
func_arb("Rupak","Rupak1","Rupak2")
Rupak,Rupak1 and Rupak2 are
bestus
def func(x,y): print(x, "and",
y, "are frnds")
func(x='Rupak', y='Rupak1')
Rupak and Rupak1 are frnds
1
def func(**x):
print(x['a'],x['b'],x['c'],x['d'])
BD is love
Spain is love
Argentina is love
Germany is love
def func(frnd1,frnd2,frnd3):
print(f"{frnd1} is my close friend.")
print(pow(2))
print(pow(4))
print(pow(6))
4
16
36
print(f[1:6])
print(f[2:6])
print(f[3:])
print(f[:])
['jam', 'kadol', 'kola', 'kola']
['kadol', 'kola', 'kola']
['kola', 'kola']
for i in f:
print(f)
['am', 'jam', 'kadol', 'kola', 'kola']
['am', 'jam', 'kadol', 'kola', 'kola']
['am', 'jam', 'kadol', 'kola', 'kola']
['am', 'jam', 'kadol', 'kola', 'kola']
['am', 'jam', 'kadol', 'kola', 'kola']
f= ['am', 'jam', 'kadol', 'kola','kola']
for i in f:
print(i)
am
jam
kadol
kola
kola
print(a)
print(tuple[1])
am
jam
am
jam
kadol
x=[1,2,3,4,5]
y=4
if y in x:
print(True)
True
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
for i in y:
x1=x-i
y1.append(x1)
print(y1)
2024
11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x=[(1,2,3), (4,5,6)]
for i in x:
print(i,end='')
(1, 2, 3)(4, 5, 6)
l1=[1,2,3]
l2=['am', 'jam', 'kola']
l3=l1 l4=l2.copy()
print(l3) for i in l4:
print(i,' ', end='')
[1, 2, 3]
am jam kola
l2.append(l1)
print(l2)
['am', 'jam', 'kola', [1, 2, 3]]
l1=[1,2,3] l2=['am',
'jam', 'kola']
for i in l2:
l1.append(i)
print(l1)
[1, 2, 3, 'am', 'jam', 'kola']
l1=[1,2,3] l2=['am',
'jam', 'kola']
for i in l2:
l1.append(l2)
print(l1)
[1, 2, 3, ['am', 'jam', 'kola'], ['am', 'jam', 'kola'], ['am', 'jam',
'kola']]
l1=[1,2,3]
l2=['am', 'jam', 'kola']
l1.extend(l2)
print(l1)
[1, 2, 3, 'am', 'jam', 'kola']
f2=['mwssi', 'ron']
f3=f2+l2 print(f3)
['mwssi', 'ron', 'am', 'jam', 'kola']