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 [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:]
In [18]:
first_ten_rows = flight_delays[:10]
last_ten_rows = flight_delays[-10:]
In [17]:
print(first_ten_rows, last_ten_rows)
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)
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
In [37]:
print(delayed_percentage)
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
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)
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)
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))
In [50]:
print(divide(5, y=20))
print(divide(100, 30))
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))