# The Power of List Iteration (loops)¶

• for in: for loop using in
• for range: for range(start,stop,step)
• more list methods: .extend(), +, .reverse(), .sort()
• strings to lists, .split(), and list to strings, .join()

> Student will be able to

• Iterate through Lists using for with in
• Use for range() in looping operations
• Use list methods .extend(), +, .reverse(), .sort()
• convert between lists and strings using .split() and .join()

# ¶

Concepts

## range(stop)¶ ### The range(stop) function creates a sequence¶

using 1 argument with range(stop)

• deault start: 0
• stop: stopping integer, does not process stop number
for count in range(10):
print(count)

### same as
for count in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
print(count)


# ¶

Examples

### range runs from 0 through the integer before stop¶

In :
# [ ] review and run example
for count in range(10):
print(count)

0
1
2
3
4
5
6
7
8
9

In :
# review and run example
digits = range(10)
print("digits =", list(digits), "\n")

for count in digits:
print(count)

digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

0
1
2
3
4
5
6
7
8
9

In :
# [ ] review and run example
sub_total = 0
for item in range(10):
sub_total += item
print("sub_total:", sub_total)
print("Total =", sub_total)

sub_total: 0
sub_total: 1
sub_total: 3
sub_total: 6
sub_total: 10
sub_total: 15
sub_total: 21
sub_total: 28
sub_total: 36
sub_total: 45
Total = 45

In :
# [ ] review and run example
# print the first half of a spelling list
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]

# find length of 1st half of list (must be int)
half_1 = int(len(spell_list)/2)

for word in range(half_1):
print(spell_list[word])

Tuesday
Wednesday
February


# ¶

## range(stop)¶

In :
# [ ] for x = 6, use range(x) to print the numbers 1 through 6
x = 6

for count in range(x):
print(count+1)

1
2
3
4
5
6

In :
# [ ] using range(x) multiply the numbers 1 through 7
# 1x2x3x4x5x6x7 = 5040

x=7
multiply_count=1
multiply_string = ""
for count in range(x):
multiply_string += str(count+1)
multiply_count *= (count+1)
if(count < x-1):
multiply_string += ' x '
print(multiply_string," = ",multiply_count)

1 x 2 x 3 x 4 x 5 x 6 x 7  =  5040


Use range(stop) to print the second half of spell_list below

In :
# [ ] print the second half of a spelling list using a range(stop) loop to iterate the list
spell_list = ["Wednesday", "Tuesday", "February", "November", "Annual", "Calendar", "Solstice"]

# find length of 1st half of list (must be int)
half_1 = int(len(spell_list)/2)

for word in range(half_1):
print(spell_list[word])

Wednesday
Tuesday
February


# ¶

Concepts

## range(start,stop)¶ ### The range(start,stop) function creates a sequence¶

using 2 arguments with range(start,stop)

• start: starting integer value of a range loop
• stop: stopping integer (second argument), does not process stop number
for count in range(5,10):
print(count)


Examples

### range runs from start integer through the integer before stop¶

In :
# [ ] review and run example
for count in range(5,10):
print(count)

5
6
7
8
9

In :
# [ ] review and run example
sub_total = 0
temp = 0
for item in range(5, 11):
temp = sub_total
sub_total += item
print("sub_total:", temp, "+", item, "=",sub_total)
print("Total =", sub_total)

sub_total: 0 + 5 = 5
sub_total: 5 + 6 = 11
sub_total: 11 + 7 = 18
sub_total: 18 + 8 = 26
sub_total: 26 + 9 = 35
sub_total: 35 + 10 = 45
Total = 45

In :
# [ ] review and run example

spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]

# find length list
spell_len = len(spell_list)
# find lenght of 1st half (aka - start of 2nd half)
half_1 = int(spell_len/2)

# print 2nd half list
for word in range(half_1,spell_len):
print(spell_list[word])

November
Annual
Calendar
Solstice


# ¶

## range(start,stop)¶

In :
# [ ] using range(start,stop), .append() the numbers 5 to 15 to the list: five_fifteen
# [ ] print list five_fifteen

five_fifteen = range(5,16)
for count in five_fifteen:
print(count)

5
6
7
8
9
10
11
12
13
14
15

In :
# [ ] using range(start,stop) - print the 3rd, 4th and 5th words in spell_list
# output should include "February", "November", "Annual"
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]

for word in range(2,5):
print(spell_list[word])

February
November
Annual

In :
# [ ] using code find the index of "Annual" in spell_list
# [ ] using range, print the spell_list including "Annual" to end of list
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]

#index=0
index = spell_list.index('Annual')
for word in range(0,index+1):
print(spell_list[word])

Tuesday
Wednesday
February
November
Annual


# ¶

Concepts

## range(start,stop,step)¶ ### The range(start,stop,step) function creates a sequence¶

using 3 arguments with range(start,stop,step)

• start: starting integer value of a range loop
• stop: stopping integer (second argument), does not process stop number
• step: skip value for each loop
for count in range(10,101,10):
print(count)


# ¶

Examples

### range runs from start integer, skipping by step, through the largest step integer before reaching stop¶

In :
# [ ] review and run example
for count in range(25,101,25):
print(count)

25
50
75
100

In :
# [ ] review and run example
sub_total = 0
temp = 0
for item in range(25,46,5):
temp = sub_total
sub_total += item
print("sub_total:", temp, "+", item, "=",sub_total)
print("Total =", sub_total)

sub_total: 0 + 25 = 25
sub_total: 25 + 30 = 55
sub_total: 55 + 35 = 90
sub_total: 90 + 40 = 130
sub_total: 130 + 45 = 175
Total = 175

In :
# [ ] review and run example printing the 1st and then every other word in spell_list
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]

for index in range(0,len(spell_list),2):
print(spell_list[index])

Tuesday
February
Annual
Solstice

In :
# [ ] review and run example casting range to list
odd_list = list(range(1,20,2))
print(odd_list)

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]


# ¶

## range(start,stop,step)¶

In :
# [ ] print numbers 10 to 20 by 2's using range

even_list = list(range(10,21,2))
print(even_list)

[10, 12, 14, 16, 18, 20]

In :
# [ ] print numbers 20 to 10 using range (need to countdown)
# Hint: start at 20

even_reverse_list = list(range(20,9,-2))
print(even_reverse_list)

[20, 18, 16, 14, 12, 10]

In :
# [ ] print first and every third word in spell_list
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]

for index in range(0,len(spell_list),2):
print(spell_list[index])

Tuesday
February
Annual
Solstice


# ¶

### Program: List of letters¶

• Input a word string (word)
• find the string length of word
• use range() to iterate through each letter in word (can use to range loops)
• Save odd and even letters from the word as lists
• odd_letters: starting at index 0,2,...
• even_letters: starting at index 1,3,...
• print odd and even lists
In :
# [ ] complete List of letters program- test with the word "complexity"

word = input('Type input string : ')
word_length = len(word)
odd_letters = []
even_letters= []

for index in range(0,word_length):
if (index%2 ==0):
even_letters.append(word[index])
else:
odd_letters.append(word[index])
print('even_letters = ',even_letters)
print('odd_letters = ',odd_letters)

Type input string : complexity
even_letters =  ['c', 'm', 'l', 'x', 't']
odd_letters =  ['o', 'p', 'e', 'i', 'y']


# ¶

In :
# [ ] fix the error printing odd numbers 1 - 9
#for num in range[1,10,2]:
#    print(num)


1