Assignment: Basic Python#

Learning Goals

This assignment will verify that you have the following skills:

  • Create lists and dictionaries

  • Iterate through lists, tuples, and dictionaries

  • Index sequences (e.g. lists and tuples)

  • Define functions

  • Use optional keyword arguments in functions

Part I: Lists and Loops#

In this problem, we will explore the basic data structures and flow controls of Python by manually creating data structures.

1) Create a list with the names of the top 10 CO₂ emitting countries (in order by total emissions)#

use 2024 data from here: https://edgar.jrc.ec.europa.eu/report_2025?vis=co2tot#emissions_table

Note 1: Don’t include things that aren’t countries

Note 2: You can simply manually read off the values from the table and enter them into your list. You do not need to do any automated data loading e.g. with pooch or things from the last assignment.

2) Have Python tell you how many countries there are by examining your list#

3) Use slicing to display the first three countries (the top emitters)#

4) Iterate through your countries and print the country name only if it has an a in the name#

Part II: Dictionaries#

1) Now create a dictionary that maps each country name to its annual CO₂ emissions#

Use the data source from Part 1

2) Use your dictionary to look up the United States’ emissions#

3) Loop through the data and create a list of countries whose emissions are greater than 1,000 million metric tons per year#

Display your result

4) Now add Brazil to your dictionary#

Part III: Functions#

1. Write a function to convert temperature from kelvin to celsius#

and celsius to kelvin

2. Write a function to convert temperature to fahrenheit#

Include an optional keyword argument to specify whether the input is in celcius or kelvin. Call your previously defined functions if necessary.

3) Check that the outputs are sensible#

by trying a few examples

4) Now write a function that converts from farenheit#

and uses a keyword argument to specify whether you want the output in celcius or kelvin

5) Write a function that takes two arguments (feet and inches) and returns height in meters#

Verify it gives sensible answers

6. Write a function takes one argument (height in meters) and returns two arguments (feet and inches)#

(Consult the tutorial on numbers if you are stuck on how to implement this.)

7) Verify that the “round trip” conversion from and back to meters is consistent#

Check for 3 different values of height in meters