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
In [2]:
while b > 5:
print(b)
b -= 1
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
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])
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)
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])
In [14]:
third_to_last = flight_delays[-3]
end_slice = flight_delays[-4:-1]
print(third_to_last, end_slice)
In [ ]: