Lists #
In this lab, we will review lists and introduce functional programming.
[1] What is a list, again? #
We call lists a data structure because they give structure to data. They put things in an ordered grouping, so you can access each element at an index.
Let’s use the Python shell to do some quick list experiments.
๐ป Open the Terminal and enter the Python shell by typing:
python3
๐ป Create a new list with 5 of your favorite foods.You should see the following output…
Python 3.9.7 (default, Oct 13 2021, 06:44:56) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
>>> food_list = ["dumplings","pizza","strawberries","ice cream","ramen","spinach"]
๐ป
Use the Python Shell to experiment with how to accomplish each task below.
โ๏ธ In your notebook, write down how you did it.
- Print out the first item in the list
- Print out the last item in the list
- Print out the middle 3 items in the list
- Add an item to the list
- Print out the length of the list
[2] Functional Programming #
In functional programming, we think of functions as transformations. They take any number of parameters and they change them into a return value.
def double(number):
return 2 * number
Here,
numberis the paramter and2 * numberis the return value.As its name suggests, this function returns twice,
number.
Here is a visualization of the function double. We are not so focused on how the function gets its
work done as we are on what goes in and what comes out:
This unlocks a powerful new way of breaking down problems: we can think about how functions could be connected together. How do you quadruple a number? Just double it, and then double the result:
quadruple_number = double(double(5))
quadruple_numberwill hold the integer value of20
We’ve already experienced this with strings, integers, operators, and lists. Let’s test this using the Python shell.
๐ป
Use the Python Shell to figure out what the return value is for each item below.
Simply copy and past each line one at a time, and print out the variable to find the return value.
number = str(10)letters = int('50')is_greater_than = 10>50number_of_fruits = len(['apples','oranges','lemons','watermelons'])
>>> number = str(10) >>> print(number)
โ๏ธ In your notebook, record the return value for each item.
[3] Setup #
๐ป
cd into your cs9/unit_01 folder.
cd Desktop/cs9
cd unit_01
๐ป Then, clone your starter code.
Be sure to change YOUR-GITHUB-USERNAME to your actual Github username.
git clone https://github.com/the-isf-academy/lab-lists-YOUR-GITHUB-USERNAME.git
[4] Transforming Lists #
Now, we will utilize a functional programming approach to transform lists.
You will write four functions:
square_lists()capitalize_list()pluralize_list()reverse_list()
Each function takes a list as a parameter, and returns and new list.
๐ป Code the function
square_list()
- Parameter: a list of numbers
- Return value: a new list with each element squared
๐ป Code the function
capitalize_list()
- Parameter: a list of strings
- Return Value:a new list with each string fully capitalized
๐ป Code the function
pluralize_list()
- Parameter: a list of strings
- Return Value: a new list word in its plural form
๐ป Code the function
reverse_list()
- Parameter: Takes a list of items (can be any data type)
- Return Value: a new list with items in reverse order
Here are a few helpful functions to transform the elements in the lists. Also be sure to reference the previous lab, for a reminder on looping through lists.
| Function | Data Type | Explanation | Example |
|---|---|---|---|
append(element) |
lists | adds an element to the end of a list | my_list.append("lemonade") |
upper() |
strings | capitalizes every letter in a string | my_string.upper() |
[Testing] #
๐ป Write tests to ensure each of your functions works as intended.
Use list_transformations_test.py to test your functions sufficiently.
โ CHECKPOINT:
Answer the following prompts in your notebook:
- What is a list and why is it useful?
- What are the benefits of using a functional programming approach to list transformations?
- How could you have utilized lists in your Unit 0 project?
[Deliverables] #
โกโจ
๐ป For this lab, you should
pushthe following files to Github.
lab-listsrepository containing the following:
list_transformations.pylist_transformations_test.pyAlso be sure to hand in your notebook with checkpoint questions.
[5] Extension: Sort #
Python has a built in function sort() that will organize the elements of a list. Python takes care of the logic behind the sorting for you. All you have to do is tell Python which list to sort.
๐ป
Test the sort() function using the Python shell.
>>> number_list = [390,2,49,5.3,-100] >>> number_list.sort()
number_listnow holds:[-100, 2, 5.3, 49, 390]
๐ป โ๏ธ The extention activity is is for you to think through and apply the logic of the sorting algothirm.
- In your notebook, write pseudocode for the
sort()function- Apply your pseudocode and write a custom
sort_list()function inlist_transformations.py
Some questions to consider:
- What are the steps involved in sorting a list of items?
- What if the list contains numbers? What if the list contains strings?
- What if the list contains numbers and strings?
๐ป
Remember to push your work to Github.