What I Learned In Python This Week #2: Lists, and How To Manipulate Them

Tools I Used:

  • Geany
    • My favorite mini-IDE!
  • Python shell / bash shell
    • I use the Python shell to learn functions interactively, and the bash shell to execute the scripts I write.

Accomplishments:

  • Date Translator (date.v1-1-1.py)
    • Kind of a bug release, I guess — I fixed a problem where single-digit days were showing up with an unnecessary initial zero.
  • Domain Name Translator (domainname.v1-0-0.py)
    • A simple script that strips the domain name from a URL. Basically just a proof-of-concept to help me understand lists in Python a bit better.
  • Quotation Box (quotationbox.v1-0-0.py)
    • Takes a line of user input and centers it in a ASCII text box in their terminal.

Functions/Concepts I Learned:

  • Lists
    • Lists are essentially sequences of data that you can manipulate in any number of ways — and manipulating data is what programming is all about!
    • [], [:], and [::] let you index, slice, and choose step size of the slice respectively. These are tools that by themselves only return the value of a list, but can be used in conjunction with other functions can powerfully manipulate lists or parts of lists.
    • None is Python’s concept of, not zero, but nothing at all; it is a null or an unset value. This is useful for filling a list up that you will put data in later.
    • len(), min(), and max() let you check the length of a list, the smallest member in a list, and the largest member in a list respectively.
    • del lets you delete members from a list.
  • Tuples
    • I think tuples are a bit above me at this point, but I understand them to be similar to lists, only non-modifiable once created. Useful for dictionaries, or perhaps an internal checksum.
  • Methods
    • Methods are really awesome! They are like functions, only you tell methods to work only on specific parts of a sequence, rather than the whole. Several of these can be implemented using slices, but using methods is less ugly looking and much more human-readable.
    • .append adds a single data point to the end of a list, while .extend lets you add multiple data points to the end of a list (including adding other lists).
    • .count counts the occurrences of elements in a list, but does not examine sublists (i.e., for numbers = [1,2,1,1,2], numbers.count(2) will return 2, but for numbers = [1,2,1,[1,2]], it would return only 1).
    • .index returns the index value of the first occurrence of your search term in a given list, and .remove will search for and remove the first occurrence of the term. However, these only deal with the first occurrence — if the value appears multiple times in a list, you will need multiple .index/.remove operations to deal with them, or else some other Python function I don’t know about yet.
    • .pop is interesting because it “pops” (i.e., deletes) the last entry in a list out and returns the value. According to Hetland, this is a way to implement a stack, a word which I have heard a lot concerning programming, but have never really heard explained.
    • .reverse reverses the elements in a list (not just returning them reversed like the reversed function).
    • .sort sorts the elements in a list in ascending order (not just returning them sorted like the sorted function). You can use the cmp, key, and reverse arguments to sort by various other means than the default ascending order.
  • list() converts a sequence to a list, and tuple() similarly converts a sequence to a tuple.
  • cmp lets you compare two values, returning 0 for a match, 1 when the first value is larger, and -1 when the first value is smaller.

This week I finally got lists, and it was very rewarding; I feel much more confident to take on the rest of Python now. While I didn’t learn any cool new functions, I learned a lot about how to put lists to work for me, and I think it was worth it. Next week, I will try to get through Hetland’s chapter on strings, and I think it will be similarly devoid of new functions, but I’ll sure learn a lot about how to play around with strings!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s