Building a Todo List Application with Python



Learn how to create a Todo list application using Python with our easy-to-follow tutorial. Add, remove, and view tasks in this organized app. Perfect for managing your daily tasks efficiently.


Introduction:

In this tutorial, we will guide you through the process of creating a simple Todo list application using Python. This project will help you organize your tasks and keep track of your progress. By following the step-by-step instructions and using Python code, you'll be able to build your own functional Todo list application.


Step 1: Setting Up the Environment

Make sure you have Python installed on your computer. You can download and install the latest version of Python from the official Python website (https://www.python.org/downloads/). Choose the appropriate version for your operating system and follow the installation instructions.


Step 2: Creating a New Python File

Open your preferred Python editor or IDE and create a new Python file (e.g., `todo_app.py`).


Step 3: Writing the Python Code

Copy and paste the following code into the `todo_app.py` file:


```python

class Todo:

    def __init__(self):

        self.tasks = []


    def add_task(self, task):

        self.tasks.append(task)

        print("Task added successfully!")


    def remove_task(self, task):

        if task in self.tasks:

            self.tasks.remove(task)

            print("Task removed successfully!")

        else:

            print("Task not found!")


    def view_tasks(self):

        if len(self.tasks) > 0:

            print("Tasks:")

            for task in self.tasks:

                print("- " + task)

        else:

            print("No tasks found!")


def main():

    todo = Todo()

    while True:

        print("\nTodo List Menu:")

        print("1. Add Task")

        print("2. Remove Task")

        print("3. View Tasks")

        print("4. Exit")


        choice = input("Enter your choice (1-4): ")


        if choice == "1":

            task = input("Enter a task: ")

            todo.add_task(task)

        elif choice == "2":

            task = input("Enter the task to remove: ")

            todo.remove_task(task)

        elif choice == "3":

            todo.view_tasks()

        elif choice == "4":

            break

        else:

            print("Invalid choice. Please try again!")


if __name__ == "__main__":

    main()

```


Explanation of the code:


1. We define a class called `Todo` to encapsulate the functionality of our Todo list application.


2. Inside the `Todo` class, we define an `__init__` method, which serves as the constructor. It initializes an empty list called `tasks` to store our tasks.


3. The `add_task` method takes a `task` as input and appends it to the `tasks` list. It provides feedback to the user that the task has been added successfully.


4. The `remove_task` method takes a `task` as input and checks if it exists in the `tasks` list. If the task is found, it is removed from the list. If the task is not found, an appropriate message is displayed to the user.


5. The `view_tasks` method checks if there are any tasks in the `tasks` list. If there are tasks, it iterates over the list and displays each task with a bullet point. If there are no tasks, a message indicating no tasks are found is displayed.


6. The `main` function is the entry point of our program. It creates an instance of the `Todo` class called `todo`.


7. Inside the `main` function, we run a while loop to continuously display a menu to the user and perform actions based on their input.


8. The menu options include adding a task, removing a task, viewing tasks, and exiting the aplication.


9. Depending on the user's choice, the corresponding method from the `Todo` class is called to perform the desired action.


10. The loop continues until the user chooses to exit by entering "4".


Step 4: Running the Todo List Application

Save the Python file and open your command prompt or terminal. Navigate to the directory where your Python file is located. Run the following command to execute the Todo list application:


```Terminal Commands

python todo_app.py

```


The application will display a menu with options to add tasks, remove tasks, view tasks, or exit. Enter the corresponding number to perform the desired action.


Conclusion:

Congratulations! You have successfully built a simple Todo list application using Python. By understanding the code, you can modify and expand the functionality to suit your specific requirements. You can add features like task priority, due dates, or even a graphical user interface to enhance the application further. Python's flexibility allows you to customize and adapt the Todo list application to meet your specific needs. Now you have a practical tool to help you stay organized and manage your tasks efficiently. Happy coding!

 

Buy Me a Chai ! ☕ 

If You are an Indian , Please Give me some Coins 🥺

UPI works only in India



Googlepay  phonpe PayTm UPI  



If you're Not Indian 🥺 Please Send Me a Thanks or Amazon Gift Card to the E-mail

websitehaneen@gmail.com


Have a Nice Day 

*

Post a Comment (0)
Previous Post Next Post