Python For loops Lab

Learning Objectives

  • Understand how for loops can help us reduce repetition
  • Understand the syntax of for loops

Picking up where we last left off

In the last lesson, we worked with some of our travel data. Let's retrieve a list with our travel information again from excel. First, we read the information from excel as a list of dictionaries, with each dictionary representing a location. And we assign this list to the variable cities.

import pandas
file_name = './cities.xlsx'
travel_df = pandas.read_excel(file_name)
cities = travel_df.to_dict('records')

Next, we retrieve the first three city names, stored as the 'City' attribute of each dictionary, and 'Population' of each of the cities. Then we plot the names as our x_values and the populations as our y_values.

import plotly

plotly.offline.init_notebook_mode(connected=True)

x_values = [cities[0]['City'], cities[1]['City'], cities[2]['City']]
y_values = [cities[0]['Population'], cities[1]['Population'], cities[2]['Population']]
trace_first_three_pops = {'x': x_values, 'y': y_values, 'type': 'bar'}
plotly.offline.iplot([trace_first_three_pops])

Of course, as you may have spotted, there is a good amount of repetition in displaying this data. Just take a look at how we retrieved the data for our x_values and y_values.

x_values = [cities[0]['City'], cities[1]['City'], cities[2]['City']]
y_values = [cities[0]['Population'], cities[1]['Population'], cities[2]['Population']]

So in this lesson, we will use our for loop to display information about our travel locations with less repetition.

Working with the For Loop

Our cities list contains information about the top 12 cities by population. For our upcoming iteration tasks, it will be useful to have a list of the numbers 0 through 11. Use what we know about len and rangeto generate a list of numbers 0 through 11. Assign this to a variable called city_indices.

city_indices = None
city_indices # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Now we want to create labels for each of the cities. We'll provide a list of the city_names for you.

city_names = ['Buenos Aires',
 'Toronto',
 'Marakesh',
 'Albuquerque',
 'Los Cabos',
 'Greenville',
 'Archipelago Sea',
 'Pyeongchang',
 'Walla Walla Valley',
 'Salina Island',
 'Solta',
 'Iguazu Falls']

Your task is to assign the variable names_and_ranks to a list, with each element equal to the city name and it's corresponding rank. For example, the first element would be, "1. Buenos Aires" and the second would be "2. Toronto". Use a for loop and the lists city_indices and city_names to accomplish this. We'll need to perform some nifty string interpolation to format our strings properly. Check out f-string interpolation to see how we can pass values into a string. Remember that list indices start at zero, but we want our names_and_ranks list to start at one!

names_and_ranks = [] 
# write a for loop that adds the properly formatted string to the names_and_ranks list
names_and_ranks[0] # '1. Buenos Aires'
names_and_ranks[1] # '2. Toronto'
names_and_ranks[-1] # '12. Iguazu Falls'

Ok, now let's create a new variable called city_populations. Use a for loop to iterate through cities and have city_populations equal to each of the populations.

city_populations = []
city_populations[0] # 2891
city_populations[1] # 2732
city_populations[-1] # 0

Great! Now we can begin to plot this data. First, let's create a trace of our populations and set it to the variable trace_populations.

trace_populations = {'x': names_and_ranks, 
                     'y': city_populations, 
                     'text': names_and_ranks, 
                     'type': 'bar', 
                     'name': 'populations'}
import plotly
plotly.offline.init_notebook_mode(connected=True)
plotly.offline.iplot([trace_populations])

Now we want declare a variable called city_areas that points to a list of all of the areas of the cities. Let's use a for loop to iterate through our cities and have city_areas equal to each area of the city.

city_areas = []
trace_areas = {'x': names_and_ranks, 'y': city_areas, 'text': names_and_ranks, 'type': 'bar', 'name': 'areas'}
import plotly
plotly.offline.init_notebook_mode(connected=True)
plotly.offline.iplot([trace_populations, trace_areas])

Summary

In this section we saw how we can use for loops to go through elements of a list and perform the same operation on each. By using for loops we were able to reduce the amount of code that we wrote and while also writing more expressive code.