Loops (Good) Exercises

Exercise 1 (Make me an odd list)

With your knowledge of growing lists, use a for loop with range() and continue to generate a list of the squares of the odd integers from 0 to 9.

Hint: You can check for ‘evenness’ using number % 2 == 0.

Exercise 2 (Make me another odd list)

Redo the previous exercise using list comprehension.

Exercise 3 (Time me!)

Use the cell magic command %%timeit to time the previous solutions. Which of the two is faster?

Exercise 4 (A problem of decay)

The initial quantity of a sample of a radioactive substance is 100 units, and it decays by 5% each year. Use a while loop to determine how long the sample will take to reduce to half its original amount.

Exercise 5 (Changes in CO\(_2\))

The following is data about atmospheric CO\(_2\) levels for several years in a (year, CO2_level) format. The units are ppm.

co2_data = [
    (2000, 369.55), (2001, 371.14), (2002, 373.28), 
    (2003, 375.80), (2004, 377.52), (2005, 379.80), 
    (2006, 381.90), (2007, 383.79), (2008, 385.60), 
    (2009, 387.43), (2010, 389.90), (2011, 391.65), 
    (2012, 393.85), (2013, 396.52), (2014, 398.65),
    (2015, 400.83), (2016, 404.24), (2017, 406.55), 
    (2018, 408.52), (2019, 411.44), (2020, 414.24)
]

Identify those years that showed an increase of CO\(_2\) of 3 ppm or more compared to the previous year.
Please print out these years along with the corresponding change in concentration.

Back to top