for
loop using in
for range(start,stop,step)
.extend()
, +, .reverse(), .sort()
.split()
, and list to strings, .join()
> Student will be able to
for
with in
for range()
in looping operations .extend()
, +, .reverse(), .sort()
.split()
and .join()
# [ ] review and run example
for count in range(10):
print(count)
# review and run example
digits = range(10)
print("digits =", list(digits), "\n")
for count in digits:
print(count)
# [ ] review and run example
sub_total = 0
for item in range(10):
sub_total += item
print("sub_total:", sub_total)
print("Total =", sub_total)
# [ ] 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])
# [ ] for x = 6, use range(x) to print the numbers 1 through 6
x = 6
#Answer:
for count in range(x):
print(count+1)
# [ ] using range(x) multiply the numbers 1 through 7
# 1x2x3x4x5x6x7 = 5040
#Answer:
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)
Use range(stop)
to print the second half of spell_list below
# [ ] 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"]
#Answer:
# 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])
# [ ] review and run example
for count in range(5,10):
print(count)
# [ ] 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)
# [ ] 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])
# [ ] using range(start,stop), .append() the numbers 5 to 15 to the list: five_fifteen
# [ ] print list five_fifteen
#Answer:
five_fifteen = range(5,16)
for count in five_fifteen:
print(count)
# [ ] 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"]
#Answer:
for word in range(2,5):
print(spell_list[word])
# [ ] 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"]
#Answer:
#index=0
index = spell_list.index('Annual')
for word in range(0,index+1):
print(spell_list[word])
Concepts
range(start,stop,step)
¶using 3 arguments with range(start,stop,step)
for count in range(10,101,10):
print(count)
# [ ] review and run example
for count in range(25,101,25):
print(count)
# [ ] 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)
# [ ] 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])
# [ ] review and run example casting range to list
odd_list = list(range(1,20,2))
print(odd_list)
# [ ] print numbers 10 to 20 by 2's using range
#Answer:
even_list = list(range(10,21,2))
print(even_list)
# [ ] print numbers 20 to 10 using range (need to countdown)
# Hint: start at 20
#Answer:
even_reverse_list = list(range(20,9,-2))
print(even_reverse_list)
# [ ] print first and every third word in spell_list
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
#Answer:
for index in range(0,len(spell_list),2):
print(spell_list[index])
Task 4
# [ ] complete List of letters program- test with the word "complexity"
#Answer:
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)
Task 5: fix the error
# [ ] fix the error printing odd numbers 1 - 9
#for num in range[1,10,2]:
# print(num)
#Answer:
for num in range(1,10,2):
print(num)
Terms of use Privacy & cookies © 2017 Microsoft