Creating a Pure Python To-Do List App is an excellent beginner project that allows you to understand core Python concepts such as functions, loops, conditionals, and file handling. Here’s a step-by-step guide for students to build a simple To-Do List App using pure Python (without external libraries).
This app will allow users to:
Ensure you have Python installed on your system. You can download it from python.org. Once installed, you can use any code editor like VS Code, PyCharm, or even IDLE (comes with Python installation).
In this simple project, we’ll use one Python file to manage everything. The tasks will be stored in a text file called tasks.txt to persist data even after the app is closed.
Project folder structure:
ToDoApp/
├── todo_app.py
└── tasks.txt
Step 3: Create Functions for the App
The app will be driven by several functions:
Load tasks from the text file.
Step 4: Write the Code
Here’s the code for the To-Do List app in pure Python:
import os
# Function to load tasks from the file
def load_tasks():
tasks = []
if os.path.exists("tasks.txt"):
with open("tasks.txt", "r") as file:
tasks = file.readlines()
return [task.strip() for task in tasks]
# Function to save tasks to the file
def save_tasks(tasks):
with open("tasks.txt", "w") as file:
for task in tasks:
file.write(task + "\n")
# Function to display the main menu
def display_menu():
print("\nTo-Do List App")
print("1. View Tasks")
print("2. Add Task")
print("3. Mark Task as Completed")
print("4. Delete Task")
print("5. Exit")
choice = input("Choose an option: ")
return choice
# Function to add a task
def add_task(tasks):
task = input("Enter the task: ")
tasks.append(task)
save_tasks(tasks)
print(f"Task '{task}' added successfully!")
# Function to view tasks
def view_tasks(tasks):
if not tasks:
print("No tasks available.")
else:
print("\nYour To-Do List:")
for index, task in enumerate(tasks):
print(f"{index + 1}. {task}")
# Function to mark a task as completed
def mark_task_completed(tasks):
view_tasks(tasks)
if tasks:
task_number = int(input("Enter task number to mark as completed: "))
if 1 <= task_number <= len(tasks):
tasks[task_number - 1] = tasks[task_number - 1] + " (Completed)"
save_tasks(tasks)
print("Task marked as completed!")
else:
print("Invalid task number!")
# Function to delete a task
def delete_task(tasks):
view_tasks(tasks)
if tasks:
task_number = int(input("Enter task number to delete: "))
if 1 <= task_number <= len(tasks):
task_to_delete = tasks.pop(task_number - 1)
save_tasks(tasks)
print(f"Task '{task_to_delete}' deleted successfully!")
else:
print("Invalid task number!")
# Main function to run the app
def main():
tasks = load_tasks()
while True:
choice = display_menu()
if choice == "1":
view_tasks(tasks)
elif choice == "2":
add_task(tasks)
elif choice == "3":
mark_task_completed(tasks)
elif choice == "4":
delete_task(tasks)
elif choice == "5":
print("Exiting the app. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Loading and Saving Tasks:
Displaying Menu:
Adding, Viewing, Marking, and Deleting Tasks:
Main Loop:
python todo_app.py
The app will prompt you with a menu to interact with.
Once the basic app is working, you can enhance it with additional features:
This project helps you get hands-on experience with basic Python programming and file handling. Once you complete the app, you’ll have a functional to-do list that saves your tasks between sessions, and you’ll have practiced core programming skills like loops, conditionals, file reading/writing, and handling user input.
Good luck building your To-Do List app!
Looking for more job opportunities? Look no further! Our platform offers a diverse array of job listings across various industries, from technology to healthcare, marketing to finance. Whether you're a seasoned professional or just starting your career journey, you'll find exciting opportunities that match your skills and interests. Explore our platform today and take the next step towards your dream job!
Looking for insightful and engaging blogs packed with related information? Your search ends here! Dive into our collection of blogs covering a wide range of topics, from technology trends to lifestyle tips, finance advice to health hacks. Whether you're seeking expert advice, industry insights, or just some inspiration, our blog platform has something for everyone. Explore now and enrich your knowledge with our informative content!