A to-do list application is a useful tool to help you stay organized and manage your day-to-day tasks. In this article, we will build a simple to-do list app using Python.
Requirements
Before we start building the application, let's go over some requirements:
- Basic knowledge of Python programming
- Python 3.10 installed on your system
- A code/text editor like VS Code, Atom, Sublime Text, etc.
Application Overview
The to-do list app we will build will have the following features:
- Add new tasks
- View list of tasks
- Mark tasks as completed
- Delete tasks
- Save tasks to the file
- Load tasks from the file
The app will run on the Command Line/Terminal, allowing users to choose options from a text menu.
Importing the Required Modules
We will need the following Python Modules for our application:
import datetime
import os
The datetime module will enable us to work with dates and times, which we will be able to utilize to list jobs by due date while the ‘os’ module will be used for saving and loading tasks from a file.
Designing the Task Class
We will create a Task class to represent each to-do item in our list. The Task class will have the following attributes:
- task name
- due date
- completion status
And the following methods:
- change task name
- change due date
- mark as completed Below is the code for the Task Class:
class Task:
def __init__(self, name, due_date):
self.name = name
self.due_date = due_date
self.completed = False
def change_name(self, new_name):
`self.name = new_name
def change_due_date(self, new_date):
self.due_date = new_date
def complete(self):
self.completed = True
This Python code defines a Task class, which serves as a blueprint for representing the to-do items. The class includes attributes and methods to manage and manipulate task-related information. Overall, this offers the basic functionality required for each task item.
Building the Menu
The code below initializes a list named tasks and defines a menu dictionary, providing options for managing tasks within a to-do list application. The menu will allow the user to choose what operation they want to perform.
# Initialize the tasks list
tasks = []
# Building the Menu
menu = {
1: 'Add New Task',
2: 'View All Tasks',
3: 'Mark Task as Completed',
4: 'Delete Task',
5: 'Save Tasks',
6: 'Load Tasks',
7: 'Quit'
}
We can print the menu options and prompt the user to select an option:
# Printing the Menu options
print('To-Do List Menu')
for key, value in menu.items():
print(f'{key} -- {value}')
choice = input('Please enter your selection: ')
Implementing the Functions
Now we need to build out the functions that will perform each menu operation.
Add New Task
This function will prompt the user for the task name and due date, create a new Task object, and add it to the list of tasks.
# Implementing the Functions and adding a new task
def add_task():
name = input('Enter task name: ')
due_date = input('Enter due date: ')
new_task = Task(name, due_date)
tasks.append(new_task)
View All Tasks
This will loop through the list of tasks and print out the details of each one - name, due date, and completion status.
# Viewing all tasks
def view_tasks():
print('All Tasks:')
for task in tasks:
print(f'Name: {task.name}')
print(f'Due Date: {task.due_date}')
print(f'Completed: {task.completed}')
print()
Mark as Completed
We will prompt the user for the index of the task to mark as completed, then set the completed flag to True on that task object.
# Marking Tasks as completed
def complete_task():
index = int(input('Enter the task number to mark as completed: '))
tasks[index - 1].complete()
Delete Task
Similar to marking as completed, we will prompt for the task index and use pop() to remove it from the list.
# Deleting Tasks
def delete_task():
index = int(input('Enter the task number to delete: '))
tasks.pop(index - 1)
Save Tasks
For saving, we will use Python's built-in pickle module. This allows us to serialize the task objects into a file that can be loaded later.
# Save Tasks
import pickle
def save_tasks():
file = open('tasks.dat', 'wb')
pickle.dump(tasks, file)
file.close()
Load Tasks
To load the tasks, we simply deserialize the data from the file back into a list of Task objects.
# Loading Tasks
def load_tasks():
global tasks
file = open('tasks.dat', 'rb')
tasks = pickle.load(file)
file.close()
Main Application Loop
Finally, we need a main loop that prints the menu, gets the user's choice, calls the appropriate function, and repeats until they choose to quit.
# Main Application Loop
def display_menu():
print('To-Do List Menu')
for key, value in menu.items():
print(f'{key} -- {value}')
while choice != '7':
if choice == '1':
add_task()
elif choice == '2':
view_tasks()
elif choice == '3':
complete_task()
input('Press enter to return to menu: ')
display_menu()
choice = input('Please enter your selection: ')
This To-Do List Application can be executed by opening the Terminal/Command Prompt and run the following command (assuming the python file is titled todolist.py)
For Windows:
python todolist.py
For Linux/Mac
python3 todolist.py
You should get an output similar to the ones in the following photos after running the program:
Windows:
For Linux/Mac:
Conclusion
And that's it! With these basics we now have a simple command line to-do list application in Python. Some ways we could expand on this are:
- Adding user accounts
- Storing tasks in a database
- Building a GUI with tkinter
- Allow sorting/filtering tasks
- Adding priority levels
Even so, this small program demonstrates core programming concepts such as functions, classes, lists, file I/O, and user input. So, with roughly 90+ lines of code, we constructed and learned quite a deal - hopefully, this provides you a good starting point for a Python program!
Based on further requirements, you can make many more improvements and make it even more fun!
Have fun coding!
Top comments (0)