Lab 03

List Comprehension & Functions

Author

Romi Hill

Published

September 23, 2026

1 Homework 1 Questions

Any questions/issues/concerns?

2 Folder Structure

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?

3 Markdown Language

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 :-)

4 Python Exercises

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.

[i**2 for i in range(1, 10 + 1)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  1. Even numbers only: From range(20), build a list of only the even numbers.

  2. First letters of words: Given ["apple", "banana", "cherry"], produce ["a", "b", "c"].

  3. Filter & transform: From range(50), build a list of numbers divisible by 7, squared.

  4. Word lengths: From ["apple","banana","cherry"], create a dict mapping each word → its length.

  5. Frequency count: From the string "banana", create a dict mapping each letter → its frequency.

  6. Filter dictionary: From {'Alice': 25, 'Bob': 17, 'Carol': 30 }, keep only entries where age ≥ 21.

  7. Unique squares: From range(10), generate the set of squares.

  8. Vowels in a word: Given the str "comprehension", create a set of vowels that appear in it.

  9. Letters in two words: Given "python" and "notebook", make a set of letters they share.

  10. Swap keys and values: Given {'a':1, 'b':2, 'c':3}, produce {1:'a', 2:'b', 3:'c'}.

Extra Challenge Exercise:

  1. Common factors: Build a set of all numbers ≤ 50 divisible by both 2 and 3.

  2. Flatten a list of lists: Given [[1,2],[3,4],[5,6]], produce [1,2,3,4,5,6].

5 Exercises from Last Time

5.1 Functions

  1. Write a function that outputs the following
print_hello('Romi')
Hello Romi!

I’ll give you the start. Hint: you need to use an operator.

def print_hello(name):
    pass # replace pass with your statement

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!

print_hello()
Hello Sophie!
print_hello("class of LX394")
Hello class of LX394!

5.2 Putting it together: Functions, Palindrones, and Anagrams

  1. Based on the code provided below, write a function that returns 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.

def is_anagram(first_word: str, second_word: str) -> bool:
    """
    Check whether two strings are palidrones of each other. 
    An anagram is formed by rearranging the letters of one word to produce another.
    """
    pass
is_anagram('listen', 'silent')
True
is_anagram('listen', 'listened')
False

Challenge question:

Modify your code so that it also ignores spaces and capitalization. For example:

is_anagram('Listen', 'siLeNt')
True
is_anagram('Tom Marvolo Riddle', 'I am Lord Voldemort')
True
  1. Based on the code provided below, write a function that returns True when x is a palindrone. Example input and expected output is provided below.
def is_palindrone(word: str) -> bool:
    """
    Check whether a word is a palindrone
    A palindrone is a word that is spelt the same backwards and forward 
    """
    pass
def is_palindrone(word: str) -> bool:
    return word == word[::-1]
is_palindrone('racecar')
is_palindrone('hello')