I made a "ToDo list app" using Python [with file saving function]

programming

"I've started learning Python, but I don't know what kind of app I should make." "I want to make something that's simple and gives me a sense of accomplishment." If you're like that, we recommendTo-do list appis.

This app allows you to add and delete tasks and save them to a file, making it perfect for both study and practical use.

In this article,Carefully explained so that even Python beginners can understandAt the same time, you will actually write code to create a ToDo app.

Benefits of creating a ToDo app with Python

conclusion

The ToDo app is a subject that is "simple but has a lot to learn."

There are many skills to learn

• How to use lists and dictionaries

• How to save a file

• Conditional branching (if statement)

• Repetition (for, while)

These are the basics you want to learn first in Python. If you create a to-do list app,Learn by actually moving the deviceSo it will be memorable.

The path to completion is clear

Add, delete, display, and save tasks. Create these four functions in order to complete the task.

The goal is clearThis is also a key point as it makes it less likely that you will give up halfway through.

Preparations required for creating an app

conclusion

All you need is Python and a text editor.

The tools you need are simple

Please prepare the following:

• Python (3.x series)

→ Official download page

• A text editor (e.g. VSCode, Sublime Text)

• Command line (Terminal for Mac, PowerShell for Windows)

Learn the basics of file storage

This time we use the open() function to save the data to a file.

This function is a useful tool that can be used to switch between modes such as "write" and "read".

Understand the basic structure of a ToDo list

conclusion

There are only four things to do: add, view, delete, and save.

Manage tasks with lists

1
tasks = []

The basic structure is that tasks are added and removed from a list.

Make user actions optional

1
while True: print("1. Add a task") print("2. View tasks") print("3. Delete a task") print("4. Save and exit") choice = input("Choose:")

In this way,User-choosable interfaceCreating this will make it look like an app.

Write a process to add and display a task

conclusion

Let's add the user's input to a list and display it pretty.

Code to add

1
task = input("Enter the task to add:") tasks.append(task)

The task will now be added to the tasks list.

List display code

1
for i, t in enumerate(tasks): print(f"{i + 1}. {t}")

Displaying them with numbers makes them easier to delete.

Implementing the functionality to delete a task

conclusion

It would be easier to use if you could delete it by entering the number.

Delete code

1
delete_index = int(input("Enter the number to delete:")) - 1 if 0 <= delete_index < len(tasks): del tasks[delete_index] else: print("Enter a valid number")

Checking number ranges is essential to prevent mistakes.

Confirmation message after deletion

1
print("Current tasks:") for i, t in enumerate(tasks): print(f"{i + 1}.{t}")

To the userIndicate that it has been deletedlet's.

Adding file saving functionality

conclusion

Using with open() allows you to safely save data.

Save Code Example

1
with open("tasks.txt", "w", encoding="utf-8") as f: for task in tasks: f.write(task + "\n") print("Saved.")

Japanese is also OK if saved in UTF-8.

Processes to load at startup

1
try: with open("tasks.txt", "r", encoding="utf-8") as f: tasks = [line.strip() for line in f.readlines()] except FileNotFoundError: tasks = []

It would be nice to know if it's okay if the file isn't there the first time.

Final code summary

conclusion

Let's put all this together in one place.

1
tasks = [] # read try: with open("tasks.txt", "r", encoding="utf-8") as f: tasks = [line.strip() for line in f.readlines()] except FileNotFoundError: pass while True: print("\n1. Add a task") print("2. Show tasks") print("3. Delete a task") print("4. Save and exit") choice = input("Select:") if choice == "1": task = input("Append:") tasks.append(task) elif choice == "2": for i, t in enumerate(tasks): print(f"{i + 1}. {t}") elif choice == "3": delete_index = int(input("Number to delete:")) - 1 if 0 <= delete_index < len(tasks): del tasks[delete_index] else: print("Invalid number.") elif choice == "4": with open("tasks.txt", "w", encoding="utf-8") as f: for task in tasks: f.write(task + "\n") print("Saved. Exit.") break else: print("Please enter the correct answer.")

Let's review what we've learned

conclusion

What I learned in this article

You can learn the basics of Python with this ToDo list.

• How to use input()

• Use lists

• Basics of if and for statements

• File processing with open()

• Saving and loading data

The next arrangement I want to try

• Add a deadline

• Check off completed tasks

• Display in GUI (e.g. tkinter)

summary

Easy to use even for Python beginnersTo-do list appteeth,The best learning material for learning basic grammar and file processing at the same timeis.

Please try running the app and experience the feeling of adding functions with your own hands.

If you want to take it a step further,Python Introduction SeriesTry developing other apps with this app!

Copied title and URL