## 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

``````

``````

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]:

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])

``````
``````

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)

``````
``````

``````

## 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]]

``````

``````

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 = {}

``````

``````

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

``````

``````

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]

``````

``````

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

``````

``````

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 = []

``````

``````

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

``````

``````

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 = []

``````

``````

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 [ ]:

``````