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

``````

``````

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

``````

``````

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

``````

``````

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

``````

``````

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

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

In [ ]:

``````