In this tutorial, we will learn how to create a simple To-Do List application using Python. This application will allow users to add tasks, view tasks, and remove tasks from their to-do lists. We will be using Python built-in functions and modules to achieve our goal. Let’s get started!
Step 1: Import Necessary Modules
First, we will need to import the “os” module, which offers a way of using operating system-dependent functionality. We will use it to clear the screen when updating the task list.
1 |
import os |
Step 2: Create a Function to Clear the Screen
Next, we will create a function called clear_screen()
that will use the “os” module to clear the screen. This function will be utilized every time the list updates.
1 2 |
def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear') |
Step 3: Create a Function to Display the To-Do List
In this step, we will create a function named view_tasks()
to display the existing tasks in the to-do list. This function will loop through the tasks in the list and display them with their index value + 1.
1 2 3 4 5 6 7 8 |
def view_tasks(tasks): clear_screen() if tasks: print("To-Do List:") for index, task in enumerate(tasks, start=1): print(f"{index}. {task}") else: print("Your To-Do List is empty.") |
Step 4: Create a Function to Add Tasks
Now, we will create a function called add_task()
that will ask the user for input and append their task to the existing list of tasks.
1 2 3 4 |
def add_task(tasks): task = input("Enter a task: ") tasks.append(task) print(f"'{task}' added to the list.") |
Step 5: Create a Function to Remove Tasks
In this step, we will create a function named remove_task()
that will ask the user which task they would like to remove and delete the selected task from the to-do list.
1 2 3 4 5 6 7 8 |
def remove_task(tasks): if tasks: view_tasks(tasks) task_index = int(input("Enter the number of the task you want to remove: ")) - 1 removed_task = tasks.pop(task_index) print(f"'{removed_task}' removed from the list.") else: print("Your To-Do List is empty.") |
Step 6: Create the Main Function
Now, we need to create a function named main()
that will serve as the entry point for our application. It will display a menu for the user to choose an action, and then call the corresponding function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def main(): tasks = [] while True: print("\nTo-Do List Actions:") print("1. View tasks") print("2. Add tasks") print("3. Remove tasks") print("4. Quit") action = input("Choose an action: ") if action == "1": view_tasks(tasks) elif action == "2": add_task(tasks) elif action == "3": remove_task(tasks) elif action == "4": break else: print("Invalid choice, please try again.") |
Step 7: Run the Application
Finally, to run the application, add the following code at the end of the file, which calls the main()
function:
1 2 |
if __name__ == "__main__": main() |
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import os def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear') def view_tasks(tasks): clear_screen() if tasks: print("To-Do List:") for index, task in enumerate(tasks, start=1): print(f"{index}. {task}") else: print("Your To-Do List is empty.") def add_task(tasks): task = input("Enter a task: ") tasks.append(task) print(f"'{task}' added to the list.") def remove_task(tasks): if tasks: view_tasks(tasks) task_index = int(input("Enter the number of the task you want to remove: ")) - 1 removed_task = tasks.pop(task_index) print(f"'{removed_task}' removed from the list.") else: print("Your To-Do List is empty.") def main(): tasks = [] while True: print("\nTo-Do List Actions:") print("1. View tasks") print("2. Add tasks") print("3. Remove tasks") print("4. Quit") action = input("Choose an action: ") if action == "1": view_tasks(tasks) elif action == "2": add_task(tasks) elif action == "3": remove_task(tasks) elif action == "4": break else: print("Invalid choice, please try again.") if __name__ == "__main__": main() |
Output
To-Do List Actions: 1. View tasks 2. Add tasks 3. Remove tasks 4. Quit Choose an action:
Conclusion
We have successfully created a simple To-Do List application in Python. This application allows users to add, view, and remove tasks from their to-do lists.
This project can be further expanded by adding features like task prioritization and due dates, or even integrating with a database to store tasks. Keep exploring and enhancing your skills!