for i in range(1, 10 + 1):
print(i**2)1
4
9
16
25
36
49
64
81
100
For Loops and List Comprehension Solutions
Romi Hill
October 2, 2026
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 for loops and lists, because they are foundational for Python coding.
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
Squares: Print the squares of numbers from 1 to 10.
I’ll give you the solution for the first one.
range(20), print only the even numbers.["apple", "banana", "cherry"], print the first letter in each element "a", "b", "c"You can use the following variable.
range(50), print the numbers in that range which are divisible by 7, squared.["apple","banana","cherry"], print the word and its length, separated by a tab and colon. Hint: you will need to use the sep option your print statement. See Sophie’s slides from Monday September 28, on if/else statements.You can use the following variable.
"banana", create a dict mapping each letter → its frequency. Hint: You need to initialize the dictionary before starting the for loop.{'Alice': 25, 'Bob': 17, 'Carol': 30 }, print only the names where their age is greater than or equal to 21.str "comprehension", create a set x that contains the set of vowels that appear in the string. You may need to use the .add() method: https://www.w3schools.com/python/python_sets_add.aspYou can use the following variable.
"python" and "notebook", make a set of letters they share.{'a':1, 'b':2, 'c':3}, produce {1:'a', 2:'b', 3:'c'}.You can use the following variables.
{1: 'a', 2: 'b', 3: 'c'}
[[1,2],[3,4],[5,6]], print each element in the list [1,2,3,4,5,6]. You can assume that the list of lists is only one level deep.You can use the following variable.
In the last exercise, we were simply printing our output.
In this exercise, rewrite the tasks from above into list comprehension notation. These exercises also include the data type that is the expected output. You will be able to carry over the logic from the previous exercise to this one.
I’ll give you can example of the first exercise.
Squares: Create a list of the squares of numbers from 1 to 10.
range(20), build a list of only the even numbers.["apple", "banana", "cherry"], create a list of the first letter in each element ["a", "b", "c"].range(50), build a list of numbers divisible by 7, squared.["apple","banana","cherry"], create a dict mapping each word → its length."banana", create a dict mapping each letter → its frequency.{'Alice': 25, 'Bob': 17, 'Carol': 30 }, keep only entries where age ≥ 21.{'Alice': 25, 'Carol': 30}
str "comprehension", create a set of vowels that appear in it."python" and "notebook", make a set of letters they share.{'a':1, 'b':2, 'c':3}, produce {1:'a', 2:'b', 3:'c'}.[[1,2],[3,4],[5,6]], produce [1,2,3,4,5,6].