Building a To-do Task Script In Python


Data structures are one of the most important programming concepts you can ever learn. Almost every super useful app or shiny new tool you build would require some form of storage. How else would you know the right tool to use if you don’t understand this concept?
While there are different data structure tools from lists to sets to tuples, in this article, I will try to explain the dictionary data structure in Python while walking you through building a to-do command line script. Why the dictionary? Oh, it’s the one that would fit the most for our purposes. So… Let’s waste no more time. Let’s begin.
A dictionary? What’s that?
The dictionary is known by a couple of names. Some call it the hash table and others call it the associative array, but the bottom line is they’re trying to describe a data structure, where you link one thing to the next. It’s kinda like the actual dictionary. You know the one I’m talking about, don’t you? That large book you were forced to carry about for English class (if you attended the kind of school I did anyway). In a normal dictionary, each word is mapped to a specific definition. Let’s say you want to search for “python”; as soon as you find the word “python”, you immediately get its definition. That’s the same way a dictionary works in Python. You have a key (like the word you want to search for), and immediately you get its value. Data is stored in a key-value pair.
To create a dictionary, you just initialize a variable with curly braces. The example below creates an empty dictionary.tasks = {}
Here is a dictionary with key-value pairs.person = {“name”: “John Cena”, “age”: 42, “country”: “USA”, “occupation”: “professional wrestler”}
Did you see how I mixed data types? Yes, it’s majorly strings, but the age key has an integer value and it didn’t throw any error. With dictionaries, you can mix data types. Another interesting thing you should know about dictionaries is that they’re unordered, which means they wouldn’t always be just the way you typed it. Like from the example above, if you run:print(person)
It’s possible you can get “age” before “name” or “occupation” as the first key-value pair. It makes sense because you don’t need the index to locate a value. All you need is the key. You also would not need the index to add a new key-value pair or edit the value of a key. Let’s say I want to add hair colour to the dictionary:person[“hair colour”] = “blond”
Or if I wanted to remove the age:del person[“age”]
As you can see, the key is everything. And you can mix and match data types with strings, integers and floats in a dictionary.
What should the script be able to do?
So… Building a to-do task app. Let’s describe what we want our script to do. We want a script where we can write down a task, mark it as “pending”, change it later to “completed” when the task is done, store the due date and other details, and delete a task if we are not going to do it anymore. Because this is a command-line app, there would be no need to build a fancy UI with checkboxes or reminders for when you have an overdue task. Now because we don’t want a situation where each time we run the script all our saved tasks disappear and we have to input them again, we’d have to implement data persistence. Data persistence just means the data we store remains stored even if we reload or exit and return. There are a couple of ways to do this, and the proper way would be to store data in a database, but because this is not going to be a very big program, we would store the data in a file on our local computer. Which brings us to the next concept we have to cover. Reading and writing to a file in Python.
Reading and writing to a file
In case you didn’t know, with a simple Python script, you can create a file and write stuff into it the file without opening it by yourself! And I’d be glad to show you how.
First, there are a couple of operations you need to know about this process. You can read a file (which just means you view the contents of the file). You can write to a file (which means you can change the contents of a file, which equally means you can overwrite info already stored there if you’re not careful. By the way, overwriting means replacing the old information with a totally different one). You can append (which is different from overwriting because you get to add the new information at the end of the file). So here are the steps to do this:
You open the file by calling the
open()
function. However, you should not just open the file like that. You need to open with the help of a context manager. It’s just because you have to close the file after you open it to prevent data leaks and stuff, and the context manager does it for you in case you forget. What’s a context manager you might be asking? It’s just what I call a “with statement”. I’ll show you in a bit.Then you pass the name of the file you want to read or write to, as well as the operation you want to perform as arguments to the
open()
function. The operations are normally “r” for reading, “w” for writing, “a” for appending and “r+” for both reading and writing. And that’s it!
So here’s an example with the context manager (with statement) I told you about earlier.
with open(“tasks.json”, “r”) as file:
tasks = json.load(file)
Quick side note: We’re working with JSON and the json library in Python because it closely resembles a dictionary, and it is an easy way to jump back and forth between JSON text and Python data structures.
JSON? And I don’t mean the rapper!
Because of how crucial JSON is to reading and writing to a file, I thought I should talk briefly on it here. JSON is short for JavaScript Object Notation, and it resembles a Python dictionary a lot. It has the same curly braces and key-value pairs. The difference is that while dictionaries are an in-memory Python data structure, JSON is just a string storage instrument. So when storing your Python data structure with JSON, you’d have to convert them to JSON, and convert the JSON back to Python data structure when you want to retrieve them.
To work with the JSON library, you’d first need to import it with the standard method of importation (import json
). Then you would get access to the two methods important for our work right now, load and dump. Yes, there are others but…
The json.load()
method is used to load a JSON object to a Python data structure, while json.dump()
will convert the Python data structure to JSON. I hope you can see which one we would be using when we read from a file and which one would be for writing to a file.
Building the project
With the short review we’ve done, perhaps you can already visualize what the code will look like. I’d give a brief rundown of the steps to the project and you can try it out yourself.
In writing the script, we would start with a while loop that runs as long as the condition is true, and then we pass various control flow statements to represent the various operations.
We can use the input()
function to collect user input and if/else statements to check them and base our operations. The important thing to know is:
You can store the tasks in a dictionary. In this case, we can use a nested dictionary.
In order to view all the tasks, you would need to read through the file where the tasks are stored (remember the steps we gave earlier and that
json.load()
is the method for reading).Adding a new task would mean you write to the file where you stored the tasks.
Editing a particular task would mean you getting the task from the key, and then making changes to the value.
Deleting a task would require you to use the key to remove the task from the file.
I hope you will have fun building this project. Here is the link to the code on my GitHub (click here). However, remember that you would only develop your skills the more you build projects by yourself. You can follow me and star the repository for more beginner Python projects and tutorials. Have a nice day!
Subscribe to my newsletter
Read articles from Victory Amadi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
