2: Writing a while loop

Instructions

Create a while loop that tests if b is greater than 5. If it is, the loop body should print b out, then subtract one from it.


In [1]:
x = 3
# The loop body will execute three times.  Once when x == 3, once when x == 4, and once when x == 5.
# Then x < 6 will evaluate to False, and it will stop.
# 3, 4, and 5 will be printed out.
while x < 6:
    print(x)
    # Using += is a shorter way of saying x = x + 1.  It will add one to x.
    x += 1

b = 10


3
4
5

Answer


In [2]:
while b > 5:
    print(b)
    b -= 1


10
9
8
7
6

3: Using the break keyword

Instructions

Let's say we want two "Bengal" tigers from available_tigers for our nature reserve.

Write a for loop that increments tiger_count when it finds one, and breaks after finding two.


In [3]:
available_count = 0
desired_dog = "Great Dane"
available_dogs = ["Labrador", "Poodle", "Sheepdog", "Great Dane", "Pomeranian", "Great Dane", "Collie"]

# Let's say we are searching for two dogs of the same breed to adopt.
# We'll loop through the dogs.
for dog in available_dogs:
    # If our desired dog is found.
    if dog == desired_dog:
        # Increment the counter.
        available_count += 1
        # We only want two dogs, so we can stop searching after we find them.
        if available_count == 2:
            break

tiger_count = 0
desired_tiger = "Bengal"
available_tigers = ["Bengal", "Dressed up poodle", "Siberian", "Sumatran", "Bengal", "Housecat", "Hobbes"]

Answer


In [8]:
for t in available_tigers:
    if t == "Bengal":
        tiger_count += 1
    if tiger_count == 2:
        break

4: Finding a column number from a name

Instructions

Write a function that will get the column number from the column name.

Use it to get the column number for the "arr_delay" column and assign it to the arr_delay variable.

Use it to get the column number for the "weather_delay" column and assign it to the weather_delay variable.


In [9]:
column_names = ['year',
 'month',
 'carrier',
 'carrier_name',
 'airport',
 'airport_name',
 'arr_flights',
 'arr_del15',
 'carrier_ct',
 'weather_ct',
 'nas_ct',
 'security_ct',
 'late_aircraft_ct',
 'arr_cancelled',
 'arr_diverted',
 'arr_delay',
 'carrier_delay',
 'weather_delay',
 'nas_delay',
 'security_delay',
 'late_aircraft_delay']

In [10]:
# It's pretty easy to get a column name from a column number.
# The third column contains the carrier (same as the airline).
print(column_names[2])


carrier

Answer


In [11]:
def number_by_name(name):
    found = None
    for i, nm in enumerate(column_names):
        if nm == name:
            found = i
            break
    return found
            

arr_delay = number_by_name("arr_delay")
weather_delay = number_by_name("weather_delay")
print(arr_delay, weather_delay)


15 17

5: Using negative indexing

Instructions

Use negative indexing to assign the third to last row in flight_delays to third_to_last.

Use negative slicing to assign the fourth, third, and second to last rows in flight_delays to end_slice


In [13]:
from flight_delays import flight_delays
# Prints the last row in flight_delays
print(flight_delays[-1])

# Prints the second to last row in flight_delays
print(flight_delays[-2])

# Prints the third to last and second to last rows in flight_delays (remember that slicing only goes up to but not including the second number)
# This will get the rows at index -3 and -2
print(flight_delays[-3:-1])


['2014', '10', 'AA', 'American Airlines Inc.', 'CMH', 'Columbus OH: Port Columbus International', '169', '50', '17.61', '1.01', '11.5', '0', '19.87', '1', '0', '3203', '889', '106', '360', '0', '1848']
['2014', '10', 'AA', 'American Airlines Inc.', 'BWI', 'Baltimore MD: Baltimore/Washington International Thurgood Marshall', '208', '51', '15.06', '1.24', '22.92', '0.75', '11.04', '2', '0', '2388', '872', '49', '734', '18', '715']
[['2014', '10', 'AA', 'American Airlines Inc.', 'TPA', 'Tampa FL: Tampa International', '397', '89', '44.79', '1.28', '15.62', '0', '27.31', '2', '0', '4886', '1847', '318', '743', '0', '1978'], ['2014', '10', 'AA', 'American Airlines Inc.', 'BWI', 'Baltimore MD: Baltimore/Washington International Thurgood Marshall', '208', '51', '15.06', '1.24', '22.92', '0.75', '11.04', '2', '0', '2388', '872', '49', '734', '18', '715']]

Answer


In [14]:
third_to_last = flight_delays[-3]
end_slice = flight_delays[-4:-1]

print(third_to_last, end_slice)


