Created
April 30, 2021 13:44
-
-
Save IvanFrecia/f68bbee6db06d1e035b9434559d3ccee to your computer and use it in GitHub Desktop.
Coursera - Python 3 Programming Specialization - Functions Files and Dictionaries - Week3 - Assessment for Tuples lesson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Create a tuple called olympics with four elements: “Beijing”, “London”, “Rio”, “Tokyo”. | |
# Amswer: | |
olympics = "Beijing", "London", "Rio", "Tokyo" | |
# 2) 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. | |
# Amswer: | |
tuples_lst = [('Beijing', 'China', 2008), ('London', 'England', 2012), ('Rio', 'Brazil', 2016, 'Current'), ('Tokyo', 'Japan', 2020, 'Future')] | |
country = [] | |
for scnd_elem in tuples_lst: | |
country.append(scnd_elem[1]) | |
print(country) | |
# 3) With only one line of code, assign the variables city, country, and year to the values of the tuple olymp. | |
# Amswer: | |
olymp = ('Rio', 'Brazil', 2016) | |
city, country, year = olymp | |
# 4) 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. | |
# Amswer: | |
def info(name, gender, age, bday_month, hometown): | |
return name, gender, age, bday_month, hometown | |
print(info('myself', 'male', 33, 'July', 'Buenos Aires')) # print is not needed for excersise | |
# 5) 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. You must use the .items() method. Note: The .items() method provides a list of tuples. | |
# Do not use .keys() method. | |
# Amswer: | |
gold = {'USA':31, 'Great Britain':19, 'China':19, 'Germany':13, 'Russia':12, 'Japan':10, 'France':8, 'Italy':8} | |
num_medals = [] | |
for how_many in gold.items(): | |
num_medals.append(how_many[1]) | |
print(num_medals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment