[i**2 for i in range(1, 10 + 1)][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
List Comprehension & Functions
Romi Hill
September 23, 2026
Any questions/issues/concerns?
I highly recommend always putting all files related to this course into one folder, and always open that folder in VSCode.
What is a folder? What is a file?
Markdown is a “mark up” language, which is a light weight language you can use to format documents. This is how “text” is displayed in .ipynb notebooks. You can formatting styles like headings, italics, bold, code snippets, and more. See https://www.markdownguide.org/cheat-sheet/ for more information and to make your notebooks look nicer :-)
In a Jupyter Notebook, make python cells that compute the following tasks. These exercises may feel tedious, but it is important to get a grasp of how to use lists, because they are foundational data type in Python.
Some relevant operations you may find useful are:
**: exponentiation, e.g. 2**3 is 8
range(n): generates a list of numbers from 0 to n-1
%: modulus operator, e.g. 5 % 2 is 1, because 5 divided by 2 is 2 with a remainder of 1
len(): returns the length of a list, string, etc
I’ll give you can example of the first exercise.
Squares: Create a list of the squares of numbers from 1 to 10.
Even numbers only: From range(20), build a list of only the even numbers.
First letters of words: Given ["apple", "banana", "cherry"], produce ["a", "b", "c"].
Filter & transform: From range(50), build a list of numbers divisible by 7, squared.
Word lengths: From ["apple","banana","cherry"], create a dict mapping each word → its length.
Frequency count: From the string "banana", create a dict mapping each letter → its frequency.
Filter dictionary: From {'Alice': 25, 'Bob': 17, 'Carol': 30 }, keep only entries where age ≥ 21.
Unique squares: From range(10), generate the set of squares.
Vowels in a word: Given the str "comprehension", create a set of vowels that appear in it.
Letters in two words: Given "python" and "notebook", make a set of letters they share.
Swap keys and values: Given {'a':1, 'b':2, 'c':3}, produce {1:'a', 2:'b', 3:'c'}.
Extra Challenge Exercise:
Common factors: Build a set of all numbers ≤ 50 divisible by both 2 and 3.
Flatten a list of lists: Given [[1,2],[3,4],[5,6]], produce [1,2,3,4,5,6].
I’ll give you the start. Hint: you need to use an operator.
Additional Exercise:
Modify the code so that the default value is “Sophie”. That is, your function should have the following output given these inputs. This should only take 9ish characters to type if your last code was correct!
True when x and y are anagrams of each other, and false otherwise. Example input and expected output is provided below.Note: You may have noticed : str and -> bool in this sample code. It’s good style to yourself/anyone else using the code that the input parameters should be a str, and the output should be a bool. However, this is not necessary. In fact, Python will still run if the output is not a bool, for example.
Challenge question:
Modify your code so that it also ignores spaces and capitalization. For example:
True when x is a palindrone. Example input and expected output is provided below.