['2014', '10', 'AA', 'American Airlines Inc.', 'TPA', 'Tampa FL: Tampa International', '397', '89', '44.79', '1.28', '15.62', '0', '27.31', '2', '0', '4886', '1847', '318', '743', '0', '1978'] [['2014', '10', 'AA', 'American Airlines Inc.', 'MSY', 'New Orleans LA: Louis Armstrong New Orleans International', '270', '59', '27.46', '3.92', '7.61', '0', '20.01', '6', '1', '3024', '1274', '186', '372', '0', '1192'], ['2014', '10', 'AA', 'American Airlines Inc.', 'TPA', 'Tampa FL: Tampa International', '397', '89', '44.79', '1.28', '15.62', '0', '27.31', '2', '0', '4886', '1847', '318', '743', '0', '1978']]

6: Indexing up to the end or from the beginning

Instructions

Assign the first 10 rows of flight_delays to first_ten_rows.

Assign the last 10 rows of flight_delays to last_ten_rows.


In [15]:
# Leaving the first number in the slice blank means "start from the beginning of the list, inclusive"
# This code will get the rows at index 0, 1, 2, 3, and 4.
first_five_rows = flight_delays[:5]

# We can also leave the last number blank to get all rows up to and including the last one.
# This will get the rows at index -5, -4, -3, -2, and -1
last_five_rows = flight_delays[-5:]

Answer


In [18]:
first_ten_rows = flight_delays[:10]
last_ten_rows = flight_delays[-10:]

In [17]:
print(first_ten_rows, last_ten_rows)


[['2014', '10', 'AA', 'American Airlines Inc.', 'JFK', 'New York NY: John F. Kennedy International', '1293', '233', '63.33', '0.69', '117.86', '0', '51.12', '4', '6', '11770', '3508', '64', '5207', '0', '2991'], ['2014', '10', 'AA', 'American Airlines Inc.', 'LAX', 'Los Angeles CA: Los Angeles International', '2695', '401', '128.57', '15.93', '112.75', '4.54', '139.21', '6', '6', '23523', '8222', '1691', '4299', '228', '9083'], ['2014', '10', 'AA', 'American Airlines Inc.', 'DFW', 'Dallas/Fort Worth TX: Dallas/Fort Worth International', '13310', '2929', '555.62', '85.27', '1073.13', '1.79', '1213.2', '152', '64', '207040', '55348', '7329', '57265', '49', '87049'], ['2014', '10', 'AA', 'American Airlines Inc.', 'OGG', 'Kahului HI: Kahului Airport', '110', '13', '4.99', '0', '3.31', '0', '4.7', '0', '0', '516', '245', '0', '75', '0', '196'], ['2014', '10', 'AA', 'American Airlines Inc.', 'HNL', 'Honolulu HI: Honolulu International', '174', '26', '16.61', '0.33', '2.83', '0', '6.23', '0', '0', '1647', '938', '9', '100', '0', '600'], ['2014', '10', 'AA', 'American Airlines Inc.', 'SFO', 'San Francisco CA: San Francisco International', '971', '284', '66.23', '7.16', '131.3', '1', '78.31', '3', '4', '15932', '3499', '364', '6514', '101', '5454'], ['2014', '10', 'AA', 'American Airlines Inc.', 'ATL', 'Atlanta GA: Hartsfield-Jackson Atlanta International', '363', '87', '43.88', '2.85', '18.56', '0', '21.72', '3', '2', '5032', '2141', '272', '1093', '0', '1526'], ['2014', '10', 'AA', 'American Airlines Inc.', 'BOS', 'Boston MA: Logan International', '1010', '265', '64.9', '4.45', '125.52', '0.8', '69.32', '9', '1', '15787', '3347', '270', '7005', '94', '5071'], ['2014', '10', 'AA', 'American Airlines Inc.', 'ORD', "Chicago IL: Chicago O'Hare International", '4419', '1626', '158.35', '14.84', '1073.66', '0.21', '378.94', '49', '4', '96666', '10221', '1282', '60924', '12', '24227'], ['2014', '10', 'AA', 'American Airlines Inc.', 'LBB', 'Lubbock TX: Lubbock Preston Smith International', '59', '19', '7.1', '1.35', '2.18', '0', '8.37', '1', '0', '1325', '341', '44', '132', '0', '808']] []

7: Finding the percentage of delayed flights

Instructions

Sum together the values in the "arr_del15" column. This is the total number of arriving flights in each airport that were delayed more than 15 minutes.

Then, divide the number of delayed flights by total_arriving_flights. Assign the result to delayed_percentage.


In [20]:
def column_number_from_name(column_name):
    column_number = None
    for i, column in enumerate(column_names):
        if column == column_name:
            column_nudef column_number_from_name(column_name):
    column_number = None
    for i, column in enumerate(column_names):
        if column == column_name:
            column_number = i
    return column_number

