Part 1 — Learn Python: Restaurant Website Example 😋

Raneem
3 min readMay 23, 2024

--

This series covers Python basics. In this part, we focus on using Lists, Sets, Tuples, Dictionaries, Functions, Loops, and If-Else statements

Let’s create a simple backend structure for a restaurant website using lists, sets, tuples, and dictionaries. We’ll include features like managing the menu, handling reservations, tracking customers, and processing orders

Step-by-Step Implementation

1. Define the Menu

Using dictionaries and tuples to manage the menu, where each dish has a name, price, and category.

menu = {
“Starters”: [
(“Spring Rolls”, 5.99),
(“Garlic Bread”, 3.99)
],
“Main Courses”: [
(“Grilled Salmon”, 15.99),
(“Spaghetti Bolognese”, 12.99)
],
“Desserts”: [
(“Cheesecake”, 6.99),
(“Chocolate Cake”, 5.99)
],
“Beverages”: [
(“Coffee”, 2.99),
(“Tea”, 2.49)
]
}

2. Handle Reservations

Using a list of dictionaries to manage reservations. Each reservation includes the customer’s name, time, and the number of people.

reservations = [
{"name": "John Doe", "time": "19:00", "people": 4},
{"name": "Jane Smith", "time": "20:00", "people": 2}
]py

3. Track Customers

Using a set to keep track of unique customers.

customers = {"John Doe", "Jane Smith"}

4. Process Orders

Using dictionaries to manage orders where each order is associated with a customer and contains a list of ordered items.

orders = {
"John Doe": ["Spring Rolls", "Grilled Salmon", "Coffee"],
"Jane Smith": ["Garlic Bread", "Spaghetti Bolognese", "Cheesecake"]
}py

5. Define Functions to Handle Website Operations

Functions to
- Add a new reservation,
- Add a new customer
- Place an order, and
- Print the menu.

def add_reservation(name, time, people):
reservations.append({"name": name, "time": time, "people": people})
print(f"Reservation added for {name} at {time} for {people} people.")def add_customer(name):
if name not in customers:
customers.add(name)
print(f"Customer {name} added.")
else:
print(f"Customer {name} already exists.")

def place_order(customer_name, items):
if customer_name in customers:
if customer_name in orders:
orders[customer_name].extend(items)
else:
orders[customer_name] = items
print(f"Order placed for {customer_name}: {', '.join(items)}")
else:
print(f"Customer {customer_name} not found. Please add the customer first.")

def print_menu():
for category, items in menu.items():
print(f"{category}:")
for item, price in items:
print(f" - {item}: ${price:.2f}")
print("\n")

# Create Menu by calling all functions
print("Menu:")
print_menu()
print("Adding a new reservation:")
add_reservation("Alice Brown", "18:30", 3)
print("Adding a new customer:")
add_customer("Alice Brown")
print("Placing an order:")
place_order("Alice Brown", ["Tea", "Chocolate Cake"])
print("Current Reservations:", reservations)
print("Current Customers:", customers)
print("Current Orders:", orders)

Summary

This example shows how to use lists, sets, tuples, and dictionaries in a restaurant website backend. The menu is organized using dictionaries and tuples, reservations are managed with a list of dictionaries, customers are tracked with a set to ensure uniqueness, and orders are stored in a dictionary for quick access. The functions handle various operations such as adding reservations, customers, and orders and printing the menu

I hope you enjoyed this real-world scenario example to learn about collections, functions, loops, and conditionals in a fun way!

Want to extend your learning? 🤔

Read PART 2 where we leverage a free API in this scenario

If you like it SHARE and give it a CLAP 👏 below 👇

Happy Learning!

--

--