In this tutorial, we will learn how to create a simple to-do list application using Python. We will be using the Python programming language and its built-in libraries to build a command-line interface for managing our to-do list. Let’s get started!
To follow along with this tutorial, you should have Python installed on your machine. You can download and install Python from the official Python website (https://www.python.org/downloads/).
todo_list.py.First, let’s import the necessary libraries.
import json
import os
Next, let’s define a function to display the main menu options.
def display_menu():
print("Todo List Menu:")
print("1. Add task")
print("2. Remove task")
print("3. View tasks")
print("4. Exit")
Now, let’s define our main function that will handle the user input and execute the corresponding actions.
def main():
tasks = []
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == "1":
task = input("Enter a new task: ")
tasks.append(task)
print("Task added.")
elif choice == "2":
task = input("Enter the task to remove: ")
if task in tasks:
tasks.remove(task)
print("Task removed.")
else:
print("Task not found.")
elif choice == "3":
print("Tasks:")
for task in tasks:
print("- " + task)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")
Lastly, let’s call the main function to start our to-do list application.
if __name__ == "__main__":
main()
To run the application, open your terminal or command prompt, navigate to the directory where you saved the todo_list.py file, and execute the following command:
python todo_list.py
You will see the main menu options. Enter the corresponding numbers to perform various actions such as adding tasks, removing tasks, viewing tasks, or exiting the application.
In this tutorial, we learned how to create a simple to-do list application using Python. We covered the basics of reading user input, managing tasks in a list, and displaying the tasks. You can further enhance this application by adding features like saving tasks to a file or implementing additional functionality. Happy coding!
Remember to check out the official Python documentation (https://docs.python.org/3/) for more information on how to work with Python and its libraries.