Functions can also take lists as inputs and perform various operations on those lists.
def count_small(numbers): total = 0 for n in numbers: if n < 10: total = total + 1 return total lotto = [4, 8, 15, 16, 23, 42] small = count_small(lotto) print small
count_smallthat has one parameter,
totalthat we can use in the
nis less than 10, we increment
forloop, we return
count_smallfunction, pass in
lotto, and store the returned result in
8are less than 10.
Write a function that counts how many times the string
"fizz" appears in a list.
fizz_countthat takes a list
countto hold the ongoing count. Initialize it to zero.
item in x:,
ifthat item is equal to the string
"fizz"then increment the
fizz_count(["fizz","cat","fizz"]) should return