Today: function call, grayscale, boolean precedence, list functions, midterm next week
How do function call parameters work? It's often intuitive, so you don't have to think about it too much. BUT when pushed, you need to know how it works to avoid getting tripped up.
1. The variables and parameters in each function are independent, sealed off from those in other functions. An "x" in one function is independent of an "x" in some other function.
2. Function call: parameter values come in by position within the ( .. ) (not by name or anything else)
Say we have a "foo" function with 2 parameters, and it is called by a "caller" function later. What does the run of caller() below print? This is very detail oriented, but mercifully there's just a few lines.
def foo(x, y):
x = x - y
return x + 1
def caller():
x = 2
y = 3
z = foo(y, x)
print(x, y, z)
Solution
printed line: 2 3 2 Values passed in to foo(x, y) are 3 and 2, value returned is 1 The "x" within foo() is independent of the "x" in caller()
>>> # + - * / with float works, yielding floats >>> # more in future >>> 3.14 * 2 6.28 >>> >>> # int math - indexing with [ ] >>> s = 'Hello' >>> len(s) - 4 1 >>> s[1] 'e' >>> >>> # indexing is fundamentally *int* only >>> s[1.0] TypeError: string indices must be integers
This does not work, but it's close
def good_day(age, weekend, raining):
"""age is int, weekend and raining are Boolean"""
if age < 30 or weekend and not raining:
print('good day')
Solution
def good_day(age, weekend, raining):
"""age is int, weekend and raining are Boolean"""
if (age < 30 or weekend) and not raining:
print('good day')
4.parse_words99: Like parse_words(), but with an extra way for a word to extend. Given a string s, parse out and return all "words", where a word is made of 1 or more adjacent alphabetic chars. Except, digits are allowed within a word after the first char, so 'ab12 6 a34a' returns ['ab12', 'a34b'].
Solution
def parse_words99(s):
search = 0
words = []
while True:
begin = search
while begin < len(s) and not s[begin].isalpha():
begin += 1
if begin >= len(s):
break
# Your code here, find end of word
end = begin + 1
# Skip over both alpha/digit
# Parenthesis added to force and/or order
while end < len(s) and (s[end].isalpha() or s[end].isdigit()):
end += 1
word = s[begin:end]
words.append(word)
search = end + 1
return words
More details see: Python Lists
>>> nums = [] >>> nums.append(1) >>> nums.append(0) >>> nums.append(6) >>> >>> nums [1, 0, 6] >>> >>> 6 in nums True >>> 5 in nums False >>> 5 not in nums True >>> >>> nums[0] 1 >>> >>> for num in nums: ... print(num) ... 1 0 6
>>> a = [1, 2, 3] >>> b = [4, 5] >>> a.append(b) >>> # what is a now? ... >>> a [1, 2, 3, [4, 5]] >>> >>> c = [1, 2, 3] >>> d = [4, 5] >>> c.extend(d) >>> c [1, 2, 3, 4, 5]
>>> a = [1, 2, 3] >>> b = [9, 10] >>> a + b [1, 2, 3, 9, 10] >>> a # original is still there [1, 2, 3]
>>> a = [1, 2, 3] >>> b = [4, 5] >>> a += b >>> a [1, 2, 3, 4, 5]
>>> lst = ['a', 'b', 'c'] >>> lst2 = lst[1:] # slice without first elem >>> lst2 ['b', 'c'] >>> lst ['a', 'b', 'c'] >>> lst3 = lst[:] # copy whole list >>> lst3 ['a', 'b', 'c'] >>> # can prove lst3 is a copy, modify lst >>> lst[0] = 'xxx' >>> lst ['xxx', 'b', 'c'] >>> lst3 ['a', 'b', 'c']
>>> lst = ['a', 'b', 'c'] ['a', 'b', 'c'] >>> lst.pop() # opposite of append 'c' >>> lst ['a', 'b'] >>> lst.pop(0) # can specify index 'a' >>> lst ['b']
Midterm - Fri Nov 1, at regular class time. Talk about next week. What to know now:
Midterm is closed-note, writing functions. The functions look a lot like functions from homework/section/lecture - be able to do those. Re-visit things you have solved before, maybe with help. Practice to be able to solve them cold. We give partial credit and do not grade on syntax.
See also: Midterm Prep