-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_Creation of multiple objects.py
More file actions
76 lines (54 loc) · 1.67 KB
/
14_Creation of multiple objects.py
File metadata and controls
76 lines (54 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Creation of multiple objects from a single class :
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
def printDetails(self) :
print(f'Name is {self.name}')
print(f'Age is {self.age}')
print(f'Roll is {self.roll}')
print('-'*20)
s1 = Student('Priyanka', 24, 1024)
s1.printDetails()
s2 = Student('Rahul', 25, 1025)
s2.printDetails()
s3 = Student('Zini', 23, 1026)
s3.printDetails()
s4 = Student('Jack', 24, 1027)
s4.printDetails()
# Creating multiple objects from a single class using (list / loop) :
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
def printDetails(self) :
print(f'Name is {self.name}')
print(f'Age is {self.age}')
print(f'Roll is {self.roll}')
print('-'*20)
names = ['Priyanka', 'Rahul', 'Zini', 'Jack', 'Dev', 'Anjali', 'Archana', 'Dr. Chand', 'Scoot']
age = [23, 24, 25, 23, 24, 25, 24, 23, 22]
roll = [1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032]
obj_list = []
for i in range(len(names)) :
s = Student(names[i], age[i], roll[i])
obj_list.append(s)
print(obj_list)
print(obj_list[0])
print(obj_list[0].name)
print(obj_list[0].age)
print(obj_list[0].roll)
print(obj_list[1].name)
print(obj_list[1].age)
print(obj_list[1].roll)
print(obj_list[2].name)
print(obj_list[2].age)
print(obj_list[2].roll)
# or,
for i in range(len(names)) :
print(obj_list[i].name)
print(obj_list[i].age)
print(obj_list[i].roll)
print('-'*20)