# Get the column number of the arr_flight column
# This column counts the total number of arriving flights for a carrier in a given airport
arr_flights_column = column_number_from_name("arr_flights")

# Extract all of the values in the column using a list comprehension
# We need to convert the values to float because they are strings initially
arr_flights = [float(row[arr_flights_column]) for row in flight_delays]

# Now we can use the sum() function to add together all of the values.
total_arriving_flights = sum(arr_flights)mber = i
    return column_number

# Get the column number of the arr_flight column
# This column counts the total number of arriving flights for a carrier in a given airport
arr_flights_column = column_number_from_name("arr_flights")

# Extract all of the values in the column using a list comprehension
# We need to convert the values to float because they are strings initially
arr_flights = [float(row[arr_flights_column]) for row in flight_delays]

# Now we can use the sum() function to add together all of the values.
total_arriving_flights = sum(arr_flights)

Answer


In [36]:
arr_del15 = [float(row[column_number_from_name("arr_del15")]) for row in flight_delays]
print(arr_del15)
total_arr_del15 = sum(arr_del15)
delayed_percentage = total_arr_del15/total_arriving_flights


[233.0, 401.0, 2929.0, 13.0, 26.0, 284.0, 87.0, 265.0, 1626.0, 19.0, 57.0, 186.0, 148.0, 108.0, 112.0, 43.0, 17.0, 117.0, 41.0, 210.0, 154.0, 75.0, 26.0, 77.0, 22.0, 86.0, 98.0, 42.0, 54.0, 95.0, 428.0, 45.0, 57.0, 41.0, 3.0, 31.0, 59.0, 89.0, 51.0, 50.0]

In [37]:
print(delayed_percentage)


0.22718773373223636

8: Finding the average delay time

Instructions

Find the sum of the "arr_delay" column.

Then, divide it by the sum of the "arr_del15" column to get the average number of minutes a plane was delayed.

Assign the result to average_delay_time.


In [38]:
def column_number_from_name(column_name):
    column_number = None
    for i, column in enumerate(column_names):
        if column == column_name:
            column_number = i
    return column_number

average_delay_time = None

Answer


In [40]:
sum_arr_delay = sum([float(row[column_number_from_name("arr_delay")]) for row in flight_delays])
arr_del15 = [float(row[column_number_from_name("arr_del15")]) for row in flight_delays]
sum_arr_del15 = sum(arr_del15)
average_delay_time = sum_arr_delay/sum_arr_del15
print(average_delay_time)


61.24656084656085

9: Making a function to calculate the delay

Instructions

Make a function that takes a column name as input, and returns the column sum.

Then use the function to take the sum of the "weather_delay" column, and divide it by the sum of the "arr_del15" column.

Assign the result to average_weather_delay_time.


In [41]:
def column_number_from_name(column_name):
    column_number = None
    for i, column in enumerate(column_names):
        if column == column_name:
            column_number = i
    return column_number

In [42]:
def sum_column(column_name):
    column_number = column_number_from_name(column_name)
    column = [float(_[column_number]) for _ in flight_delays]
    return sum(column)

weather_delay_sum = sum_column("weather_delay")
arr_del15_sum = sum_column("arr_del15")
average_weather_delay_time = weather_delay_sum/arr_del15_sum

print(average_weather_delay_time)


2.15461493239271

11: Named arguments to functions

Instructions

Fix the statements above so the code runs properly.

The first statement should divide 5 by 20, and the second should divide 100 by 30.


In [49]:
def divide(x=1, y=1):
    return x/y

# Use positional arguments, which will implicitly pass 10 to x and 5 to y.
print(divide(10,5))

# Use named arguments, which will pass the values to the named variable.
print(divide(y=10, x=5))

# If we use named arguments, the order doesn't matter
print(divide(x=5, y=10))

# But we can't have any positional arguments after we use a named argument
print(divide(5, 20))
print(divide(100, 30))


2.0
0.5
0.5
0.25
3.3333333333333335

Answer


In [50]:
print(divide(5, y=20))
print(divide(100, 30))


0.25
3.3333333333333335

12: Optional arguments to a function

Instructions

Fix the last two statements so that they work properly.

The first statement should multiply 4 3 1

The second statement should multiply 3 2 3


In [ ]:
def multiply(a, b=2, c=1):
    return a * b * c

# This will multiply 5 * 2 * 1
print(multiply(5))

# This will multiply 6 * 4 * 1
print(multiply(5, 4))

# This will multiply 5 * 2 * 1
print(multiply(a=5))

# This will multiply 6 * 2 * 4
print(multiply(a=6, c=4))

# Invalid, because we didn't fill the a variable, which doesn't have a default.
print(multiply(4, b=3))

# Invalid, because we didn't fill the a variable.
print(multiply(c=3))