python38
python38
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 1/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Shopping Cart
Users can add different items to their shopping cart and checkout.
The total value of the cart should be more than a minimum amount (Rs.
100/-) for the checkout.
During Offer Sales, all users get a flat discount on their cart and the
minimum cart value will be Rs. 200/-.
Attributes
Instance Attributes
Class Attributes
Instance Attributes
Attributes whose value can differ for each instance of class are modeled as
instance attributes.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 2/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Class Attributes
Attributes whose values stay common for all the objects are modelled as
Class Attributes.
PYTHON
1 class Cart:
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 3/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self,..):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(items)
10 a = Cart()
Expand
Output
Self
self passed to method contains the object, which is an instance of class.
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self,item_name, quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self)
10
Expand
Output
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 4/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self, item_name,quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self.items)
10 a = Cart()
Expand
Output
{"book": 3}
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self, item_name,quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self.items)
10 a = Cart()
Expand
Output
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 5/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
{'book': 3}
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self, item_name,quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self.items)
10 print(Cart.items)
Output
Example 1
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6
7 print(Cart.min_bill)
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 6/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Output
100
Example 2
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def print_min_bill(self):
7 print(Cart.min_bill)
8
9 a = Cart()
10 a.print_min_bill()
Output
100
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def print_min_bill(self):
5 print(Cart.min_bill)
6 a = Cart()
7 b = Cart()
8 Cart min bill = 200
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 7/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
8 Cart.min_bill = 200
9 print(a.print_min_bill())
10 print(b.print_min_bill())
Output
200
200
Method
Instance Methods
Class Methods
Static Methods
Instance Methods
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 8/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Instance methods can access all attributes of the instance and have self as a
parameter.
Example 1
Code
PYTHON
1 class Cart:
2 def __init__(self):
3 self.items = {}
4 def add_item(self, item_name,quantity):
5 self.items[item_name] = quantity
6 def display_items(self):
7 print(self.items)
8
9 a = Cart()
10 a.add_item("book", 3)
Expand
Output
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 9/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
{'book': 3}
Example 2
Code
PYTHON
1 class Cart:
2 def __init__(self):
3 self.items = {}
4 def add_item(self, item_name,quantity):
5 self.items[item_name] = quantity
6 self.display_items()
7 def display_items(self):
8 print(self.items)
9
10 a = Cart()
Expand
Output
{'book': 3}
Class Methods
Methods which need access to class attributes but not instance attributes
are marked as Class Methods.
For class methods, we send
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 10/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 @classmethod
5 def update_flat_discount(cls,
6 new_flat_discount):
7 cls.flat_discount = new_flat_discount
8
9 Cart.update_flat_discount(25)
10 print(Cart.flat_discount)
Output
25
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 11/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 @classmethod
5 def update_flat_discount(cls, new_flat_discount):
6 cls.flat_discount = new_flat_discount
7
8 @classmethod
9 def increase_flat_discount(cls, amount):
10 new_flat_discount = cls.flat_discount + amoun
11 cls.update flat discount(new flat discount)
Expand
Output
50
Static Method
We might need some generic methods that don’t need access to either
instance or class attributes. These type of methods are called Static
Methods.
Usually, static methods are used to create utility functions which make more
sense to be part of the class.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 12/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 class Cart:
2
3 @staticmethod
4 def greet():
5 print("Have a Great Shopping")
6
7 Cart.greet()
Output
No cls or self as
self as parameter cls as parameter
parameters
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 13/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 14/14