top of page

07.28.2018: One Lesson of Python

Today's soundtrack is ChuggaBoom: Zodiac Arrest, a really fun nü-metal album with tongue-in-cheek lyrics, solid production, and great musicianship.

This afternoon, I'm working through the fourth chapter of Al Sweigart's Automate the Boring Stuff With Python, "Lists."

We use lists to hold information. If we make a list, each item inside of it is automatically assigned an individual integer value. We can use negative integer values to call up items at the end of the list. When calling up items from a list, we slice the list by putting a colon into our command to pull up more than one item at a time. We can use the "len" function to tell us how many items are in the list. We can use indexes to change items in a list, kind of like how we would with a variable. We can combine more than one list together with +, or multiply a list by a real number with *. We can use the "del" function to remove items from a list. Rather than reading through a list to find whether or not an item is in it, we can use "in" and "not in" to determine whether an item is contained in a list. We can use "the multiple assignment trick [to] assign multiple variables with the values in a list in one line of code" (citation). (This is a really cool one!) Another handy shortcut is using "augmented assignment operators (citation); doing so allows us to say "value += x" to add x to itself, rather than saying "value = value + x."

A method lets us modify a list by naming the list and then telling it what to do. We can use the "append" method to add an item to the end of a list; we use "insert" to put an item into a list at the desired position. We can use the "remove" method to take out a specific item from a list by calling its name; we use the "del" method to remove a numbered item from a list. We can sort a list alphabetically or chronologically, forward or backward, by using the "sort" method.

If we want to make a list-type sequence, but we don't want its values changed, we can write a tuple instead. We can use "the functions list() and tuple() [to] return list and tuple versions of the values passed to them" (citation).

Finally, if we want to make two copies of a list and only make changes to one, we can use the functions "copy" and "deepcopy"; the former copies a list, and the latter "will copy [...] inner lists as well" (citation).

​​

bottom of page