Extra Exercises

  1. Create a tuple called olympics with four elements: “Beijing”, “London”, “Rio”, “Tokyo”.

1.1 Create a tuple called practice that has four elements: ‘y’, ‘h’, ‘z’, and ‘x’.

1.2 Create a tuple named tup1 that has three elements: ‘a’, ‘b’, and ‘c’.

  1. The list below, tuples_lst, is a list of tuples. Create a list of the second elements of each tuple and assign this list to the variable country.

2.1 Provided is a list of tuples. Create another list called t_check that contains the third element of every tuple.

2.2 Below, we have provided a list of tuples. Write a for loop that saves the second element of each tuple into a list called seconds.

  1. With only one line of code, assign the variables city, country, and year to the values of the tuple olymp.

3.1 With only one line of code, assign the variables water, fire, electric, and grass to the values “Squirtle”, “Charmander”, “Pikachu”, and “Bulbasaur”

3.2 With only one line of code, assign four variables, v1, v2, v3, and v4, to the following four values: 1, 2, 3, 4.

  1. Define a function called info with five parameters: name, gender, age, bday_month, and hometown. The function should then return a tuple with all five parameters in that order.

4.1 Define a function called information that takes as input, the variables name, birth_year, fav_color, and hometown. It should return a tuple of these variables in this order.

4.2 Define a function called info with the following required parameters: name, age, birth_year, year_in_college, and hometown. The function should return a tuple that contains all the inputted information.

  1. Given is the dictionary, gold, which shows the country and the number of gold medals they have earned so far in the 2016 Olympics. Create a list, num_medals, that contains only the number of medals for each country. Note: The .items() method provides a list of tuples. Do not use .keys() method.

5.1 If you remember, the .items() dictionary method produces a list of tuples. Keeping this in mind, we have provided you a dictionary called pokemon. For every key value pair, append the key to the list p_names, and append the value to the list p_number. Do not use the .keys() or .values() methods.

5.2 The .items() method produces a list of key-value pair tuples. With this in mind, write code to create a list of keys from the dictionary track_medal_counts and assign the list to the variable name track_events. Do NOT use the .keys() method.

Next Section - Introduction: Nested Data and Nested Iteration