3: The math module

Instructions

Use the sqrt() function within the math module to assign the square root of 16.0 to a.

Use the ceil() function within the math module to assign the ceiling of 111.3 to b.

Use the floor() function within the math module to assign the floor of 89.9 to c.


In [1]:
import math

Answer


In [2]:
a = math.sqrt(16.0)
b = math.ceil(111.3)
c = math.floor(89.9)

In [3]:
print(a, b, c)


4.0 112 89

4: Variables within modules

Instructions

Assign the square root of pi to a.

Assign the ceiling of pi to b.

Assign the floor of pi to c.


In [4]:
import math

print(math.pi)


3.141592653589793

Answer


In [5]:
PI = math.pi
a = math.sqrt(PI)
b = math.ceil(PI)
c = math.floor(PI)

In [6]:
print(a, b, c)


1.7724538509055159 4 3

5: The csv module

Instructions

Read in all of the data from "nfl.csv" into a list variable named nfl using the csv module.

Answer


In [9]:
import csv

f = open("nfl.csv")
csvreader = csv.reader(f)
nfl = list(csvreader)

In [10]:
print(nfl)


[['2009', '1', 'Pittsburgh Steelers', 'Tennessee Titans'], ['2009', '1', 'Minnesota Vikings', 'Cleveland Browns'], ['2009', '1', 'New York Giants', 'Washington Redskins'], ['2009', '1', 'San Francisco 49ers', 'Arizona Cardinals'], ['2009', '1', 'Seattle Seahawks', 'St. Louis Rams'], ['2009', '1', 'Philadelphia Eagles', 'Carolina Panthers'], ['2009', '1', 'New York Jets', 'Houston Texans'], ['2009', '1', 'Atlanta Falcons', 'Miami Dolphins'], ['2009', '1', 'Baltimore Ravens', 'Kansas City Chiefs'], ['2009', '1', 'Indianapolis Colts', 'Jacksonville Jaguars'], ['2009', '1', 'New Orleans Saints', 'Detroit Lions'], ['2009', '1', 'Denver Broncos', 'Cincinnati Bengals'], ['2009', '1', 'Green Bay Packers', 'Chicago Bears'], ['2009', '1', 'Dallas Cowboys', 'Tampa Bay Buccaneers'], ['2009', '1', 'New England Patriots', 'Buffalo Bills'], ['2009', '1', 'San Diego Chargers', 'Oakland Raiders'], ['2009', '2', 'New York Giants', 'Dallas Cowboys'], ['2009', '2', 'Chicago Bears', 'Pittsburgh Steelers'], ['2009', '2', 'Oakland Raiders', 'Kansas City Chiefs'], ['2009', '2', 'Washington Redskins', 'St. Louis Rams'], ['2009', '2', 'Baltimore Ravens', 'San Diego Chargers'], ['2009', '2', 'San Francisco 49ers', 'Seattle Seahawks'], ['2009', '2', 'Buffalo Bills', 'Tampa Bay Buccaneers'], ['2009', '2', 'New York Jets', 'New England Patriots'], ['2009', '2', 'New Orleans Saints', 'Philadelphia Eagles'], ['2009', '2', 'Atlanta Falcons', 'Carolina Panthers'], ['2009', '2', 'Cincinnati Bengals', 'Green Bay Packers'], ['2009', '2', 'Denver Broncos', 'Cleveland Browns'], ['2009', '2', 'Arizona Cardinals', 'Jacksonville Jaguars'], ['2009', '2', 'Houston Texans', 'Tennessee Titans'], ['2009', '2', 'Minnesota Vikings', 'Detroit Lions'], ['2009', '2', 'Indianapolis Colts', 'Miami Dolphins'], ['2009', '3', 'San Diego Chargers', 'Miami Dolphins'], ['2009', '3', 'New Orleans Saints', 'Buffalo Bills'], ['2009', '3', 'New York Giants', 'Tampa Bay Buccaneers'], ['2009', '3', 'New York Jets', 'Tennessee Titans'], ['2009', '3', 'Cincinnati Bengals', 'Pittsburgh Steelers'], ['2009', '3', 'Minnesota Vikings', 'San Francisco 49ers'], ['2009', '3', 'Detroit Lions', 'Washington Redskins'], ['2009', '3', 'Philadelphia Eagles', 'Kansas City Chiefs'], ['2009', '3', 'Jacksonville Jaguars', 'Houston Texans'], ['2009', '3', 'New England Patriots', 'Atlanta Falcons'], ['2009', '3', 'Chicago Bears', 'Seattle Seahawks'], ['2009', '3', 'Baltimore Ravens', 'Cleveland Browns'], ['2009', '3', 'Indianapolis Colts', 'Arizona Cardinals'], ['2009', '3', 'Denver Broncos', 'Oakland Raiders'], ['2009', '3', 'Green Bay Packers', 'St. Louis Rams'], ['2009', '3', 'Dallas Cowboys', 'Carolina Panthers'], ['2009', '4', 'Houston Texans', 'Oakland Raiders'], ['2009', '4', 'San Francisco 49ers', 'St. Louis Rams'], ['2009', '4', 'New England Patriots', 'Baltimore Ravens'], ['2009', '4', 'Pittsburgh Steelers', 'San Diego Chargers'], ['2009', '4', 'Washington Redskins', 'Tampa Bay Buccaneers'], ['2009', '4', 'Jacksonville Jaguars', 'Tennessee Titans'], ['2009', '4', 'New Orleans Saints', 'New York Jets'], ['2009', '4', 'New York Giants', 'Kansas City Chiefs'], ['2009', '4', 'Indianapolis Colts', 'Seattle Seahawks'], ['2009', '4', 'Chicago Bears', 'Detroit Lions'], ['2009', '4', 'Denver Broncos', 'Dallas Cowboys'], ['2009', '4', 'Cincinnati Bengals', 'Cleveland Browns'], ['2009', '4', 'Miami Dolphins', 'Buffalo Bills'], ['2009', '4', 'Minnesota Vikings', 'Green Bay Packers'], ['2009', '5', 'Arizona Cardinals', 'Houston Texans'], ['2009', '5', 'Philadelphia Eagles', 'Tampa Bay Buccaneers'], ['2009', '5', 'Carolina Panthers', 'Washington Redskins'], ['2009', '5', 'Atlanta Falcons', 'San Francisco 49ers'], ['2009', '5', 'New York Giants', 'Oakland Raiders'], ['2009', '5', 'Pittsburgh Steelers', 'Detroit Lions'], ['2009', '5', 'Denver Broncos', 'New England Patriots'], ['2009', '5', 'Minnesota Vikings', 'St. Louis Rams'], ['2009', '5', 'Seattle Seahawks', 'Jacksonville Jaguars'], ['2009', '5', 'Dallas Cowboys', 'Kansas City Chiefs'], ['2009', '5', 'Indianapolis Colts', 'Tennessee Titans'], ['2009', '5', 'Cleveland Browns', 'Buffalo Bills'], ['2009', '5', 'Cincinnati Bengals', 'Baltimore Ravens'], ['2009', '5', 'Miami Dolphins', 'New York Jets'], ['2009', '6', 'Oakland Raiders', 'Philadelphia Eagles'], ['2009', '6', 'Arizona Cardinals', 'Seattle Seahawks'], ['2009', '6', 'Atlanta Falcons', 'Chicago Bears'], ['2009', '6', 'Carolina Panthers', 'Tampa Bay Buccaneers'], ['2009', '6', 'New Orleans Saints', 'New York Giants'], ['2009', '6', 'Jacksonville Jaguars', 'St. Louis Rams'], ['2009', '6', 'Minnesota Vikings', 'Baltimore Ravens'], ['2009', '6', 'New England Patriots', 'Tennessee Titans'], ['2009', '6', 'Kansas City Chiefs', 'Washington Redskins'], ['2009', '6', 'Houston Texans', 'Cincinnati Bengals'], ['2009', '6', 'Green Bay Packers', 'Detroit Lions'], ['2009', '6', 'Pittsburgh Steelers', 'Cleveland Browns'], ['2009', '6', 'Buffalo Bills', 'New York Jets'], ['2009', '6', 'Denver Broncos', 'San Diego Chargers'], ['2009', '7', 'Arizona Cardinals', 'New York Giants'], ['2009', '7', 'Indianapolis Colts', 'St. Louis Rams'], ['2009', '7', 'Cincinnati Bengals', 'Chicago Bears'], ['2009', '7', 'New Orleans Saints', 'Miami Dolphins'], ['2009', '7', 'Dallas Cowboys', 'Atlanta Falcons'], ['2009', '7', 'San Diego Chargers', 'Kansas City Chiefs'], ['2009', '7', 'New York Jets', 'Oakland Raiders'], ['2009', '7', 'Houston Texans', 'San Francisco 49ers'], ['2009', '7', 'New England Patriots', 'Tampa Bay Buccaneers'], ['2009', '7', 'Pittsburgh Steelers', 'Minnesota Vikings'], ['2009', '7', 'Green Bay Packers', 'Cleveland Browns'], ['2009', '7', 'Buffalo Bills', 'Carolina Panthers'], ['2009', '7', 'Philadelphia Eagles', 'Washington Redskins'], ['2009', '8', 'Minnesota Vikings', 'Green Bay Packers'], ['2009', '8', 'Miami Dolphins', 'New York Jets'], ['2009', '8', 'Tennessee Titans', 'Jacksonville Jaguars'], ['2009', '8', 'Dallas Cowboys', 'Seattle Seahawks'], ['2009', '8', 'Baltimore Ravens', 'Denver Broncos'], ['2009', '8', 'Chicago Bears', 'Cleveland Browns'], ['2009', '8', 'Carolina Panthers', 'Arizona Cardinals'], ['2009', '8', 'Indianapolis Colts', 'San Francisco 49ers'], ['2009', '8', 'St. Louis Rams', 'Detroit Lions'], ['2009', '8', 'San Diego Chargers', 'Oakland Raiders'], ['2009', '8', 'Houston Texans', 'Buffalo Bills'], ['2009', '8', 'Philadelphia Eagles', 'New York Giants'], ['2009', '8', 'New Orleans Saints', 'Atlanta Falcons'], ['2009', '9', 'Atlanta Falcons', 'Washington Redskins'], ['2009', '9', 'Cincinnati Bengals', 'Baltimore Ravens'], ['2009', '9', 'Tennessee Titans', 'San Francisco 49ers'], ['2009', '9', 'New England Patriots', 'Miami Dolphins'], ['2009', '9', 'Jacksonville Jaguars', 'Kansas City Chiefs'], ['2009', '9', 'Indianapolis Colts', 'Houston Texans'], ['2009', '9', 'Tampa Bay Buccaneers', 'Green Bay Packers'], ['2009', '9', 'Seattle Seahawks', 'Detroit Lions'], ['2009', '9', 'Dallas Cowboys', 'Philadelphia Eagles'], ['2009', '9', 'New Orleans Saints', 'Carolina Panthers'], ['2009', '9', 'Arizona Cardinals', 'Chicago Bears'], ['2009', '9', 'San Diego Chargers', 'New York Giants'], ['2009', '9', 'Pittsburgh Steelers', 'Denver Broncos'], ['2009', '10', 'San Francisco 49ers', 'Chicago Bears'], ['2009', '10', 'New Orleans Saints', 'St. Louis Rams'], ['2009', '10', 'Indianapolis Colts', 'New England Patriots'], ['2009', '10', 'San Diego Chargers', 'Philadelphia Eagles'], ['2009', '10', 'Arizona Cardinals', 'Seattle Seahawks'], ['2009', '10', 'Miami Dolphins', 'Tampa Bay Buccaneers'], ['2009', '10', 'Kansas City Chiefs', 'Oakland Raiders'], ['2009', '10', 'Jacksonville Jaguars', 'New York Jets'], ['2009', '10', 'Minnesota Vikings', 'Detroit Lions'], ['2009', '10', 'Carolina Panthers', 'Atlanta Falcons'], ['2009', '10', 'Tennessee Titans', 'Buffalo Bills'], ['2009', '10', 'Cincinnati Bengals', 'Pittsburgh Steelers'], ['2009', '10', 'Green Bay Packers', 'Dallas Cowboys'], ['2009', '10', 'Washington Redskins', 'Denver Broncos'], ['2009', '10', 'Baltimore Ravens', 'Cleveland Browns'], ['2009', '11', 'Miami Dolphins', 'Carolina Panthers'], ['2009', '11', 'Philadelphia Eagles', 'Chicago Bears'], ['2009', '11', 'Kansas City Chiefs', 'Pittsburgh Steelers'], ['2009', '11', 'San Diego Chargers', 'Denver Broncos'], ['2009', '11', 'Minnesota Vikings', 'Seattle Seahawks'], ['2009', '11', 'Green Bay Packers', 'San Francisco 49ers'], ['2009', '11', 'Dallas Cowboys', 'Washington Redskins'], ['2009', '11', 'New England Patriots', 'New York Jets'], ['2009', '11', 'New Orleans Saints', 'Tampa Bay Buccaneers'], ['2009', '11', 'New York Giants', 'Atlanta Falcons'], ['2009', '11', 'Jacksonville Jaguars', 'Buffalo Bills'], ['2009', '11', 'Oakland Raiders', 'Cincinnati Bengals'], ['2009', '11', 'Detroit Lions', 'Cleveland Browns'], ['2009', '11', 'Indianapolis Colts', 'Baltimore Ravens'], ['2009', '11', 'Arizona Cardinals', 'St. Louis Rams'], ['2009', '11', 'Tennessee Titans', 'Houston Texans'], ['2009', '12', 'Denver Broncos', 'New York Giants'], ['2009', '12', 'Green Bay Packers', 'Detroit Lions'], ['2009', '12', 'Dallas Cowboys', 'Oakland Raiders'], ['2009', '12', 'Baltimore Ravens', 'Pittsburgh Steelers'], ['2009', '12', 'Seattle Seahawks', 'St. Louis Rams'], ['2009', '12', 'Atlanta Falcons', 'Tampa Bay Buccaneers'], ['2009', '12', 'Philadelphia Eagles', 'Washington Redskins'], ['2009', '12', 'Buffalo Bills', 'Miami Dolphins'], ['2009', '12', 'San Diego Chargers', 'Kansas City Chiefs'], ['2009', '12', 'San Francisco 49ers', 'Jacksonville Jaguars'], ['2009', '12', 'Tennessee Titans', 'Arizona Cardinals'], ['2009', '12', 'Indianapolis Colts', 'Houston Texans'], ['2009', '12', 'Cincinnati Bengals', 'Cleveland Browns'], ['2009', '12', 'Minnesota Vikings', 'Chicago Bears'], ['2009', '12', 'New York Jets', 'Carolina Panthers'], ['2009', '12', 'New Orleans Saints', 'New England Patriots'], ['2009', '13', 'New York Jets', 'Buffalo Bills'], ['2009', '13', 'Seattle Seahawks', 'San Francisco 49ers'], ['2009', '13', 'San Diego Chargers', 'Cleveland Browns'], ['2009', '13', 'Chicago Bears', 'St. Louis Rams'], ['2009', '13', 'Oakland Raiders', 'Pittsburgh Steelers'], ['2009', '13', 'Philadelphia Eagles', 'Atlanta Falcons'], ['2009', '13', 'Indianapolis Colts', 'Tennessee Titans'], ['2009', '13', 'Carolina Panthers', 'Tampa Bay Buccaneers'], ['2009', '13', 'Miami Dolphins', 'New England Patriots'], ['2009', '13', 'New York Giants', 'Dallas Cowboys'], ['2009', '13', 'Denver Broncos', 'Kansas City Chiefs'], ['2009', '13', 'Cincinnati Bengals', 'Detroit Lions'], ['2009', '13', 'Jacksonville Jaguars', 'Houston Texans'], ['2009', '13', 'Arizona Cardinals', 'Minnesota Vikings'], ['2009', '13', 'New Orleans Saints', 'Washington Redskins'], ['2009', '13', 'Green Bay Packers', 'Baltimore Ravens'], ['2009', '14', 'Cleveland Browns', 'Pittsburgh Steelers'], ['2009', '14', 'New York Jets', 'Tampa Bay Buccaneers'], ['2009', '14', 'Philadelphia Eagles', 'New York Giants'], ['2009', '14', 'Tennessee Titans', 'St. Louis Rams'], ['2009', '14', 'San Diego Chargers', 'Dallas Cowboys'], ['2009', '14', 'Houston Texans', 'Seattle Seahawks'], ['2009', '14', 'Washington Redskins', 'Oakland Raiders'], ['2009', '14', 'New Orleans Saints', 'Atlanta Falcons'], ['2009', '14', 'Miami Dolphins', 'Jacksonville Jaguars'], ['2009', '14', 'Buffalo Bills', 'Kansas City Chiefs'], ['2009', '14', 'New England Patriots', 'Carolina Panthers'], ['2009', '14', 'Minnesota Vikings', 'Cincinnati Bengals'], ['2009', '14', 'Indianapolis Colts', 'Denver Broncos'], ['2009', '14', 'Baltimore Ravens', 'Detroit Lions'], ['2009', '14', 'Green Bay Packers', 'Chicago Bears'], ['2009', '14', 'San Francisco 49ers', 'Arizona Cardinals'], ['2009', '15', 'Indianapolis Colts', 'Jacksonville Jaguars'], ['2009', '15', 'Dallas Cowboys', 'New Orleans Saints'], ['2009', '15', 'Carolina Panthers', 'Minnesota Vikings'], ['2009', '15', 'New England Patriots', 'Buffalo Bills'], ['2009', '15', 'Oakland Raiders', 'Denver Broncos'], ['2009', '15', 'Philadelphia Eagles', 'San Francisco 49ers'], ['2009', '15', 'Tampa Bay Buccaneers', 'Seattle Seahawks'], ['2009', '15', 'Tennessee Titans', 'Miami Dolphins'], ['2009', '15', 'Houston Texans', 'St. Louis Rams'], ['2009', '15', 'Pittsburgh Steelers', 'Green Bay Packers'], ['2009', '15', 'Atlanta Falcons', 'New York Jets'], ['2009', '15', 'Baltimore Ravens', 'Chicago Bears'], ['2009', '15', 'San Diego Chargers', 'Cincinnati Bengals'], ['2009', '15', 'Cleveland Browns', 'Kansas City Chiefs'], ['2009', '15', 'Arizona Cardinals', 'Detroit Lions'], ['2009', '15', 'New York Giants', 'Washington Redskins'], ['2009', '16', 'San Diego Chargers', 'Tennessee Titans'], ['2009', '16', 'Cleveland Browns', 'Oakland Raiders'], ['2009', '16', 'Arizona Cardinals', 'St. Louis Rams'], ['2009', '16', 'Pittsburgh Steelers', 'Baltimore Ravens'], ['2009', '16', 'Green Bay Packers', 'Seattle Seahawks'], ['2009', '16', 'Tampa Bay Buccaneers', 'New Orleans Saints'], ['2009', '16', 'New York Jets', 'Indianapolis Colts'], ['2009', '16', 'Cincinnati Bengals', 'Kansas City Chiefs'], ['2009', '16', 'Dallas Cowboys', 'Washington Redskins'], ['2009', '16', 'New England Patriots', 'Jacksonville Jaguars'], ['2009', '16', 'Houston Texans', 'Miami Dolphins'], ['2009', '16', 'San Francisco 49ers', 'Detroit Lions']]

