for loops with range (slides)

CSc 110 - for loops with range

while vs. for loops

In addition to while, we can use for to create loops

index = 0
while index < 3:
  print(index)
  index += 1
0
1
2
for index in range(3):
  print(index)
0
1
2

while vs. for loops

In addition to while, we can use for to create loops

values = [70, 20, 30]
index = 0
while index < len(values):
  print(values[index])
  index += 1
70
20
30
values = [70, 20, 30]
for index in range(len(values)):
  print(values[index])
70
20
30

Write a function

It iterates over the list, changing odd numbers to even number (even up).

Use a for loop.

test_integers = [1, 2, 3, 4]
assert make_all_even(test_integers) == [2, 2, 4, 4]
assert test_integers == [2, 2, 4, 4] # change persists

Write a function – solution

def 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]
  assert test_integers == [2, 2, 4, 4]
  print(test_integers) 
  
main()
[2, 2, 4, 4]

Write a function

It returns a list of integers that represent the indices of the vowels in the string

Name file indices_of_vowels.py and submit to Gradescope.

Use a for loop.

Test cases:

assert indices_of_vowels("hello") == [1, 4]
assert indices_of_vowels("") == []
assert indices_of_vowels("aeiou") == [0, 1, 2, 3, 4]

Write a function – solution

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()

More on how to use range()

Syntax: range(start, stop, step)

  • start Optional. An integer specifying at which position to start. Default is 0
  • stop Required. An integer specifying at which position to stop (NOT included).
  • step Optional. An integer specifying the incrementation. Default is 1

More on how to use range()

for n in range(0, 5, 1): # step is 1, same as range(5)
  print(n)
0
1
2
3
4
for n in range(0, 5, 2): # step is 2
  print(n)
0
2
4
for n in range(5, 0, -2): # step is -2
  print(n)
5
3
1

Write a function

It returns a new string with items at even indices in characters concatenated together.

Use a for loop.

Test cases:

characters = ["a", "e", "p", "o", "p", "w", "l", "i", "e", "f"]
assert every_two_together(characters) == "apple"
assert every_two_together([]) == ""

Write a function – solution

def every_two_together(chars):
  new_string = ""
  for i in range(0, len(chars), 2):
    new_string += chars[i]
  return new_string

def main():
  characters = ["a", "e", "p", "o", "p", "w", "l", "i", "e", "f"]
  assert every_two_together(characters) == "apple"
  assert every_two_together([]) == ""
  
main()

Write a function

It returns a new list in reversed order, containing items at every other index in the original list.

Use a for loop.

Test cases:

characters = ["o", "e", "k", "l", "c", "p", "l", "p", "m", "a"]
assert every_two(characters) == ["a", "p", "p", "l", "e"]
assert every_two(["y", "e", "o", "e", "j"]) == ["j", "o", "y"]

Write a function – solution

def every_two(chars):
  result = []
  for i in range(len(chars)-1, -1, -2):
    result.append(chars[i])
  return result

def main():
  characters = ["o", "e", "k", "l", "c", "p", "l", "p", "m", "a"]
  assert every_two(characters) == ["a", "p", "p", "l", "e"]
  assert every_two(["y", "e", "o", "e", "j"]) == ["j", "o", "y"]
  print("pass the test case")
  
main()
pass the test case

Write a function

Test cases:

my_numbers = [0, 1, 0, 3, 0]
assert add_every_two(my_numbers) == 0

Write a function – solution

def add_every_two(numbers):
  result = 0
  for i in range(0, len(numbers), 2):
    result += numbers[i]
  return result

def main():
  my_numbers = [0, 1, 0, 3, 0]
  assert add_every_two(my_numbers) == 0
  
main()