Python Functions
What is the difference between *args and **kwargs?
*args passes a variable number of non-keyworded arguments list, while **kwargs passes a variable number of keyword arguments dictionary
total = 0 def increment(): global total total += 1 return total increment() What is printed?
1
first_lambda = lambda x: x+5 first_lambda(10) What is printed?
15
def sum_vals(*args): total = 0 for val in args: total += val return total sum_vals(1, 2, 3, 4, 5, 6) What is printed? (Hint: is there an error)
21
len('max') What is printed?
3
any([val for val in [1,2,3] if val > 5]) What does this function print? "True" or "False"
False
student = 'Max' def say_hello(): return f'Hello {student}' say_hello() What is printed?
Hello Max
total = 0 def increment(): total += 1 return total increment() What is printed?
There is an error
all([num for num in [4,2,10,6,8] if num % 2 == 0]) What does this function print? "True" or "False"
True
five_by_two = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)] list(zip(*five_by_two)) What is printed?
[(0, 1, 2, 3, 4), (1, 2, 3, 4, 5)]
list(zip([1,2,3], [4,5,6])) What does this list print?
[(1,4), (2,5), (3,6)]
What is a function?
a process for executing a task, that can accept input and return output, and is useful for executing similar procedures over and over
map
a standard function that accepts at least two arguments, a function and an iterable
Write a function to add 2 values
def add(a,b): return a+b
Write a function called decrement_list, that accepts a single list as a parameter, and returns the list of items decremented by 1
def decrement_list(n): return list(map(lambda x: x-1, n))
Write a function called 'extremes,' which accepts the iterable. It should return a tuple containing the minimum and maximum elements.
def extremes(nums): return(min(nums), max(nums))
Write a code with the function favorite_animals using **kwargs. It should print "Max's favorite animal is a pig" and "Colt's favorite animal is a cat"
def favorite_colors(**kwargs): for key, value in kwargs.items(): print(f"{key}'s favorite animal is a {value}") favorite_colors(max='pig', colt='cat')
Implement a function is_all_strings that accepts a single iterable and returns True if it contains ONLY strings
def is_all_strings(lst): return all(type(l) == str for l in lst)
Write a function called 'multiple_letter_count' which takes one parameter (string) and returns a dictionary with keys being letters and values being the count of the letter
def multiple_letter_count(string): return {letter: string.count(letter) for letter in string}
l = [1, 2, 3, 4] Write a code to print the even values of the array list l, with the variable "evens"
evens = list(filter(lambda x: x%2==0, 1)) evens
return
exits the function, outputs the value after return keyword, and pops function off of the call stack
Write a code using lambda to multiply two values x and y
lambda x, y: x*y
[3,4,1,2] Write a function to print the maximum value of this unnamed list, which is '4'
max([3,4,1,2])
[3,4,1,2] Write a function to print the minimum value of this unnamed list, which is '1'
min([3,4,1,2])
Why do we use functions?
prevent code duplication and make sure you do not have to rewrite the "print()" statement multiple times
unsorted_array = [0,5,1,6] Print the reversed array
print(list(reversed(unsorted_array)))
max('run') What is printed?
r
l = [1, 2, 3, 4] Write a code to double the values of the array list l, with the variable "result"
result = map(lambda x: x*2, l) print(list(result))
name the common return mistakes
returning too early in a loop, and unnecessary "else"
reduce
runs a function of two arguments cumulatively to the items of iterable, from left to right, which reduces the iterable to a single value
unsorted_array = [0,5,1,6] Sort the array using the 'sorted' built-in function
sorted(unsorted_array)