More Python




CS156

Chris Pollett

Feb 9, 2015

Outline

Lists

Example Using Lists and Command Line

import sys
if(len(sys.argv)) != 2: 
   #notice sys.argv is a list of command-line args and we found its length
   print "Please supply a filename"
   raise SystemExit(1) # throw an error and exit
f = open(sys.argv[1]) #program name is argv[0]
lines = f.readlines() # reads all lines into list one go
f.close()

#convert inputs to list of ints
ivalues = [int(line) for line in lines]

# print min and max
print "The min is ", min(ivalues)
print "The max is", max(ivalues)

Tuples

Sets

Dictionaries

Quiz

Which of the following is true?

  1. `A^(star)`-search is an informed search algorithm.
  2. The space complexity of iterative deepening depth first search is asymptotically worse than breadth-first search.
  3. Python has switch/case.

Iteration and Looping

Examples of things can iterate over with for

Functions

Function Scoping, Functions as Variables

Generators

Coroutines

Objects and Classes