Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96c35c5

Browse files
authored
Contains 2 part
1 parent b1fc24c commit 96c35c5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎docs/OOPS/collection_of_objects.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
We can also store a number of objects inside a list or a dictionary.
3+
The below example, we have a list of mobile objects and we are iterating over the list and printing the values
4+
'''
5+
6+
class Mobile:
7+
def __init__(self, brand, price):
8+
self.brand = brand
9+
self.price = price
10+
11+
mob1=Mobile("Apple", 1000)
12+
mob2=Mobile("Samsung", 2000)
13+
mob3=Mobile("Apple", 3000)
14+
mob4=Mobile("Samsung", 4000)
15+
mob5=Mobile("Apple", 5000)
16+
17+
list_of_mobiles=[mob1, mob2, mob3, mob4, mob5]
18+
19+
for mobile in list_of_mobiles:
20+
print (mobile.brand,mobile.price)
21+
22+
23+
24+
'''
25+
What do you think will be the output of the below code?
26+
'''
27+
28+
class Mobile:
29+
def __init__(self,brand,price):
30+
self.brand = brand
31+
self.price = price
32+
33+
mob1=Mobile("Apple", 1000)
34+
mob2=Mobile("Samsung", 2000)
35+
mob3=Mobile("Apple", 3000)
36+
37+
38+
list_of_mobiles=[mob1, mob2, mob3]
39+
40+
mob3.brand="Samsung"
41+
42+
for mobile in list_of_mobiles:
43+
print (mobile.brand, mobile.price)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /