Python

Python Project: ToDo List (5 Essential Skills)

Implement a ToDo list in Python

Implement a ToDo list in Python and you will learn a few things, including.

  • User interaction. You will learn the nature of how to make a user interaction with Python.
  • Loop. You will learn how to make a loop that will run until the user quits the program. Also, you will learn how to iterate over a Python list.
  • Data structures. You will learn about Python’s built-in data structure list, and see it can be used to create a ToDo list.
  • Conditionals. You will learn to create conditionals, and how to make the program behave as the user want.
  • Enumerate. You will learn how to enumerate the items in a list according to the index. This is a powerful skill to learn.

Are you ready for this?

Project Description

The ToDo list Python program you will write can do 4 things.

  1. It can show the content of your ToDo list
  2. It can add an item to your ToDo list
  3. It can remove an item from your ToDo list
  4. It can quit the program

Step 1 The Data Model

Deciding how the data is represented is the most crucial decision. Most do not think that there are many options.

This choice is not given in the project description but impacts most of the other code. To see if a data model is a good choice, you should consider how it affects the implementation of the steps.

Here we will consider the Python list as our first choice.

To see if this is a good choice, we can see how it impacts the program features.

  1. It can show the content of your ToDo list – This can be done by iterating over the list and showing items.
  2. It can add an item to your ToDo list – This can be done by appending an item to the list.
  3. It can remove an item from your ToDo list – This can be done by removing an item from the list.
  4. It can quit the program – This is not relevant to the list.

You can consider other data structures or even implement your custom class.

Step 2 Program Structure

The overall structure of the program can be implemented in a while loop with conditionals. While the data structure we use is Python list.

A great way to have it is in an infinite while loop, which will only be terminated if the user chooses the quit. option.

Let’s consider this code.

todo = []

while True:
    # print the options to the user
    print('1: Show content')
    print('2: Add an item to list')
    print('3: Remove an item from the list')
    print('4: Quit')
    # prompt the user
    option = input('Choose option: ')
    
    if option == '1':
        pass
    elif option == '2':
        pass
    elif option == '3':
        pass
    elif option == '4':
        print('Goodbye')
        break
    else:
        print('Not understood')

The quit option is implemented with a break in the infinite while loop (while True).

Now we just need to implement the rest of the code.

Step 3 Implementation

The full code is implemented here.

todo = []

while True:
    # print the options to the user
    print('1: Show content')
    print('2: Add an item to list')
    print('3: Remove an item from the list')
    print('4: Quit')
    # prompt the user
    option = input('Choose option: ')
    
    if option == '1':
        for idx, item in enumerate(todo):
            print(idx, item)
    elif option == '2':
        item = input('Add item to list: ')
        todo.append(item)
    elif option == '3':
        idx_str = input('What item to remove (use index): ')
        idx = int(idx_str)
        todo.pop(idx)
    elif option == '4':
        print('Goodbye')
        break
    else:
        print('Not understood')

We use an enumerate under option 1 to get the index of each item in the list. This makes our implementation easier, as you can use this index when you delete an item under option 2.

Want more Python projects?

This is part of 19 Python Projects and you can find another one and create the classical ELIZA in 13 lines of code.

Rune

Recent Posts

Build and Deploy an AI App

Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…

5 days ago

Building Python REST APIs with gcloud Serverless

Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…

5 days ago

Accelerate Your Web App Development Journey with Python and Docker

App Development with Python using Docker Are you an aspiring app developer looking to level…

6 days ago

Data Science Course Made Easy: Unlocking the Path to Success

Why Value-driven Data Science is the Key to Your Success In the world of data…

2 weeks ago

15 Machine Learning Projects: From Beginner to Pro

Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…

2 weeks ago

Unlock the Power of Python: 17 Project-Based Lessons from Zero to Machine Learning

Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…

2 weeks ago