More Python




CS156

Chris Pollett

Sep 10, 2014

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

Iteration and Looping

Examples of things can iterate over with for

Functions

Function Scoping, Functions as Variables

Generators

Coroutines

Objects and Classes