15-112 Lecture 8 (Friday, July 11, 2014)

Command-line arguments

You may have noticed that, when you start some programs, you can provide them with inputs as the command line, for example, "ls examples", or "touch example3.py", or "chmod +x example2.py". This is because the operating system loader provides running programs with access to these command-line arguments. In Python it does this via a list, where the 0th argument is the name of the script, itself.

To get access to this functionality, we need to "import" the system module. In other words, we need to ask the Python interpreter to make use of some code that is installed on the system, and organized as a module, but that we didn't write.

Let's see an example:


#!/usr/bin/python

import sys

if (len (sys.argv) == 1):
  print "Only the name of the program has been provided."
else:
  print "There are " + str(len(sys.argv)) + " arguments, including the name of the script."

print "The name of the script is " + sys.argv[0] + "."

index = 1
while (index < len(sys.argv)):
  print sys.argv[index]
  index = index + 1

for argument in sys.argv:
  print argument

  

What are the take-aways?

Tuples

In an abstract sense, tuples are different than lists. But, in practice, as implemented in Python, they are essentially immutable Lists. I don't want to spend a lot of time on them, because that basically sums it up. They work the same way -- except that mutators, such as insert(), remove(), append(), etc, don't exist.

About the only detail worth noting is that they are declared using ()-parenthesis, rather than []-brackets. An example is below:

Let's see an example:


#!/usr/bin/python

person1 = ("Gregory Kesden", "Faculty", 1999)

print person1[0]
print person1[1]
print person1[2]

print ""

for attribute in person1:
  print attribute

  

Choosing Tuples vs. Lists

From an under-the-hood perspective, one would expect tuples, where applicable, to be the more efficient and thereby preferable choice. In general, a simpler fixed-format data structure is smaller and can be represented internally in more efficient ways that one that might need to grow and has methods to support this.

Having said that, in practice, this type of low-level optimization is rarely on the minds of Python programmers, who tend to be focused on rapid development, while maintaining correctness. To keep code digestable by everyone, they tend to have conventions and follow them, keeping it simple.

Consistent with the capabilities and conventions, most Python programmers would:

Sets

Sets are another type of collection. But, unlike lists and tuples, they are not ordered or indexed. Instead, they are just bags of things. In general, what one does with a set is put things in, take things out, and/or test to see whether or not something or things are members.

Lists were created using []-brackets. Sets were created using ()-parentheses. Sets, oddly, are created using both! Their initial items are listed between ([ and ]). Also, note that, in the example below, the keyword set is required, unlike for lists, tuples, or dictionaries (later).

A single item can be added to a set using add(), a list of items can be added to a set using update(), and a single item can be removed using remove().

There are 4 key binary operations upon sets:

Let's look at an example:


#!/usr/bin/python

instructors = set (["Greg", "Dave", "Adam", "Jamie", "Kelly"])
faculty = set (["Greg", "Dave", "Cohen", "Kamlet", "Rob"])
gradstudents = set (["Adam", "Jamie", "Kelly", "Richard", "Sarah", "Sam", "Rob"])

print "people"
people = faculty | gradstudents
print people

print ""

print "gradinstructors"
gradinstructors = instructors & gradstudents
print gradinstructors

print ""

print "facultyinstructors"
facultyinstructors = instructors & faculty
print facultyinstructors

print ""

print "gradAndFaculty"
gradAndFaculty = faculty & gradstudents
print gradAndFaculty

print ""
  

Dictionaries

Dictionaries are a very useful collection. They store key-value pairs in the same way that, for example, a dictionary of words and definitions stores definitions associated with their words -- we look up the words to find the definitions.

Dictionaries, as a form of collection, store values indexed by keys. What I mean by "indexed by keys" is finable by looking up the key. Essentially, a Dictionary operates like a List -- except that you can pick the index for each item, rather than being limited to sequential integers.

As an aside, just in case you hear the phrase, in some languages, such as Perl, dictionaries are known as "associative arrays".

Like the syntax for lists used []-brackets, and tuples ()-parenthesis, dictionaries use {}-squiggly-brakcets.

Let's look at an example:


#!/usr/bin/python

offices = {
  "Greg" : "GHC 7711",
  "Tom" : "GHC 4117",
  "Dave" : "GHC 5001"
}

# Look up a single name
print "Greg's office is " + offices["Greg"]
print "Tom's office is " + offices["Tom"]


# Convert the Dictionary to a list, to get a list of keys
keys = list(offices)
for name in keys:
  print name

# Iterate through the list to get the values
for name in keys:
  print offices[name]

# Remove Greg's entry
print offices
del offices["Greg"]
print offices

# Add a new entry
offices["Gregory"] = "GCH 7711"
print offices
  

Some things to note: