Python Project: A Notes App
A notes app is a program that stores short notes in a file. It can add a note, list the notes, and delete one.
Write two notes
The first open writes two notes, one per line. The second open loads them back into a list.
f = open("notes.txt", "w")
f.write("Buy milk\n")
f.write("Call Ada\n")
f.close()
f = open("notes.txt", "r")
notes = f.read().splitlines()
f.close()
print(notes)Add a note
The program loads the notes, appends one more, and writes the whole list back.
f = open("notes.txt", "w")
f.write("Buy milk\n")
f.close()
f = open("notes.txt", "r")
notes = f.read().splitlines()
f.close()
notes.append("Water the plants")
f = open("notes.txt", "w")
for note in notes:
f.write(note + "\n")
f.close()
print(notes)Delete a note by number
del notes[1] removes the second note. The list keeps the rest in order.
notes = ["Buy milk", "Call Ada", "Water the plants"]
del notes[1]
print(notes)Example
f = open("my_notes.txt", "w")
f.write("Buy milk\n")
f.write("Call Ada\n")
f.write("Water the plants\n")
f.close()
f = open("my_notes.txt", "r")
notes = f.read().splitlines()
f.close()
notes.append("Read chapter 5")
del notes[1]
f = open("my_notes.txt", "w")
for note in notes:
f.write(note + "\n")
f.close()
print("Notes:")
for i, note in enumerate(notes, start=1):
print(i, note)
print("Total notes:", len(notes))Key points
- Notes app: a program that stores short notes in a file, one note per line
- Use open() with "w" to write the notes, one per line
- Use read().splitlines() to load the notes back into a list
- Add a note with append(), then save the whole list
- Delete a note with del notes[index], then save again
- Print the notes with numbers, so each note has its place
In this example, the program writes my_notes.txt first, with three notes inside. It loads the notes back into a list, appends a fourth note, and deletes the second one. The list is saved again, one note per line. The numbered printout shows the three notes that remain. The total of 3 proves the add and the delete both worked.
Note
Numbers make deleting clear to describe. "Delete note 2" names one note.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
How does the app delete a note?