6: Counting how many times a team won

Instructions

Fill in the mission code to do the following:

Import and use the csv module to load data from our "nfl.csv" file.

Count how many games the "New England Patriots" won from 2009-2013. To do this, set a counter to 0, and increment by 1 whenever you see a row whose winner column is equal to "New England Patriots".

Assign the count to patriots_wins.

Answer


In [12]:
import csv

f = open("nfl.csv")
reader = csv.reader(f)
data = list(reader)
patriots_wins = 0

for row in data:
    if row[2] == "New England Patriots":
        patriots_wins += 1
print(patriots_wins)


10

7: Making a function to count wins

Instructions

Write a function called nfl_wins that will take a team name as input.

The function should return the number of wins the team had from 2009-2013.

Use the function to assign the number of wins by the "Dallas Cowboys" to cowboys_wins.

Use the function to assign the number of wins by the "Atlanta Falcons" to falcons_wins.


In [13]:
import csv

f = open("nfl.csv", 'r')
nfl = list(csv.reader(f))

# Define your function here
def nfl_wins(team):
    team_wins = 0

    for row in nfl:
        if row[2] == team:
            team_wins += 1
            
    return team_wins

cowboys_wins = nfl_wins("Dallas Cowboys")
falcons_wins = nfl_wins("Atlanta Falcons")

