Say I make a program that keeps track of the days I worked and the hours I worked, would I use a dictionary? And how would I differentiate from a Monday on week 1 from a Monday on week 2? How do I get it to store this information after I close the program? (Python Language)
Share
A dictionary is a good way to store the data while your program is running.
There are a number of ways to add some data permanence (so it’s around after you close the program). The Python modules pickle and shelve are useful and easy to use. One issue with these is that you can’t easily inspect the data outside of a Python program. There are also modules to read and write text files in the JSON and XML formats, and JSON is particularly easy to read in a text editor. If you aren’t already proficient, the databases like MySQL are way more than you need for a personal program like you mention and unless you want to invest some time into learning how to use them, you should go with a simpler solution.
As for the Monday on week 1 vs week 2, you have many options. You could use the actual date (this seems like a good idea to me), or you could key the dictionary with tuples, like (“Monday”, 1). The main rule is that dictionary keys must be immutable (ints, strings, tuples — that contain only immutable objects, etc), but not (dictionaries, lists, etc).