Professional Documents
Culture Documents
Python 03
Python 03
>>> i = word.find(‘es’)
>>> print(i)
EXAMPLES
>>> i = word.find(‘es’, 3)
>>> print(i)
EXAMPLES
>>> i = word.find(‘t’, 1, 3)
>>> print(i)
THE in OPERATOR
>>> ‘n’ in ‘banana’
True
>>> ‘seed’ in ‘banana’
False
EXAMPLE
“variable”
>>> print( [] )
[]
GO THROUGH THE LIST
for i in [5, 4, 3, 2, 1] : 5
4
print(i)
3
print('Blastoff!‘) 2
1
Blastoff!
LIST TRAVERSAL
friends_cast[0] = ‘Monica’
print(friends_cast)
MUTABILITY: LIST VS
STRINGS
“variable”
>>> t1 = [1, 2]
>>> t1.append(3)
>>> print(t1)
[1, 2, 3]
LIST INSERT METHOD
>>> l = [1, 2]
>>> print(l)
[1, 2, 4]
LIST INSERT METHOD
>>> l = [1, 2]
>>> print(l)
[1, 2, 4]
LIST INSERT METHOD
>>> l = [1, 2]
>>> print(l)
[1, 4, 2]
LIST METHODS
>>> t1 = [1, 2]
>>> t2 = t1.append(3)
>>> print(t1)
[1,2,3]
>>> print(t2)
None
‘is’ OPERATOR
Given two strings
a = ‘banana’
b = ‘banana’
key-value pair
DICTIONARY
Lists index their entries based on the position in the list.
“lookup tag”
DICTIONARY
>>> eng2sp = {}
dict()
>>> print(eng2sp)
{}
DICTIONARY
>>> print(eng2sp)
{'one': 'uno'}
DICTIONARY
>>> print(eng2sp)
>>> item_price = {}
>>> item_price[‘milk’] = 10
>>> print(item_price[‘milk’])
10
DICTIONARY
>>> item_price = {}
>>> item_price[‘milk’] = 10
>>> print(item_price[‘sugar’])
KeyError: ‘sugar’
DICTIONARY
>>> number_of_days[‘February’]
ITERATION ON DICTIONARIES
Even though dictionaries are not stored in order, we can
write a for loop that goes through all the entries in a
dictionary - actually it goes through all of the keys in the
dictionary and looks up the values
ITERATION
eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
for k in eng2sp:
print(k)
ITERATION: VALUES
eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
for k in eng2sp:
print(eng2sp[k])
‘in’ OPERATOR
>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
>>> counts.values()
>>> counts.items()
Tuples
Tuples
a sequence of values
>>> t1 = ()
>>> t2 = (1)
(1,)
Tuple Assignment
a, b = 2, 3
Return values
def divmod(a,b):
return a//b, a%b
Dictionaries & Tuples
>>> t = d.items()
[(‘b’,18),(‘a’,12)]
Looping
Input Central
and Output Processing
Devices Unit
Secondary
if x< 3: print Memory
Main
Memory
Persistence?
Others: Persistent
Web servers
Operating Systems
File
filename is a string
mode is optional and should be 'r' if we are
planning reading the file and 'w' if we are
going to write to the file
returns a handle use to manipulate the file
open modes
r
w
a
rb
wb
Text file processing
of lines
A text file has newlines at the end of each
line
File handle as a sequence
A file handle open for read can be treated as a
sequence of strings where each line in the file
is a string in the sequence
We can use the for statement to iterate through
a sequence
Remember - a sequence is an ordered set
File handle as a sequence
or TRACEBACK
Exceptions
success_failure_ratio = num_success/num_failure
print(‘success/failure = ’, success_failure_ratio)
success_failure_ratio = num_success/num_failure
print(‘success/failure = ’, success_failure_ratio)
except IOError:
ZeroDivisionError:
print(‘Division by zero’)
Back to opening file
try:
open(file_name, ‘r’)
except IOError:
print(‘File’, file_name, ‘does not exist’)
The try ... except Clause
try:
<body>
except <exceptionType1>:
<handle1>
except <excetpionType2> as e:
<handle2>
finally:
<finalize_process>
Best practices
Defensive programming
x,y = eval(input(‘Enter integers x,y for the ratio x/y: ’)
try:
print(‘x,y = ’, x/y)
except ZeroDivisionError:
print(‘division by zero’)
except:
raise ValueError(‘Bad Arguments’)
Defensive programming
x,y = eval(input(‘Enter x,y for the ratio x/y: ’)
assert type(x)==int and type(y) == int
try:
print(‘x,y = ’, x/y)
except ZeroDivisionError:
print(‘File’, file_name, ‘does not exist’)