Lists, Modules, and Functions Exercises
This week we are just going to work on exercises to help reinforce what we've learn about modules, list comprehensions, and functions in Python.
Here's a starting exercise we'll go over together as a warm-up:
Warm-up Exercise: Write a function stringify
that takes a list l
and returns a list that contains the string form of each element in l
.
# solution 1 (C-like, not-preferred but works)
def stringify(l):
new_l = []
for i in range(len(l)):
new_l.append(str(l[i]))
return new_l
# solution 2 (Python-like, better)
def stringify(l):
new_l = []
for elem in l:
new_l.append(str(elem))
return new_l
# solution 3 (Python-like with list comprehensions, best)
def stringify(l):
new_l = [str(elem) for elem in l]
return new_l
stringify([1, 2, 3, 4])
Exercise 1: Write a function sum_list
that takes in a list l
of numbers and returns its sum. (There is a sum
command built-in to Python that does exactly this, but please don't use it for this exercise.)
def sum_list(l):
total = 0
for num in l:
total += num
return total
sum_list([1, 2, 3, 4, 5])
Exercise 2: Write a function sum_even
that takes an (even) integer n
and returns the sum of every even integer between 1 and n
. (Hint: use sum
or sum_list
above and a list comprehension.)
def sum_even(n):
return sum_list(2*num for num in range(n+1))
sum_even(4)
Exercise 3: Write a function find_even
that takes in a list l
of integers and returns a new list that contains only the even values of this list (in the same order).
def find_even(l):
return [num for num in l if num % 2 == 0]
find_even([1, 2, 3, 4, 5, 6, 7, 8])
Exercise 4: Write a function number_to_list
that takes an integer n
and returns a new list that contains all the (base-10) digits of n
in order.
# solution 1
def number_to_list(n):
l = []
while n > 10:
l.insert(0,n % 10)
n = n // 10
return l
# solution 2
def number_to_list(n):
return [int(d) for d in str(n)]
number_to_list(1234)
Exercise 5: Write a function is_subset
that takes a list l1
and a list l2
and returns True
if every element of l1
is a subset of l2
. Don't worry about ordering or duplicate elements.
def is_subset(l1, l2):
for elem in l1:
if elem not in l2:
return False
return True
is_subset([1, 2, 3], [1, 2, 3, 4])
Exercise 6: Write a function random_element
that takes a list l
and returns a random element of the list. Return each item with equal probability.
import random
# solution 1
def random_element(l):
return l[random.randint(0, len(l))]
# solution 2 (if you happened to already know about random.choice or you looked it up)
def random_element(l):
return random.choice(l)
random_element([1, 2, 3, 4, 5])
Exericse 7: Write a function is_Tuesday
that takes no arguments and prints "Happy Tuesday!" if the current day is Tuesday and "I wish it were Tuesday..." otherwise.
(Hint 1: You will need to use a module we haven't covered yet. See if you can figure it out by searching it yourself!)
(Hint 2 below this)
.
.
.
.
.
.
.
.
.
.
.
(Hint 2: Try searching "Python day of week")
import datetime
def is_Tuesday():
if datetime.datetime.today().weekday() == 1:
print("Happy Tuesday!")
else:
print("I wish it were Tuesday...")
is_Tuesday()