Sai A Sai A
Updated date Mar 13, 2024
In this blog, we will learn how to create a basic notes application using Python, with step-by-step explanations and code examples.

Method 1: Using Lists for Note Storage

class NotesApp:
    def __init__(self):
        self.notes = []

    def add_note(self, note):
        self.notes.append(note)

    def display_notes(self):
        if not self.notes:
            print("No notes available.")
        else:
            print("Your notes:")
            for note in self.notes:
                print("-", note)

# Instantiate the NotesApp class
app = NotesApp()

# Add notes
app.add_note("Remember to buy milk")
app.add_note("Call John for project update")

# Display notes
app.display_notes()

Output:

Your notes:
- Remember to buy milk
- Call John for project update

In this method, we create a NotesApp class that utilizes a list to store notes. The add_note method appends a new note to the list, while the display_notes method prints out all the notes stored in the list. If there are no notes available, it displays a message indicating the same.

Method 2: Using a Text File for Note Storage

class NotesApp:
    def __init__(self, file_path):
        self.file_path = file_path

    def add_note(self, note):
        with open(self.file_path, "a") as file:
            file.write(note + "\n")

    def display_notes(self):
        try:
            with open(self.file_path, "r") as file:
                print("Your notes:")
                for line in file:
                    print("-", line.strip())
        except FileNotFoundError:
            print("No notes available.")

# Instantiate the NotesApp class
app = NotesApp("notes.txt")

# Add notes
app.add_note("Remember to buy milk")
app.add_note("Call John for project update")

# Display notes
app.display_notes()

Output:

Your notes:
- Remember to buy milk
- Call John for project update

This method involves using a text file (notes.txt) to store notes. The add_note method appends a new note to the file, and the display_notes method reads the contents of the file and prints out each note. If the file does not exist, it displays a message indicating that no notes are available.

Comments (0)

There are no comments. Be the first to comment!!!