Explain ‘os’ module usages
os.mkdir(aPath) os.listdir(aPath) os.path.isdir(aPath) os.system(command)
Explain ‘sys’ module methods and variables.
sys. argv[] sys.exit() sys.builtin_module_names # Linked C modules
sys. byteorder # Native byte order
sys. check_interval Signal check frequency
sys. exec_prefix #Root directory
sys. executable #Name of executable
sys. exitfunc #Exit function name
sys. modules #Loaded modules
sys. path #Search path
sys. platform #Current platform
sys. stdin, sys.stdout, sys.stderr # File objects for I/O
sys. version_info #Python version info
sys. winver #Version number
Function definition in Python
Python function definition
def myFunction(arg1, arg2=5, arg3=”default’’):
…
return result
Python ‘List’ methods
append(item)
pop(position)
count(item)
remove(item)
extend(list)
reverse()
index(item)
sort()
insert(position, item)
Python ‘String’ methods
capitalize() *
lstrip()
center(width)
partition(sep)
count(sub, start, end)
replace(old, new)
decode()
rfind(sub, start ,end)
encode()
rindex(sub, start, end)
endswith(sub)
rjust(width)
expandtabs()
rpartition(sep)
find(sub, start, end)
rsplit(sep)
index(sub, start, end)
rstrip()
isalnum() *
split(sep)
isalpha() *
splitlines()
isdigit() *
startswith(sub)
islower() *
strip()
isspace() *
swapcase() *
istitle() *
title() *
isupper() *
translate(table)
join()
upper() *
ljust(width)
zfill(width)
lower() *
* are local-dependent for 8 bit scales.
Python ‘File’ methods
close()
readlines(size)
flush()
seek(offset)
fileno()
tell()
isatty()
truncate(size)
next()
write(string)
read(size)
writelines(list)
readline(size)
Python indexes and slices
* Indexes and Slices of a=[0,1,2,3,4,5]
len(a) –> 6
a[0] –> 0
a[5] –> 5
a[-1] –> 5
a[-2] –> 4
a[1:] –> [1,2,3,4,5]
a[:5] –> [0,1,2,3,4]
a[:-2] –> [0,1,2,3]
a[1:3] –> [1,2]
a[1:-1] –> [1,2,3,4]
b=a[:] –> Shallow copy of a
Python String formating
arg_str = “ {0} {1} {3}”.format(arg1, arg2, arg3)
Python loop using List
for pic in pic_list:
…
Python hash (dictionary): Definition
dict = {‘Alice’: ‘2341’, ‘Beth’: ‘9102’, ‘Cecil’: ‘3258’}
Python hash (dictionary): Value access
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]
Python hash (dictionary): Removing elements
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
del dict[‘Name’] # remove entry with key ‘Name’
dict.clear() # remove all entries in dict
del dict # delete entire dictionary
Python hash (dictionary): Methods
Python hash (dictionary): functions on hash data structure
Example: Use Python ‘List’ as a stack
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
Python Class Definition
class Person:
def \_\_init\_\_(self,pos):
self.pos = pos
self.alive = 1
def \_\_str\_\_(self):
return "Person #%d, %s" % (self.pos, self.alive)
# Creates a chain of linked people
# Returns the last one in the chain
def createChain(self,n):
if n\>0:
self.succ = Person(self.pos+1)
return self.succ.createChain(n-1)
else:
return self
# Kills in a circle, getting every nth living person
# When there is only one remaining, the lone survivor is returned
def kill(self,pos,nth,remaining):
if self.alive == 0: return self.succ.kill(pos,nth,remaining)
if remaining == 1: return self
if pos == nth:
self.alive = 0
pos=0
remaining-=1
return self.succ.kill(pos+1,nth,remaining)Python sorting using ‘sorted’ – this is super cool!
>>> a = [‘ccc’, ‘aaaa’, ‘d’, ‘bb’]
>>> sorted(a)
[‘aaaa’, ‘bb’, ‘ccc’, ‘d’]
>>> sorted(a)
[‘aaaa’, ‘bb’, ‘ccc’, ‘d’]
>>> len
<built-in function len>
>>> sorted(a, key=len)
[‘d’, ‘bb’, ‘ccc’, ‘aaaa’]
Given a Python list of strings, how do you sort by the last character of each string?
>>> a = [‘ccc’, ‘aaaz’, ‘d’, ‘bb’]
>>> def last(s): return s[-1]
…
>>> sorted(a, key=last)
[‘bb’, ‘ccc’, ‘d’, ‘aaaz’]
>>> “:”.join(a)
‘ccc:aaaz:d:bb’
Python tuple
Python hashtable (dictionary)
Example of using (Python) hashtable
Python files: Let’s create Unix ‘cat’ command in Python
def cat(filename):
f = open(filename, ‘rU’)
for line in f:
print line, # Note: The comma at the end removes the new line char!
f.close()
def cat2(filename): f = open(filename, 'rU') lines = f.readlines() # Read the entire lines as array of lines print lines f.close()
def cat3(filename): f = open(filename, 'rU') text = f.read() # Read the entire file as a string print text f.close()
Python regular expression module ‘re’: Basic examples
import re
## Search for pattern 'iii' in string 'piiig'. ## All of the pattern must match, but it may appear anywhere. ## On success, match.group() is matched text. match = re.search(r'iii', 'piiig') =\> found, match.group() == "iii" match = re.search(r'igs', 'piiig') =\> not found, match == None
## . = any char but \n match = re.search(r'..g', 'piiig') =\> found, match.group() == "iig"
## \d = digit char, \w = word char match = re.search(r'\d\d\d', 'p123g') =\> found, match.group() == "123" match = re.search(r'\w\w\w', '@@abcd!!') =\> found, match.group() == "abc"
Python regular expression module ‘re’: Repetition examples
i+ = one or more i’s, as many as possible.
match = re.search(r’pi+’, ‘piiig’) => found, match.group() == “piii”
## Finds the first/leftmost solution, and within it drives the + ## as far as possible (aka 'leftmost and largest'). ## In this example, note that it does not get to the second set of i's. match = re.search(r'i+', 'piigiiii') =\> found, match.group() == "ii"
## \s\* = zero or more whitespace chars ## Here look for 3 digits, possibly separated by whitespace. match = re.search(r'\d\s\*\d\s\*\d', 'xx1 2 3xx') =\> found, match.group() == "1 2 3" match = re.search(r'\d\s\*\d\s\*\d', 'xx12 3xx') =\> found, match.group() == "12 3" match = re.search(r'\d\s\*\d\s\*\d', 'xx123xx') =\> found, match.group() == "123"
## ^ = matches the start of string, so this fails: match = re.search(r'^b\w+', 'foobar') =\> not found, match == None ## but without the ^ it succeeds: match = re.search(r'b\w+', 'foobar') =\> found, match.group() == "bar"