Shopping Cart
Learn how to create a simple shopping cart using Python lists, dictionaries, loops, functions and calculations.
1. Project Introduction
A shopping cart allows a user to select products, specify quantities and calculate the total price.
This project is useful for understanding how e-commerce systems manage product and cart data.
2. Creating Product Data
A dictionary can represent one product.
product = {
"id": 1,
"name": "Laptop",
"price": 55000
}
3. Creating a Product List
Multiple products can be stored in a list.
products = [
{
"id": 1,
"name": "Laptop",
"price": 55000
},
{
"id": 2,
"name": "Keyboard",
"price": 1500
},
{
"id": 3,
"name": "Mouse",
"price": 800
}
]
4. Creating the Cart
The shopping cart can start as an empty list.
cart = []
Products selected by the user can then be added to this list.
5. Adding a Product to the Cart
cart.append({
"id": 1,
"name": "Laptop",
"price": 55000,
"quantity": 1
})
The quantity tells us how many units the customer wants to purchase.
6. Calculating Item Total
The total price of one cart item is:
item_total = (
item["price"] *
item["quantity"]
)
7. Calculating Cart Total
We can loop through all cart items and add their individual totals.
total = 0
for item in cart:
total += (
item["price"] *
item["quantity"]
)
8. Creating an Add-to-Cart Function
def add_to_cart(product, quantity):
cart.append({
"id": product["id"],
"name": product["name"],
"price": product["price"],
"quantity": quantity
})
9. Updating Quantity
If the same product is already in the cart, we can increase its quantity instead of creating another cart item.
for item in cart:
if item["id"] == product_id:
item["quantity"] += 1
10. Removing a Product
A product can be removed from the cart using remove().
cart.remove(item)
In a larger application, finding the item by a unique product ID is safer than relying only on the product name.
11. Changing Quantity
Users may want to increase or decrease the quantity.
item["quantity"] += 1 item["quantity"] -= 1
12. Checking for an Empty Cart
if not cart:
print("Your cart is empty.")
Empty lists are considered False in Python.
13. Creating a Cart Total Function
def calculate_total():
total = 0
for item in cart:
total += (
item["price"] *
item["quantity"]
)
return total
14. Taking User Input
product_id = int(
input("Enter product ID: ")
)
quantity = int(
input("Enter quantity: ")
)
The input should be converted from text to an integer before performing numerical operations.
15. Input Validation
if quantity <= 0:
print(
"Quantity must be greater than zero."
)
We should also verify that the selected product exists before adding it to the cart.
16. Displaying the Cart
for item in cart:
print(
item["name"],
"x",
item["quantity"],
"=",
item["price"] *
item["quantity"]
)
17. Menu-Based Shopping Cart
A while loop can provide a simple menu.
while True:
print("1. View Products")
print("2. Add to Cart")
print("3. View Cart")
print("4. Remove Item")
print("5. Change Quantity")
print("6. Checkout")
print("7. Exit")
choice = input(
"Enter choice: "
)
18. Checkout Calculation
During checkout, the final cart total can be calculated.
total = calculate_total()
print("Final Total:", total)
A real shopping system would then connect the order to payment and order-management systems.
19. Complete Project Flow
20. Concepts You Practiced
- Python Lists
- Python Dictionaries
- Nested Data Structures
- for Loops
- while Loops
- if / else
- Functions
- append()
- remove()
- Dictionary Access
- Quantity Calculations
- Input Validation
- Shopping Cart Logic
21. Run Complete Project 🚀
You have learned the core logic of a shopping cart. Now open the complete interactive project.
Run Complete Project