Skip to main content

Writing a simple Todo App

Our app will be a simple todo list. The user can add a new todo. First, create a file called main.py, then we will need to store a list of todos in our app. Let's create a todos variable as an empty list. Then we can add a new function to add a todo to the list.

Add Todo

# main.py

todos = []

def add_todo(todos: list, todo: str):
todos.append(todo)
return todos

todos = add_todo(todos, "Buy milk")
print(todos)

In the above code, we are using append function. It is a built in function in Python to add an item to a list.

Run the above code and you should see ['Buy milk'] printed to the console.

Using main function

Python is an interpreted language and don't require a main function like C or C++. So, the above program can be run directly. But, to make it easy for other developer to read it. I suggest to create a main function to be our app entry point and to orchestrate the flow of our app.

# main.py

def main():
todos = []

todos = add_todo(todos, "Buy milk")
print(todos)

def add_todo(todos: list, todo: str):
todos.append(todo)
return todos

# this will run the `main` function when we run the program `python3 main.py`
if __name__ == "__main__":
main()

In above code, we put our todos variable inside the main function. Then todos variable is passed as argument to the add_todo function, and the result is assigned back to todos variable. This way, we can say that the todos is holding our app state. It's a good practice to keep the app state in one place. That's way we create a main function to orchestrate the flow of our app.

Now, we can add a new todo to our list. But, we need to print the list of todos to the user. Let's create a new function called print_todos to print the todos to the user. And let's test it by adding more todos.

# main.py

def main():
todos = []

todos = add_todo(todos, "Buy milk")
todos = add_todo(todos, "Wash shoes")
todos = add_todo(todos, "Print assignment")
print_todos(todos)

def add_todo(todos: list, todo: str):
todos.append(todo)
return todos

def print_todos(todos: list):
for todo in todos:
print(f"- {todo}")

# this will run the `main` function when we run the program `python3 main.py`
if __name__ == "__main__":
main()

The print_todos function will print the todos as bullet items. The print(f"- {todo}") is a way to print a string in Python 3.6 and above. It's called f-string. It's a more readable way to format a string. You can read more about it here.

Remove a todo

To remove a todo, we need to know the index of the todo in the list. Then we can use the pop function to remove the todo from the list. Let's create a new function called remove_todo to remove a todo from the list.

# main.py

def main():
todos = []

todos = add_todo(todos, "Buy milk")
todos = add_todo(todos, "Wash shoes")
todos = add_todo(todos, "Print assignment")
print_todos(todos)

print("Removing todo at index 1")
todos = remove_todo(todos, 1)

print("----")
print_todos(todos)

def add_todo(todos: list, todo: str):
todos.append(todo)
return todos

def print_todos(todos: list):
for todo in todos:
print(f"- {todo}")

def remove_todo(todos: list, index: int):
todos.pop(index)
return todos

# this will run the `main` function when we run the program `python3 main.py`
if __name__ == "__main__":
main()

remove_todo have two arguments, todos and index. What it do is to remove the todo at the given index from the list. Then it return the list of the updated todos. In the above code, we remove the todo at index 1, which is Wash shoes. Then we print the updated list of todos. And you can see the Wash shoes is removed from the list.

In the next section, we will add mark as done feature to our app by introducing a data structure.`