Vous êtes sur la page 1sur 3

CMPS 5P Notes for November 7, 2016

Tuples are immutable, but look a lot like lists.


- One use of this is using a tuple as a key in the dictionary, since its immutable.
len((1,2,a)) = 3
len((1,2,[a])) = 3
These are both perfectly legal as far as Python is concerned
t = (1,2,[a])
t[2].append(b)
>>> t = (1,2,[a,b])

Tuples themselves are immutable, though this list contained therein can still
be changed, making the whole tuple kind of mutable, and unfit for use in a
dictionary.

t = (1,2,(a)) This, however, could be used as a dictionary key, since a nested tuple is still immutable.
t = tuple(abc)
t = (abc, )
>>>(abc, )
You either need the comma or the actual tuple command. Just not a parenthesis by
itself
t = (abc)
>>> abc
t[0] = 9
t.append(b) As you can see, its still immutable.
>>> TypeError
a,b = b,a This accomplishes the task of switching the assignment of b and a in the tuple.
num = 5
success (True)
return (nun,success)
email = elm@ucsc.edu
email.split(0)
>>> cruzid,domain = email.split(0)
print(cruzid)

L1 = [Ethan, Oceane, Jennifer]


The zip fn combines like a zipper, but will stop at the shorter
L2 = [elm, obel, jsawyer]
list if there is a length discrepancy. Can work with >2 lists
z = zip(L1,L2)
list(z)
>>>[(Ethan, elm), (Oceane, obel), (Jennifer, jsawyer)]
for t in zip(L1,L2):
print({0}s CruzID is {1}.format(t[0],t[1])
>>> Ethans CruzID is elm
for name, cruzid in zip(L1,L2):
print({0}s CruzID is {1}.format (name,cruzid)
>>> Ethans CruzID is elm

for t in zip(L1,L2):
print({0}s CruzID is {1}.format (*t)
>>> Ethans CruzID is elm
def mysum(*args):
total = 0
for i in args:
total += 1
return total
mysum (1,2,3,4)
>>> 10
Hrs = [a,b,c]
freqs = [31,10,20]
L = list(zip(Hrs,freqs))
>>> [(a,31),(b,10),(c,20)]
L = list(zip(freqs,Hrs))
>>> [(31,a),(10,b),(20,c)]
CMPS 5P Notes for November 9, 2016

Note: a no vowels function uses a string of vowels and eliminates if a character is in the string.
Vocab: parameters are the arguments put into a function
Vocab: return is what the function does and spits out after
- When putting in comments on your code, make sure to include these phrases.
Note: typing in help(function_name) will generate the print placed in a 3-quotation note inside a
function. This is where youd put information on what the function does, its parameters and its
return
Show here is another way of getting the help() command for a built-in function:
Help on built-in function log in module math:
>>>log()
log(x[, base])
Return the logarithm of x to the given base.
If the base not specified, return the natural logarithm (base e) of x.
LBYL: Look Before You Leap
EAFP: Easier to ask forgiveness than permission.

Exceptions up until this point have meant that our program had failed, but there are ways to deal with
exception that we will learn right now.
try:
x=0
y=5
z = x/y
print( z is {0}.format(z))
except ZeroDivisionError as e:
print(Divide by Zero!)
print(e.args)
except NameError as e:
print(Undefined variable)
except:
print(Another error)
#Finally:
#
print(Always do this!)
print(I finished! Yay!)
This would normally run smoothly, but switching x and ys values will yield the ZeroDivisionError.
The good thing about the try and except prompts are that they do not crash the program when
exceptions arise. You would use this in your program when you wish to attempt something that may
result in error, but you dont wish to crash your program.
def open_a_file:
done = False
while not done:
filename = input(Please input a file name:)
try:
fobj = open(filename)
done = True
except:
done = False
return fobj
Note: This would effectively be a loop that repeated until a valid input was reached without crashing
the program every time a not acceptable name was entered

Vous aimerez peut-être aussi