Iterating N Times
Iterating Through an Object
Iterating Until a Condition is Met
N
Times¶N
times, we can perform a for
loop using the range()
function:for number in range(10):
print(number)
0 1 2 3 4 5 6 7 8 9
print(0)
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
0 1 2 3 4 5 6 7 8 9
range()
function results in a sequence of the numbers 0 through 9 (a range of 10 values)list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for
loop block is then iterated 10 times, and the number
variable represents the loop iteration numberfor number in range(4):
print('\nThis is the next iteration:')
print(number)
This is the next iteration: 0 This is the next iteration: 1 This is the next iteration: 2 This is the next iteration: 3
What will be the value of number
when this code finishes running?
for number in range(10):
print(number)
Fill in the blanks below to make the code sum numbers 0 through 99.
summation = 0
for ______ in ______(______):
summation = summation + number
N
times...neighborhoods = [
'oakley', 'hyde park', 'clifton', 'corryville', 'northside'
]
for neighborhood in neighborhoods:
print(neighborhood)
oakley hyde park clifton corryville northside
neighborhoods = [
'oakley', 'hyde park', 'clifton', 'corryville', 'northside'
]
new_neighborhoods = []
for neighborhood in neighborhoods:
new_neighborhoods.append(neighborhood.title())
new_neighborhoods
['Oakley', 'Hyde Park', 'Clifton', 'Corryville', 'Northside']
enumerate()
¶enumerate()
functionneighborhoods = [
'oakley', 'hyde park', 'clifton', 'corryville', 'northside'
]
for index, value in enumerate(neighborhoods):
print(index, '-', value)
0 - oakley 1 - hyde park 2 - clifton 3 - corryville 4 - northside
neighborhoods = [
'oakley', 'hyde park', 'clifton', 'corryville', 'northside'
]
for index, value in enumerate(neighborhoods):
neighborhoods[index] = value.title()
neighborhoods
['Oakley', 'Hyde Park', 'Clifton', 'Corryville', 'Northside']
numbers = {'one': 1, 'two': 2, 'three': 3}
for i in numbers:
print(i)
one two three
enumerate
, you can access each key-value pair using .items()
:numbers = {'one': 1, 'two': 2, 'three': 3}
for key, value in numbers.items():
print(key, "-", value)
one - 1 two - 2 three - 3
import pandas as pd
flights_df = pd.read_csv('../data/flights.csv')
flights_df[['dep_delay', 'arr_delay', 'air_time']].head(3)
dep_delay | arr_delay | air_time | |
---|---|---|---|
0 | 2.0 | 11.0 | 227.0 |
1 | 4.0 | 20.0 | 227.0 |
2 | 2.0 | 33.0 | 160.0 |
pandas
subsetting and column syntax to manipulate and overwrite the variablestime_variables = ['dep_delay', 'arr_delay', 'air_time']
for variable in time_variables:
flights_df[variable] = flights_df[variable] / 60
flights_df[['dep_delay', 'arr_delay', 'air_time']].head(3)
dep_delay | arr_delay | air_time | |
---|---|---|---|
0 | 0.033333 | 0.183333 | 3.783333 |
1 | 0.066667 | 0.333333 | 3.783333 |
2 | 0.033333 | 0.550000 | 2.666667 |
for ______ in [0, 2, 4, 6, 8, 10]:
print(element ** ______)
import numpy as np
for column in flights_df.columns:
if flights_df[column].dtype != np.float64 and flights_df[column].dtype != np.int64:
flights_df[column] = flights_df[column].str.lower()
N
times or through/over objectsbreak
statement and the while
loopbreak
statement¶break
statement can be used to exit a loop at any timefor number in range(10):
if number == 5:
break
print(number)
0 1 2 3 4
for
loop breaks during the 5th iteration -- when number
is equal to 55
not be printedwhile
Loops¶break
can be included in complex loops with a lot of conditions, the while
loop can be used during simple tasksnumber = 1
while number < 5:
print(number)
number = number + 1
1 2 3 4
while
loops do require control of an iterator or conditional variableRewrite this for
loop to use a while
loop.
for number in range(15):
if number % 2 == 0:
print(number)
N
, and let's put it in a list:number_list_loop = []
for number in range(5):
number_list_loop.append(number)
number_list_comprehension = [number for number in range(5)]
number_list_loop == number_list_comprehension
True
neighborhoods = [
neighborhood.title()
for neighborhood
in ['oakley', 'hyde park', 'clifton', 'corryville', 'northside']
]
neighborhoods
['Oakley', 'Hyde Park', 'Clifton', 'Corryville', 'Northside']
for
loops# syntax of for loop
for i in sequence:
expression
# syntax for a list comprehension
[expression for i in sequence]
# syntax of for loop
for i in sequence:
if i == condition:
expression
# syntax for a list comprehension
[expression for i in sequence if i == condition]
neighborhoods = [
'oakley', 'hyde park', 'clifton', 'corryville', 'northside'
]
neighborhoods_loop = {}
for name in neighborhoods:
neighborhoods_loop[name] = name.title()
neighborhoods_loop
{'oakley': 'Oakley', 'hyde park': 'Hyde Park', 'clifton': 'Clifton', 'corryville': 'Corryville', 'northside': 'Northside'}
neighborhoods = [
'oakley', 'hyde park', 'clifton', 'corryville', 'northside'
]
neighborhoods_comprehension = {
name: name.title()
for name in neighborhoods
}
neighborhoods_comprehension
{'oakley': 'Oakley', 'hyde park': 'Hyde Park', 'clifton': 'Clifton', 'corryville': 'Corryville', 'northside': 'Northside'}
neighborhoods_loop == neighborhoods_comprehension
True
# syntax of for loop
for i in sequence:
expression
# syntax of dictionary comprehension
{key: expression for i in sequence}
Are there questions before moving on?