✍️ Lists
Level 1
Some Cool Ints
Define a list of 10 integers from 1 to 10 and display the size of the list.
Some Cool Names
Define a list of 5 first names and print the list.
Some Cool Grades
- Prompt the user how many grades they want to enter
- Prompt the user for the grades, one at a time
- Store all the grades in a single list
- print the list
Note: we cannot use “input” in the web browser so you must do this in Thonny
How do you prompt the user with python?
We can’t run code that uses input in the web browser, so run this in Thonny:
grades = []
num = int(input("How many grades do you want to enter? "))
for i in range(num):
grade = float(input(f"Enter grade for student {i + 1}: "))
grades.append(grade)
print(grades) Sumthings going on here
Using the list below, calculate the total sum of each number squared
total = nums₀² + nums₁² + nums₂² + nums₃² + nums₄² + nums₅²
Level 2
Studying is for Squares
You’re given a list of numbers:
nums = [1, 2, 3, 4, 5, 6, 7]Write a program that creates a new list where each number is replaced by its square, then prints the result:
[1, 4, 9, 16, 25, 36, 49]
Use a l🔁🔁p
Reverse a List
Reverse a list without using reverse(). You’re given the following cool list
cool_list = [100, 200, 300, 400, 500]Your goal is to print the reversed version:
[500, 400, 300, 200, 100]
There are like 5 valid ways to do it, all of which use a l🔄🔄p
Min / Max
Given the list:
listo = [8, 2, 15, 1, 9]Write a program that finds and prints the largest and smallest numbers without using min() or max(). The expected output is:
Largest number: 15
Smallest number: 1
How can you keep track of something over the course of a loop?
Average
Given a list of an unknown length:
numbers = [1,6,3,8,9,....]
Write a program that finds the average value without using sum(), len() nor other functions.
Can you visit all the items and find the sum and the length?
Level 3
Searching
Given the paired lists below:
destinations= ["Cancun", "New York", "Rio", "Madrid", "Paris", "Vancouver"]
tickets = [480.0, 834.0, 954.0, 690.0, 574.0, 322.0]Write a program which asks the user for a target destination. If it finds the destination in the list, it should print the ticket price, otherwise it should print “Destination not found”. The program cannot use the method.index()
Example output 1
Where would you like to travel? Cancun
Found it! The price to go to Cancun is 480.00$
Example output 2
Where would you like to travel? Cairo
Destination not found
You can return when a function is ready to exit
Numeric Calculus
You are given a list of x_values and y_values without knowing the explicit formula that relates them.
x_values = [0, 0.1, 0.2, 0.3, 0.4, 0.5]
y_values = [-0.6384318904120898, 0.4680346286487408, 0.4521364981725247, 0.8607335975351307, 0.9269427310806756, 1.0982126783257407]- Write a function called
get_positives()which takes as input a list ofxsand a list ofysand returns a list of x values where y is greater or equal to 0.
- Write a function called
local_minima()which takes as input a list ofxsand a list ofysand returns a list of values where y is a local minima (larger than the previous y and larger than the next y): \[ y_{i-1} > y_i < y_{i+1}\]
Use for i in range() to loop over the list and compare each point to the previous one and the next. Be careful of the index out of range!
- Write a function called
derive()which takes as input a list ofxsand a list ofysand finds an estimate of the derivative by calculating the slope \[\frac{\Delta{y}}{\Delta{x}}\] at every point where \(\Delta{x} = x_i - x_{i-1}\) and \(\Delta{y} = y_i - y_{i-1}\). It should return a list of slope values for each value of x.
Use for i in range() to loop over the list and compare each point to the previous one. Be careful of the index out of range!
- Write a function
is_increasing()which takes a list ofvaluesand returnsTrueif the values are always increasing, otherwise returnFalse.
Check if a value isn’t increasing compared to the previous one.
Level 4
Chaos Mode
You are giving the following chaotic list
chaos = [42, 67, 'beans', True, 'butter', 64, [], False]Create a new list that contains only the True / False and print it.
What must our list item be for it to be worthy of being in our new list?
Flat Stanley
You’re given a list of lists:
list_of_lists = [[1, 2], [3, 4], [5, 6, 7]]Write a program that flattens this nested list into a single list, then prints it:
[1, 2, 3, 4, 5, 6, 7]
l🔁🔁p && .append()