More Python




CS156

Chris Pollett

Sep 11, 2017

Outline

File I/O

Example Files in a Folder as a List

import glob
path = './*'
files = glob.glob(path) 
for name in files:
   print name

Strings

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 are true?

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

Iteration and Looping

Examples of things can iterate over with for

Functions

Function Scoping, Functions as Variables