GithubHelp home page GithubHelp logo

python-for-loops-lab-lon01-dtsc-ft-091420's Introduction

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.

python-for-loops-lab-lon01-dtsc-ft-091420's People

Contributors

jeffkatzy avatar tkoar avatar cutterbuck avatar loredirick avatar yoyothesheep avatar

Watchers

James Cloos avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Michelle Rios avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.