70
20
30
while vs. for loopsIn addition to while, we can use for to create loops
make_all_evenintegersfor loopdef make_all_even(integers):
# for each index in list
for index in range(len(integers)):
integers[index] += integers[index] % 2 # add zero if even, one if odd
return integers
def main():
test_integers = [1, 2, 3, 4]
assert make_all_even(test_integers) == [2, 2, 4, 4]
print(test_integers) # we print the list we created before function call
main()[2, 2, 4, 4]
indices_of_vowelsstring as its parameter.for loopTest cases:
def indices_of_vowels(string):
result = [] # initialize empty list to hold indices
# for every index in list
for index in range(len(string)):
if string[index] in "aeiou": # check if character is vowel
result.append(index) # append index to result
return result
def main():
assert indices_of_vowels("hello") == [1, 4]
assert indices_of_vowels("") == []
assert indices_of_vowels("aeiou") == [0, 1, 2, 3, 4]
main()Submit your indices_of_vowels functions to Gradescope for attendance.
Name your file indices_of_vowels.py
range()Syntax: range(start, stop, step)
start Optional. An integer number specifying at which position to start. Default is 0stop Required. An integer number specifying at which position to stop (not included).step Optional. An integer number specifying the incrementation. Default is 1range()every_two_togethercharacterscharacters concatenated togetherfor loopTest cases:
What other test cases should we run?
Test cases: