2: Find the different genders

Instructions

Loop through the rows in legislators, and extract the gender column (fourth column)

Append the genders to genders_list.

Then turn genders_list into a set, and assign it to unique_genders

Finally, convert unique_genders back into a list, and assign it to unique_genders_list.


In [4]:
# We can use the set() function to convert lists into sets.
# A set is a data type, just like a list, but it only contains each value once.
car_makers = ["Ford", "Volvo", "Audi", "Ford", "Volvo"]

# Volvo and ford are duplicates
print(car_makers)

# Converting to a set
unique_car_makers = set(car_makers)
print(unique_car_makers)

# We can't index sets, so we need to convert back into a list first.
unique_cars_list = list(unique_car_makers)
print(unique_cars_list[0])

genders_list = []
unique_genders = set()
unique_genders_list = []


['Ford', 'Volvo', 'Audi', 'Ford', 'Volvo']
{'Ford', 'Audi', 'Volvo'}
Ford

In [5]:
from legislators import legislators

Answer


In [6]:
genders_list = []

for leg in legislators:
    genders_list.append(leg[3])
unique_genders = set(genders_list)
unique_gender_list = list(unique_genders)

In [8]:
print(unique_gender_list)


['', 'M']

3: Replacing genders

Instructions

Loop through the rows in legislators and replace any gender values of "" with "M".


In [9]:
for leg in legislators:
    if leg[3] == '':
        leg[3] = 'M'

4: Parsing birth years

Instructions

Loop through the rows in legislators

Inside the loop, get the birthday column from the row, and split the birthday.

After splitting the birthday, get the birth year, and append it to birth_years

At the end, birth_years will contain the birth years of all the congresspeople in the data.


In [11]:
birth_years = []

for row in legislators:
    birth_year = row[2].split("-")[0]
    birth_years.append(birth_year)
print(birth_years)


['1745', '1742', '1743', '1730', '1739', '', '1738', '1745', '1748', '1734', '1756', '', '1737', '1754', '1736', '', '1727', '1733', '1732', '1737', '1739', '1734', '1740', '1745', '1728', '', '1738', '1737', '1739', '1744', '', '1761', '1756', '1752', '1737', '1745', '1744', '1742', '1726', '', '1733', '1741', '', '1756', '', '1721', '', '1764', '', '1740', '1745', '1745', '', '1735', '1745', '', '1753', '1748', '1749', '1740', '1746', '1729', '', '', '1726', '1748', '1756', '', '1753', '', '', '1753', '', '1739', '', '1738', '1741', '1750', '1761', '', '1758', '1744', '1759', '1743', '1736', '', '1741', '1739', '', '1745', '1751', '1743', '1754', '1758', '1743', '1764', '1727', '1749', '1751', '1739', '1753', '', '1758', '1752', '', '1755', '1753', '1747', '', '1751', '1752', '1760', '1756', '', '1753', '1754', '1749', '1758', '1743', '', '1741', '1729', '1755', '', '1750', '1760', '1761', '1743', '1757', '1746', '1765', '', '1753', '1728', '', '', '', '', '1759', '', '1740', '1749', '1749', '1748', '1752', '', '', '', '1763', '', '1739', '', '', '1735', '', '1740']

6: Practice with enumerate

Instructions

Use a for loop to enumerate the ships list.

In the body of the loop, print the ship at the current index, then the car at the current index.

Make sure you have two separate print statements.


In [12]:
dogs = ["labrador", "poodle", "collie"]
cats = ["siamese", "persian", "somali"]

# Enumerate the dogs list, and print the values.
for i, dog in enumerate(dogs):
    # Will print the dog at the current loop iteration.
    print(dog)
    # This will equal dog.  Prints the dog at index i.
    print(dogs[i])
    # Print the cat at index i.
    print(cats[i])

ships = ["Andrea Doria", "Titanic", "Lusitania"]
cars = ["Ford Edsel", "Ford Pinto", "Yugo"]

for i, e in enumerate(ships):
    print(e)
    print(cars[i])


labrador
labrador
siamese
poodle
poodle
persian
collie
collie
somali
Andrea Doria
Ford Edsel
Titanic
Ford Pinto
Lusitania
Yugo

7: Create a birth year column

Instructions

Loop through the rows in legislators list, and append the corresponding value in birth_years to each row.


In [14]:
lolists = [["apple", "monkey"], ["orange", "dog"], ["banana", "cat"]]
trees = ["cedar", "maple", "fig"]

for i, row in enumerate(lolists):
    row.append(trees[i])

# Our list now has a new column containing the values from trees.
print(lolists)

# Legislators and birth_years have both been loaded in.


[['apple', 'monkey', 'cedar'], ['orange', 'dog', 'maple'], ['banana', 'cat', 'fig']]

In [15]:
for i, e in enumerate(legislators):
    e.append(birth_years[i])

9: Practice with list comprehensions

Double all of the prices in apple_price, and assign the resulting list to apple_price_doubled.

Subtract 100 from all of the prices in apple_price, and assign the resulting list to apple_price_lowered.


In [16]:
# Define a list of lists
data = [["tiger", "lion"], ["duck", "goose"], ["cardinal", "bluebird"]]

# Extract the first column from the list
first_column = [row[0] for row in data]

apple_price = [100, 101, 102, 105]

In [17]:
apple_price_doubled = [2*p for p in apple_price]
apple_price_lowered = [p-100 for p in apple_price]

print(apple_price_doubled, apple_price_lowered)


[200, 202, 204, 210] [0, 1, 2, 5]

10: Convert birth years to integers


In [20]:
for row in legislators:
    try:
        row[7] = int(row[7])
    except ValueError as verr:
        row[7] = None

# Hmm, but the above code fails.
# It fails because there is a value in the column that can't be converted to an int.
# Remember how some genders were missing?  It also looks like some birthdays were missing, which is giving us invalid values in the birth years column.

12: Practice with try/except

Instructions

Use try/except statements to parse another_invalid_int and another_valid_int.

Assign 0 to another_invalid_int in the except block.

At the end, another_valid_int will be parsed properly, and another_invalid_int will be 0.


In [22]:
# Cannot be parsed into an int with the int() function.
invalid_int = ""

# Can be parsed into an int.
valid_int = "10"

# Parse the valid int
try:
    valid_int = int(valid_int)
except Exception:
    # This code is never run, because there is no error parsing valid_int into an integer.
    valid_int = 0

# Try to parse the invalid int
try:
    invalid_int = int(invalid_int)
except Exception:
    # The parsing fails, so we end up here.
    # The code here will be run, and will assign 0 to invalid_int.
    invalid_int = 0

print(valid_int)
print(invalid_int)

another_invalid_int = "Oregon"
another_valid_int = "1000"

try:
    another_invalid_int = int(another_invalid_int)
except Exception as ex:
    another_invalid_int = 0
    
try:
    another_valid_int = int(another_valid_int)
except Exception as ex:
    another_valid_int = 0


10
0

In [23]:
print(another_invalid_int, another_valid_int)


0 1000

13: The pass keyword

Instructions

Use a try/except block to parse valid_int into an integer.

Use the pass keyword inside the except block.


In [25]:
invalid_int = ""
try:
    # This parsing will fail
    invalid_int = int(invalid_int)
except Exception:
    # Nothing will happen in the body of the except statement, because we are passing.
    pass

# invalid_int still has the same value.
print(invalid_int)

# We can also use the pass statement with for loops.
# (although it's less useful in this example)
a = [1,4,5]
for i in a:
    pass

# And if statements.
if 10 > 5:
    pass

# We can use the pass keyword inside the body of any statement that ends with a colon.
valid_int = "10"

try:
    valid_int = int(valid_int)
except:
    pass

print(valid_int)


10

14: Convert birth years to integers

Instructions

Loop over the rows in legislators, and convert the values in the birth year column to integers.

In cases where parsing fails, assign 0 as the value.


In [26]:
for row in legislators:
    try:
        row[7] = int(row[7])
    except Exception as ex:
        row[7] = 0

In [27]:
print(legislators)


[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', 1743], ['Carroll', 'Daniel', '1730-07-22', 'M', 'rep', 'MD', '', 1730], ['Clymer', 'George', '1739-03-16', 'M', 'rep', 'PA', '', 1739], ['Contee', 'Benjamin', '', 'M', 'rep', 'MD', '', 0], ['Dalton', 'Tristram', '1738-05-28', 'M', 'sen', 'MA', 'Pro-Administration', 1738], ['Elmer', 'Jonathan', '1745-11-29', 'M', 'sen', 'NJ', 'Pro-Administration', 1745], ['Few', 'William', '1748-06-08', 'M', 'sen', 'GA', 'Anti-Administration', 1748], ['Floyd', 'William', '1734-12-17', 'M', 'rep', 'NY', '', 1734], ['Gale', 'George', '1756-06-03', 'M', 'rep', 'MD', '', 1756], ['Grayson', 'William', '', 'M', 'sen', 'VA', 'Anti-Administration', 0], ['Grout', 'Jonathan', '1737-07-23', 'M', 'rep', 'MA', '', 1737], ['Hawkins', 'Benjamin', '1754-08-15', 'M', 'sen', 'NC', 'Pro-Administration', 1754], ['Huntington', 'Benjamin', '1736-04-19', 'M', 'rep', 'CT', '', 1736], ['Izard', 'Ralph', '', 'M', 'sen', 'SC', 'Pro-Administration', 0], ['Johnson', 'William', '1727-10-07', 'M', 'sen', 'CT', 'Pro-Administration', 1727], ['Johnston', 'Samuel', '1733-12-15', 'M', 'sen', 'NC', 'Pro-Administration', 1733], ['Lee', 'Richard', '1732-01-20', 'M', 'sen', 'VA', 'Anti-Administration', 1732], ['Maclay', 'William', '1737-07-20', 'M', 'sen', 'PA', 'Anti-Administration', 1737], ['Mathews', 'George', '1739-08-30', 'M', 'rep', 'GA', '', 1739], ['Morris', 'Robert', '1734-01-20', 'M', 'sen', 'PA', 'Pro-Administration', 1734], ['Partridge', 'George', '1740-02-08', 'M', 'rep', 'MA', '', 1740], ['Paterson', 'William', '1745-12-24', 'M', 'sen', 'NJ', 'Pro-Administration', 1745], ['Smith', 'William', '1728-04-12', 'M', 'rep', 'MD', '', 1728], ['Stone', 'Michael', '', 'M', 'rep', 'MD', '', 0], ['Van Rensselaer', 'Jeremiah', '1738-08-27', 'M', 'rep', 'NY', '', 1738], ['Wynkoop', 'Henry', '1737-03-02', 'M', 'rep', 'PA', '', 1737], ['Dickinson', 'Philemon', '1739-04-05', 'M', 'sen', 'NJ', 'Pro-Administration', 1739], ['Walker', 'John', '1744-02-13', 'M', 'sen', 'VA', 'Pro-Administration', 1744], ['Ashe', 'John', '', 'M', 'rep', 'NC', '', 0], ['Barnwell', 'Robert', '1761-12-21', 'M', 'rep', 'SC', '', 1761], ['Burr', 'Aaron', '1756-02-06', 'M', 'sen', 'NY', 'Republican', 1756], ['Cabot', 'George', '1752-12-03', 'M', 'sen', 'MA', 'Federalist', 1752], ['Carroll', 'Charles', '1737-09-19', 'M', 'sen', 'MD', '', 1737], ['Ellsworth', 'Oliver', '1745-04-29', 'M', 'sen', 'CT', 'Federalist', 1745], ['Gerry', 'Elbridge', '1744-07-17', 'M', 'rep', 'MA', '', 1744], ['Huger', 'Daniel', '1742-02-20', 'M', 'rep', 'SC', '', 1742], ['Jacobs', 'Israel', '1726-06-09', 'M', 'rep', 'PA', '', 1726], ['Key', 'Philip', '', 'M', 'rep', 'MD', '', 0], ['Read', 'George', '1733-09-18', 'M', 'sen', 'DE', '', 1733], ['Robinson', 'Moses', '1741-03-22', 'M', 'sen', 'VT', 'Anti-Administration', 1741], ['Schoonmaker', 'Cornelius', '', 'M', 'rep', 'NY', '', 0], ['Seney', 'Joshua', '1756-03-04', 'M', 'rep', 'MD', '', 1756], ['Sheredine', 'Upton', '', 'M', 'rep', 'MD', '', 0], ['Sherman', 'Roger', '1721-04-19', 'M', 'sen', 'CT', 'Pro-Administration', 1721], ['Silvester', 'Peter', '', 'M', 'rep', 'NY', '', 0], ['Steele', 'John', '1764-11-16', 'M', 'rep', 'NC', '', 1764], ['Sterett', 'Samuel', '', 'M', 'rep', 'MD', '', 0], ['Sturges', 'Jonathan', '1740-08-23', 'M', 'rep', 'CT', '', 1740], ['Tucker', 'Thomas', '1745-06-25', 'M', 'rep', 'SC', '', 1745], ['Wayne', 'Anthony', '1745-01-01', 'M', 'rep', 'GA', '', 1745], ['White', 'Alexander', '', 'M', 'rep', 'VA', '', 0], ['Williamson', 'Hugh', '1735-12-05', 'M', 'rep', 'NC', 'Federalist', 1735], ['Willis', 'Francis', '1745-01-05', 'M', 'rep', 'GA', '', 1745], ['Edwards', 'John', '', 'M', 'sen', 'KY', 'Anti-Administration', 0], ['Potts', 'Richard', '1753-07-19', 'M', 'sen', 'MD', 'Federalist', 1753], ['Armstrong', 'James', '1748-08-29', 'M', 'rep', 'PA', '', 1748], ['Beatty', 'John', '1749-12-10', 'M', 'rep', 'NJ', '', 1749], ['Boudinot', 'Elias', '1740-05-02', 'M', 'rep', 'NJ', '', 1740], ['Bourne', 'Shearjashub', '1746-06-14', 'M', 'rep', 'MA', '', 1746], ['Bradford', 'William', '1729-11-04', 'M', 'sen', 'RI', 'Federalist', 1729], ['Cadwalader', 'Lambert', '', 'M', 'rep', 'NJ', '', 0], ['Carnes', 'Thomas', '', 'M', 'rep', 'GA', '', 0], ['Clark', 'Abraham', '1726-02-15', 'M', 'rep', 'NJ', '', 1726], ['Cobb', 'David', '1748-09-14', 'M', 'rep', 'MA', '', 1748], ['Coffin', 'Peleg', '1756-11-03', 'M', 'rep', 'MA', '', 1756], ['Dawson', 'William', '', 'M', 'rep', 'NC', 'Anti-Administration', 0], ['Edwards', 'Benjamin', '1753-08-12', 'M', 'rep', 'MD', '', 1753], ['Fitzsimons', 'Thomas', '', 'M', 'rep', 'PA', '', 0], ['Forrest', 'Uriah', '', 'M', 'rep', 'MD', '', 0], ['Frelinghuysen', 'Frederick', '1753-04-13', 'M', 'sen', 'NJ', 'Federalist', 1753], ['Gillon', 'Alexander', '', 'M', 'rep', 'SC', '', 0], ['Gordon', 'James', '1739-10-31', 'M', 'rep', 'NY', '', 1739], ['Griffin', 'Samuel', '', 'M', 'rep', 'VA', '', 0], ['Holten', 'Samuel', '1738-06-09', 'M', 'rep', 'MA', '', 1738], ['Irvine', 'William', '1741-11-03', 'M', 'rep', 'PA', '', 1741], ['Learned', 'Amasa', '1750-11-15', 'M', 'rep', 'CT', '', 1750], ['Lee', 'Richard', '1761-01-20', 'M', 'rep', 'VA', '', 1761], ['Martin', 'Alexander', '', 'M', 'sen', 'NC', 'Republican', 0], ['McDowell', 'Joseph', '1758-02-25', 'M', 'rep', 'NC', '', 1758], ['Mebane', 'Alexander', '1744-11-26', 'M', 'rep', 'NC', '', 1744], ['Mercer', 'John', '1759-05-17', 'M', 'rep', 'MD', '', 1759], ['Mitchell', 'Stephen', '1743-12-09', 'M', 'sen', 'CT', 'Pro-Administration', 1743], ['Montgomery', 'William', '1736-08-03', 'M', 'rep', 'PA', '', 1736], ['Neville', 'Joseph', '', 'M', 'rep', 'VA', '', 0], ['Niles', 'Nathaniel', '1741-04-03', 'M', 'rep', 'VT', '', 1741], ['Pickens', 'Andrew', '1739-09-13', 'M', 'rep', 'SC', '', 1739], ['Scott', 'Thomas', '', 'M', 'rep', 'PA', '', 0], ['Strong', 'Caleb', '1745-01-09', 'M', 'sen', 'MA', 'Federalist', 1745], ['Talbot', 'Silas', '1751-01-11', 'M', 'rep', 'NY', '', 1751], ['Tredwell', 'Thomas', '1743-02-06', 'M', 'rep', 'NY', '', 1743], ['Van Gaasbeck', 'Peter', '1754-09-27', 'M', 'rep', 'NY', '', 1754], ['Vining', 'John', '1758-12-23', 'M', 'sen', 'DE', 'Federalist', 1758], ['Wadsworth', 'Jeremiah', '1743-07-12', 'M', 'rep', 'CT', '', 1743], ['Walker', 'Francis', '1764-06-22', 'M', 'rep', 'VA', '', 1764], ['Ward', 'Artemas', '1727-11-26', 'M', 'rep', 'MA', '', 1727], ['Watts', 'John', '1749-08-27', 'M', 'rep', 'NY', '', 1749], ['Williams', 'Benjamin', '1751-01-01', 'M', 'rep', 'NC', '', 1751], ['Wingate', 'Paine', '1739-05-14', 'M', 'rep', 'NH', '', 1739], ['Tazewell', 'Henry', '1753-11-27', 'M', 'sen', 'VA', 'Republican', 1753], ['Walton', 'George', '', 'M', 'sen', 'GA', 'Federalist', 0], ['Ames', 'Fisher', '1758-04-09', 'M', 'rep', 'MA', 'Federalist', 1758], ['Bingham', 'William', '1752-03-08', 'M', 'sen', 'PA', 'Federalist', 1752], ['Bloodworth', 'Timothy', '', 'M', 'sen', 'NC', 'Republican', 0], ['Bourne', 'Benjamin', '1755-09-09', 'M', 'rep', 'RI', 'Federalist', 1755], ['Buck', 'Daniel', '1753-11-09', 'M', 'rep', 'VT', 'Federalist', 1753], ['Coles', 'Isaac', '1747-03-02', 'M', 'rep', 'VA', 'Republican', 1747], ['Crabb', 'Jeremiah', '', 'M', 'rep', 'MD', 'Federalist', 0], ['Dearborn', 'Henry', '1751-02-23', 'M', 'rep', 'MA', 'Republican', 1751], ['Duvall', 'Gabriel', '1752-12-06', 'M', 'rep', 'MD', 'Republican', 1752], ['Earle', 'Samuel', '1760-11-28', 'M', 'rep', 'SC', 'Republican', 1760], ['Gilbert', 'Ezekiel', '1756-03-25', 'M', 'rep', 'NY', 'Federalist', 1756], ['Greenup', 'Christopher', '', 'M', 'rep', 'KY', 'Republican', 0], ['Gunn', 'James', '1753-03-13', 'M', 'sen', 'GA', 'Federalist', 1753], ['Hancock', 'George', '1754-06-13', 'M', 'rep', 'VA', 'Federalist', 1754], ['Hathorn', 'John', '1749-01-09', 'M', 'rep', 'NY', 'Republican', 1749], ['Heath', 'John', '1758-05-08', 'M', 'rep', 'VA', 'Republican', 1758], ['Henderson', 'Thomas', '1743-08-15', 'M', 'rep', 'NJ', 'Federalist', 1743], ['Henry', 'John', '', 'M', 'sen', 'MD', 'Federalist', 0], ['Langdon', 'John', '1741-06-26', 'M', 'sen', 'NH', 'Republican', 1741], ['Leonard', 'George', '1729-07-04', 'M', 'rep', 'MA', 'Federalist', 1729], ['Lyman', 'William', '1755-12-07', 'M', 'rep', 'MA', 'Republican', 1755], ['Marshall', 'Humphrey', '', 'M', 'sen', 'KY', 'Federalist', 0], ['Muhlenberg', 'Frederick', '1750-01-01', 'M', 'rep', 'PA', 'Republican', 1750], ['Murray', 'William', '1760-02-09', 'M', 'rep', 'MD', 'Federalist', 1760], ['Orr', 'Alexander', '1761-11-06', 'M', 'rep', 'KY', 'Republican', 1761], ['Page', 'John', '1743-04-17', 'M', 'rep', 'VA', 'Republican', 1743], ['Paine', 'Elijah', '1757-01-21', 'M', 'sen', 'VT', 'Federalist', 1757], ['Patten', 'John', '1746-04-26', 'M', 'rep', 'DE', 'Republican', 1746], ['Preston', 'Francis', '1765-08-02', 'M', 'rep', 'VA', 'Republican', 1765], ['Read', 'Jacob', '', 'M', 'sen', 'SC', 'Federalist', 0], ['Richards', 'John', '1753-04-18', 'M', 'rep', 'PA', 'Republican', 1753], ['Rutherford', 'Robert', '1728-10-20', 'M', 'rep', 'VA', 'Republican', 1728], ['Sherburne', 'John', '', 'M', 'rep', 'NH', 'Republican', 0], ['Smith', 'Isaac', '', 'M', 'rep', 'NJ', 'Federalist', 0], ['Sprigg', 'Thomas', '', 'M', 'rep', 'MD', 'Republican', 0], ['Strudwick', 'William', '', 'M', 'rep', 'NC', 'Federalist', 0], ['Swift', 'Zephaniah', '1759-02-27', 'M', 'rep', 'CT', 'Federalist', 1759], ['Tatom', 'Absalom', '', 'M', 'rep', 'NC', 'Republican', 0], ['Trumbull', 'Jonathan', '1740-03-26', 'M', 'sen', 'CT', 'Federalist', 1740], ['White', 'James', '1749-06-16', 'M', 'rep', 'TN', '', 1749], ['Blount', 'William', '1749-03-26', 'M', 'sen', 'TN', 'Republican', 1749], ['Goodhue', 'Benjamin', '1748-09-20', 'M', 'sen', 'MA', 'Federalist', 1748], ['Howard', 'John', '1752-06-04', 'M', 'sen', 'MD', 'Federalist', 1752], ['Hunter', 'John', '', 'M', 'sen', 'SC', 'Republican', 0], ['Laurance', 'John', '', 'M', 'sen', 'NY', 'Federalist', 0], ['Tattnall', 'Josiah', '', 'M', 'sen', 'GA', 'Republican', 0], ['Allen', 'John', '1763-06-12', 'M', 'rep', 'CT', 'Federalist', 1763], ['Benton', 'Lemuel', '', 'M', 'rep', 'SC', 'Republican', 0], ['Bradbury', 'Theophilus', '1739-11-13', 'M', 'rep', 'MA', 'Federalist', 1739], ['Brooks', 'David', '', 'M', 'rep', 'NY', 'Federalist', 0], ['Bryan', 'Nathan', '', 'M', 'rep', 'NC', 'Republican', 0], ['Bullock', 'Stephen', '1735-10-10', 'M', 'rep', 'MA', 'Federalist', 1735], ['Burges', 'Dempsey', '', 'M', 'rep', 'NC', 'Republican', 0], ['Chapman', 'John', '1740-10-18', 'M', 'rep', 'PA', 'Federalist', 1740]]

15: Fill in years without a value

Instructions

Loop through legislators, and replace any values in the birth_year column that are 0 with the previous value.


In [28]:
data = [[1,1],[0,5],[10,7]]
last_value = 0

# There are some holes in this code -- it won't work properly if the first birth year is 0, for example, but its fine for now.
# It keeps track of the last value in the column in the last_value variable.
# If it finds an item that equals 0, it replaces the value with the last value.
for row in data:
    # Check if the item is 0.
    if row[0] == 0:
        # If it is, replace it with the last value.
        row[0] = last_value
    # Set last value equal to the item -- we need to do this in order to keep track of what the previous value was, so we can use it for replacement.
    last_value = row[0]

# The 0 value in the second row, first column has been replaced with a 1.
print(data)


[[1, 1], [1, 5], [10, 7]]

Answer


In [29]:
last_birth_year = 0
for row in legislators:
    if row[7] == 0:
        row[7] = last_birth_year
    last_birth_year = row[7]

16: Counting up the female names

Instructions

Count up how many times each female name occurs in legislators. First name is the second column.

You'll need to make sure that gender (fourth column) equals "F", and that birth year (eighth column) is greater than 1940.

Store the first name key and the counts in the female_name_counts dictionary.

You'll need to use nested if statements to first check if gender and birth year are valid, and then to check if the first name is in female_name_counts.


In [30]:
names = ["Jim", "Bob", "Bob", "JimBob", "Joe", "Jim"]
name_counts = {}
for name in names:
    if name in name_counts:
        name_counts[name] = name_counts[name] + 1
    else:
        name_counts[name] = 1

female_name_counts = {}

Answer


In [31]:
female_name_counts = {}

for row in legislators:
    if row[3] == 'F' and row[7] > 1940:
        fname = row[1]
        if fname in female_name_counts:
            female_name_counts[fname] += 1
        else:
            female_name_counts[fname] = 1

18: Practicing with the None type

Instructions

Check whether c equals None, and assign the result to c_none.

Check whether d equals None, and assign the result to d_none.


In [34]:
# Set a variable equal to the None type
a = None
# A normal variable
b = 1

# This is True
print(a is None)
# And this is False
print(b is None)

# a is of the None type
print(type(a))

# Assigns whether a equals None to a_none
a_none = a is None
# Evaluates to True
print(a_none)

c = None
d = "Bamboo"


True
False
<class 'NoneType'>
True

Answer


In [35]:
c_none, d_none = c is None, d is None
print(c_none, d_none)


True False

19: Finding maximums with the None type

Instructions

Use a for loop to set min_val equal to the smallest value in income.


In [36]:
max_val = None
data = [-10, -20, -50, -100]
for i in data:
    # If max_val equals None, or i is greater than max_val, then set max_val equal to i.
    # This ensures that no matter how small the values in data are, max_val will always get changed to a value in the list.
    # If you are checking if a value equals None and you are using it with and or or, then the None check always needs to come first.
    if max_val is None or i > max_val:
        max_val = i

min_val = None
income = [100,700,100,50,100,40,56,31,765,1200,1400,32,6412,987]

Answer


In [37]:
min_val = None

for inc in income:
    if min_val is None or inc < min_val:
        min_val = inc
        
print(min_val)


31

20: Finding how many times the top female names occur

Instructions

Loop through the keys in female_name_counts, and get the value associated with the key.

Assign the value to max_value if it is larger, or if max_value is None.

At the end of the loop, max_value will be the largest value in the dictionary.


In [38]:
# female_name_counts has been loaded in.
max_value = None

Answer


In [39]:
for key in female_name_counts:
    val = female_name_counts[key]
    if max_value is None or val > max_value:
        max_value = val

21: Finding the female names that occur the most

Instructions

Loop through the keys in female_name_counts.

If any value equals 2, append the key to top_female_names.

At the end, top_female_names will be a list of the most occurring female congressperson names.


In [41]:
# female_name_counts has been loaded in.
top_female_names = []

Answer


In [42]:
for key in female_name_counts:
    value = female_name_counts[key]
    if value == 2:
        top_female_names.append(key)
        
print(top_female_names)


[]

23: Practice with the items method

Instructions

Use the .items() method along with a for loop to loop through plant_types.

Inside the loop, print the key, and then the value.


In [43]:
animal_types = {"robin": "bird", "pug": "dog", "osprey": "bird"}

# The .items method lets us access a dictionary key and value in a loop.
for key,value in animal_types.items():
    print(key)
    print(value)
    # This is equal to the value
    print(animal_types[key])

plant_types = {"orchid": "flower", "cedar": "tree", "maple": "tree"}


osprey
bird
bird
robin
bird
bird
pug
dog
dog

Answer


In [44]:
for key, val in plant_types.items():
    print(key)
    print(val)


orchid
flower
cedar
tree
maple
tree

24: Finding the male names that occur the most

Instructions

Loop through legislators, and count up how much each name where the gender column equals "M" and the birth year is after 1940 occurs. Store the results in a dictionary.

Then find the highest value in that dictionary.

Finally, loop through the dictionary and append any keys where the value equals the highest value to top_male_names.


In [45]:
# legislators has been loaded in.

top_male_names = []

Answer


In [49]:
male_name_counts = {}

print(len(legislators))

for row in legislators:
    if row[3] == 'M' and row[7] > 1940:
        fname = row[1]
        if fname in male_name_counts:
            male_name_counts[fname] += 1
        else:
            male_name_counts[fname] = 1
print(male_name_counts)     
max_value = None
for key in male_name_counts:
    val = male_name_counts[key]
    if max_value is None or val > max_value:
        max_value = val
        
for key, val in male_name_counts.items():
    if val == max_value:
        top_male_names.append(key)

print(top_male_names)


156
{}
[]

In [ ]: