For each of these sequences of statements, what does Python print?
def triple(n: int) -> int:
''' Return three times the parameter
'''
return n * 3
print('Four')
print(triple(5))
print('Score')
print(triple(2) + triple(10))
print(triple(3), triple(100))
Four
15
Score
36
9 300
def print_n_copies(n: int, s: str):
''' Print specified number of copies of string
'''
print(n * s)
return
print('Heads')
print_n_copies(4, 'Flip')
print('Tails')
Assume you have the following definitions:
a = 5
Animal = namedtuple('Animal', 'name species age weight')
L = ['Tinker', 'Evers', 'Chance']
What is the data type of each of the following expressions? Choose from int, float, bool, str, list, or Animal.
a * 10
3.14159
'rhinoceros'
12 * (5 + 1)
len('Hippopotamus')Animal('Roger', 'rhinoceros', 45, 1500)Animal('Roger', 'rhinoceros', 45, 1500).age[2, 4, 6, 8, 10]
L
L[1]
For each of these sequences of statements, what does Python print?
s = 'Duck'
t = 'Duck'
print('Donald')
if s >= t:
print('Huey')
print('Dewey')
print('Louie')
Donald
Huey
Dewey
Louie
a = 10
b = 20
if a >= b:
print('Huey')
else:
print('Dewey')
print('Louie')
Dewey
Louie
p = 'Mickey'
q = 'Minnie'
print('Mouse')
if p == q:
print('Goofy')
print('Pluto')
Mouse
Pluto
L = ['Huey', 'Dewey', 'Louie']
print('Nephews')
for duck in L:
print(duck)
print('Uncle Donald')
Nephews
Huey
Dewey
Louie
Uncle Donald
L = [2000, 2004, 2008, 2012]
print('Election years')
for i in range(4):
print(i, "--", L[i])
print('Remember to vote!')
Election years
0 -- 2000
1 -- 2004
2 -- 2008
3 -- 2012
Remember to vote!
Write a function called abbr that takes two non-empty strings (i.e., you can assume each string is at least one character long; you don't have to check). It should return a two-character abbreviation constructed of the first character of each of the input strings. For example,
abbr('University', 'California')
returns 'UC'. Your definition should follow the design recipe by including: (a) type specifications for the parameters and return value, (b) a docstring comment, and (c) at least two example calls with the expected answers that can be run as tests; these can be print statements or assert statements.
def abbr(s1: str, s2: str) -> str:
''' Return abbreviation made from first char of each parameter
'''
return s1[0] + s2[0]
print(abbr('University', 'California'), 'should be UC')
print(abbr('Los', 'Angeles'), 'should be LA')
Why is it a good idea to avoid duplicate code in our programs?
In this Python code:
def double(n: int) -> int:
''' Return twice the parameter value '''
return 2 * n
print(double(13), "should be 26")
identify each of the following:
(On a paper exam, you could circle portions of the code and draw arrows. Electronically, just copy the list and, after each item, copy the text that applies.)
-- function name (in definition): double [first occurrence]
-- function definition: def line and next two lines
-- function call (of the function double): double(13)
-- argument to a call to double: 13
-- parameter definition in double: (n: int)
-- parameter use in double: 2 * n
-- return type specification: -> int
-- docstring comment ("purpose statement"): triple-quoted string [line 2]
Write a function called least_expensive_dish that takes a list of restaurants and returns the name of the least expensive dish on the list.
There are two approaches to this; either one is fine. The first approach, as in Lab 3, uses the sort method on lists with the key= argument and the name of a function like this one:
def Restaurant_price(r: Restaurant) -> float:
''' Return the value of the price attribute of the parameter
'''
(You don't have to define this function; assume it already exists.)
The second approach just goes through the list with a loop, keeping track of the lowest-priced item it has found.
def least_expensive_dish(L: list) -> str:
''' Return the name of the least expensive dish in the list
'''
L.sort(key=Restaurant_price)
return L[0].dish
def least_expensive_dish2(L: list) -> str:
''' Return the name of the least expensive dish in the list
'''
lowest_price_so_far = L[0].price
lowest_dish_so_far = L[0].dish
for r in L[1:]:
if r.price < lowest_price_so_far:
lowest_price_so_far = r.price
lowest_dish_so_far = r.dish
return lowest_dish_so_far