Python projects with source code
Python projects with source code
I'd be happy to provide you with some Python project ideas and their source codes.
Project 1: To-Do List App
The To-Do List app is a simple task management tool that allows users to add, delete, and mark tasks as completed. This project helps you learn how to work with lists and dictionaries in Python.
class TodoList:
def init(self):
self.tasks = {}
def add_task(self, name, deadline):
self.tasks[name] = {'deadline': deadline, 'completed': False}
def remove_task(self, name):
if name in self.tasks:
del self.tasks[name]
def mark_completed(self, name):
if name in self.tasks:
self.tasks[name]['completed'] = True
def list_tasks(self):
for task in self.tasks.values():
status = 'Completed' if task['completed'] else 'Not Completed'
print(f"{task['deadline']} - {task['name']} ({status})")
todo_list = TodoList()
while True:
user_input = input("Enter command (add/remove/mark/list/quit): ")
if user_input == 'add':
name = input("Enter task name: ")
deadline = input("Enter task deadline: ")
todo_list.add_task(name, deadline)
elif user_input == 'remove':
name = input("Enter task name to remove: ")
todo_list.remove_task(name)
elif user_input == 'mark':
name = input("Enter task name to mark completed: ")
todo_list.mark_completed(name)
elif user_input == 'list':
todo_list.list_tasks()
elif user_input == 'quit':
break
Project 2: Hangman Game
The Hangman game is a classic word-guessing game that tests your ability to create functions and work with strings in Python.
import random
HANGMAN_PICS = ['''
+---+
| |
|
|
|
=========''', '''
+---+
| |
O |
|
|
=========''', '''
+---+
| |
O |
| |
|
=========''', '''
+---+
| |
O |
/| |
|
=========''', '''
+---+
| |
O |
/| |
|
=========''', '''
+---+
| |
O |
/| |
/ |
=========''']
words = ['apple', 'banana', 'cherry', 'date', 'elderberry']
def draw_hangman(attempts):
return HANGMAN_PICS[6-attempts]
def get_random_word():
return random.choice(words)
def main():
word = get_random_word()
guessed = '_' * len(word)
attempts = 10
while attempts > 0:
print(draw_hangman(attempts))
print(guessed)
guess = input("Guess a letter: ").lower()
if guess in word:
guessed_list = list(guessed)
for i in range(len(word)):
if word[i] == guess:
guessed_list[i*2:(i+1)*2] = guess
guessed = ''.join(guessed_list)
if '_' not in guessed:
print('Congratulations! You won!')
return
else:
attempts -= 1
print(f"Incorrect guess. You have {attempts} attempts left.")
print(draw_hangman(attempts))
print(f"Sorry, the word was: {word}")
print("You didn't win this time.")
if name == "main":
main()
Project 3: Chatbot
The Chatbot project is a simple AI-powered conversation tool that uses natural language processing (NLP) and machine learning (ML) techniques to understand user input and respond accordingly. This project helps you learn how to work with dictionaries, lists, and conditional statements in Python.
import random
class Chatbot:
def init(self):
self.responses = {
'hello': ['Hi!', 'Hello!'],
'how are you': ['I'm doing well.', 'I'm feeling good.'],
'what is your name': ['My name is Chatbot.', 'I am Chatbot.']
}
def get_response(self, user_input):
for keyword in self.responses:
if user_input.lower().startswith(keyword):
return random.choice(self.responses[keyword])
return "Sorry, I didn't understand that."
chatbot = Chatbot()
while True:
user_input = input("Enter your message: ")
response = chatbot.get_response(user_input)
print(response)
These are just a few examples of what you can create with Python. Remember to always practice and experiment with different projects and techniques to improve your skills!
Simple Python projects for beginners with source code
Here are some simple Python projects for beginners with their source code:
Project 1: Guessing Game
This game asks the user to guess a random number between 1 and 100.
import random
def guessing_game():
secret_number = random.randint(1, 100)
while True:
guess = int(input("Guess a number (1-100): "))
if guess < secret_number:
print("Too low! Try again!")
elif guess > secret_number:
print("Too high! Try again!")
else:
print(f"Congratulations! You guessed the number {secret_number} correctly.")
break
guessing_game()
Project 2: To-Do List
This program allows you to add, remove, and list tasks from a to-do list.
class TodoList:
def init(self):
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def remove_task(self, task):
if task in self.tasks:
self.tasks.remove(task)
else:
print(f"Task '{task}' not found.")
def list_tasks(self):
for task in self.tasks:
print(task)
todo_list = TodoList()
while True:
user_input = input("Type 'add' to add a task, 'remove' to remove a task, or 'list' to list tasks: ")
if user_input == 'add':
new_task = input("Enter the new task: ")
todo_list.add_task(new_task)
elif user_input == 'remove':
task_to_remove = input("Enter the task you want to remove: ")
todo_list.remove_task(task_to_remove)
elif user_input == 'list':
todo_list.list_tasks()
else:
print("Invalid command. Try again!")
Project 3: Hangman Game
This game allows you to guess a word by providing individual letters.
import random
def hangman():
words = ['apple', 'banana', 'cherry']
chosen_word = random.choice(words)
guessed_letters = []
guessed_word = ["_"] * len(chosen_word)
while True:
print(" ".join(guessed_word))
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("Please enter a single letter.")
continue
if guess in guessed_letters:
print(f"You already guessed the letter '{guess}'. Try again!")
continue
guessed_letters.append(guess)
if guess not in chosen_word:
print(f"The letter '{guess}' is not in the word.")
else:
for i, letter in enumerate(chosen_word):
if letter == guess:
guessed_word[i] = guess
if "_" not in guessed_word:
print("Congratulations! You guessed the word!")
break
hangman()
Project 4: Rock, Paper, Scissors Game
This game allows you to play rock, paper, scissors with a computer opponent.
import random
def rock_paper_scissors():
choices = ['rock', 'paper', 'scissors']
while True:
user_choice = input("Type 'rock', 'paper', or 'scissors' to make your choice: ").lower()
if user_choice not in choices:
print(f"Invalid choice '{user_choice}'. Try again!")
continue
computer_choice = random.choice(choices)
print(f"The computer chose {computer_choice}.")
if user_choice == computer_choice:
print("It's a tie! Try again.")
elif (user_choice == 'rock' and computer_choice == 'scissors') or
(user_choice == 'scissors' and computer_choice == 'paper') or
(user_choice == 'paper' and computer_choice == 'rock'):
print(f"You win! Your choice '{user_choice}' beats the computer's choice '{computer_choice}'.")
else:
print(f"You lose! The computer's choice '{computer_choice}' beats your choice '{user_choice}'.")
play_again = input("Type 'y' to play again or any other key to quit: ").lower()
if play_again != 'y':
break
rock_paper_scissors()
These projects should give you a good starting point for learning Python. Remember, the best way to learn is by doing! Try these projects out and see what you can create.