Practical Projects in Python
This lesson challenges you to apply the concepts learned through six interactive projects. From simple console games to web applications and automation, each project develops your skills in a practical and creative way.
Included Projects
- Number Guessing Game
- Simple Banking Application
- Mini CLI Chat
- Tic-Tac-Toe in Console
- Automation: Web Scraping with requests + BeautifulSoup
- Simple Web Application with Flask
Each project comes with explanations, full code, and extension suggestions. You can customize them, combine them, or turn them into more complex applications.
Number Guessing Game - CLI Project in Python
This simple command-line game allows the user to guess a number randomly generated by the computer. It is ideal for practicing loops, conditions, and input handling.
Game Code
import random
secret_number = random.randint(1, 100)
attempts = 0
print("Guess the number between 1 and 100!")
while True:
try:
guessed = int(input("Enter a number: "))
attempts += 1
if guessed < secret_number:
print("๐ Too low.")
elif guessed > secret_number:
print("๐ Too high.")
else:
print(f"๐ Congratulations! You guessed it in {attempts} attempts.")
break
except ValueError:
print("โ ๏ธ Enter a valid number.")
What You Practice
- Using the
randommodule whileloops andif-elif-elseconditions- Exception handling with
try-except - Command-line interaction
Possible Extensions
- Allow choosing the range (e.g., 1โ500)
- Save the score to a file
- Add a high score system
- Turn the game into a GUI using
tkinter
Simple Banking Application - CLI Project in Python
This application simulates a bank account in the command line. The user can check the balance, deposit money, and withdraw funds. It is ideal for practicing functions, conditional structures, and loops.
Application Code
balance = 1000
def display_menu():
print("\n๐ณ Banking Menu")
print("1. Check balance")
print("2. Deposit money")
print("3. Withdraw money")
print("4. Exit")
while True:
display_menu()
option = input("Choose an option: ")
if option == "1":
print(f"๐ฐ Current balance is: {balance} GBP")
elif option == "2":
amount = float(input("Enter deposit amount: "))
balance += amount
print(f"โ
You deposited {amount} GBP.")
elif option == "3":
amount = float(input("Enter withdrawal amount: "))
if amount <= balance:
balance -= amount
print(f"โ
You withdrew {amount} GBP.")
else:
print("โ Insufficient funds.")
elif option == "4":
print("๐ Goodbye!")
break
else:
print("โ ๏ธ Invalid option.")
What You Practice
- Simple functions
whileloops and conditions- Handling numeric input
Possible Extensions
- Save the balance to a file
- Add password authentication
- Allow transfers between accounts
Mini CLI Chat - Interactive Project in Python
This project simulates a conversation between two users in the command line. Messages are saved in a text file, and each user can read and send messages. It is ideal for practicing file handling and interactive loops.
Application Code
def send_message(name):
message = input(f"{name} says: ")
with open("chat.txt", "a") as f:
f.write(f"{name}: {message}\n")
def display_chat():
print("\n๐ Chat History:")
try:
with open("chat.txt", "r") as f:
print(f.read())
except FileNotFoundError:
print("๐ฌ The chat is empty.")
print("๐ฌ Mini CLI Chat - type 'exit' to quit.")
while True:
display_chat()
user = input("Who is writing (User1/User2)? ")
if user.lower() == "exit":
break
send_message(user)
What You Practice
- Reading and writing text files
- Functions and parameters
- Interactive loop with input
Possible Extensions
- Add timestamp to each message
- Allow clearing the chat history
- Transform the app into a web chat with Flask
Tic-Tac-Toe in Console - Interactive Game in Python
This project implements the classic "X and O" game in the command line. Two players alternate moves on a 3x3 board, and the program automatically checks for a winner or a draw.
Game Code
board = [" "]*9
def display_board():
print(f"""
{board[0]} | {board[1]} | {board[2]}
---+---+---
{board[3]} | {board[4]} | {board[5]}
---+---+---
{board[6]} | {board[7]} | {board[8]}
""")
def check_winner(symbol):
combinations = [
[0,1,2], [3,4,5], [6,7,8], # rows
[0,3,6], [1,4,7], [2,5,8], # columns
[0,4,8], [2,4,6] # diagonals
]
return any(all(board[i] == symbol for i in c) for c in combinations)
player = "X"
while True:
display_board()
try:
position = int(input(f"Player {player}, choose a position (0โ8): "))
if board[position] == " ":
board[position] = player
if check_winner(player):
display_board()
print(f"๐ Player {player} won!")
break
elif " " not in board:
display_board()
print("๐ค Draw!")
break
player = "O" if player == "X" else "X"
else:
print("โ ๏ธ Position already taken.")
except (ValueError, IndexError):
print("โ ๏ธ Enter a valid number between 0 and 8.")
What You Practice
- Lists and indexing
- Functions and logical checks
- Interactive loop
Possible Extensions
- Add scores and multiple rounds
- Allow playing against the computer
- Transform the game into a graphical interface with
tkinter
Automation - Web Scraping with requests + BeautifulSoup
This project demonstrates how to extract data from a web page using the requests and
BeautifulSoup libraries. It is ideal for automating online data collection.
Application Code
import requests
from bs4 import BeautifulSoup
url = "https://quotes.toscrape.com"
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
quotes = soup.find_all("span", class_="text")
print("๐ Extracted Quotes:")
for q in quotes:
print(q.text)
What You Practice
- Sending HTTP requests with
requests - Traversing HTML structure with
BeautifulSoup - Extracting data from HTML elements
Possible Extensions
- Save quotes to a text file or CSV
- Extract authors and quote topics
- Create a program that collects data from multiple pages
Simple Web Application with Flask - Python Web Project
This project builds a basic web application using the Flask framework. It is ideal for starting
web development in Python and understanding routes, templates, and the local server.
Application Code
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "๐ Hello, John! Welcome to your Flask application."
@app.route("/about")
def about():
return "๐ This is a simple web application created with Flask."
if __name__ == "__main__":
app.run(debug=True)
What You Practice
- Creating a local web server
- Defining routes (
@app.route) - Returning HTML or text content
Possible Extensions
- Add forms and data processing
- Integrate HTML templates with
Jinja2 - Transform the app into a blog or dashboard