print(cowboys_wins, falcons_wins)


10 7

10: Counting wins in a given year

Instructions

Modify the nfl_wins function so that it takes a team name, in the form of a string, and a year, also in the form of a string, as input. Call this new function nfl_wins_in_a_year

Your function should output the number of wins the team had in the given year, as an integer. Use the and operator to combine booleans, checking whether the desired team won and whether the game happened in the correct year for each row in the dataset.

Use your function to assign the number of wins by the "Cleveland Browns" in "2010" to browns_2010_wins.

Use your function to assign the number of wins by the "Philadelphia Eagles" in "2011" to eagles_2011_wins.


In [14]:
import csv

f = open("nfl.csv", 'r')
nfl = list(csv.reader(f))

def nfl_wins(team):
    count = 0
    for row in nfl:
        if row[2] == team:
            count = count + 1
    return count

In [15]:
def nfl_wins_in_a_year(team, year):
    count = 0
    for row in nfl:
        if row[0] == year and row[2] == team:
            count = count + 1
    return count

browns_2010_wins = nfl_wins_in_a_year("Cleveland Browns", "2010")
eagles_2011_wins = nfl_wins_in_a_year("Philadelphia Eagles", "2011")

In [16]:
print(browns_2010_wins, eagles_2011_wins)


0 0

In [ ]: