|
| 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