# Noemi Cabrera
# 31 October 2021
# In this lesson, I learned to use the for..in loop with the range() function to perform
# different operations and tasks. The range() function can have 1, 2, or 3 arguments.
# If it has only 1 argument, it is the stop value and the start would be 0 by default.
# If it has 2 arguments, then those are the start and stop values of the sequence. If it
# has 3 arguments, those are the start value, stop value, and the step (skip) value.
# I didn't have any difficulties in this lesson.
# [ In this code, the range function is used with a for.. in loop. In this case, the range
# function takes the argument 10, which is the stop value of the sequence, and its
# default start is 0. The final sequence is from integers 0 to 9.] review and run example
for count in range(10):
print(count)
# In this code, the range function with argument 10 is casted to a list and printed. The
# for loop iterates throuugh each ite in the range function and prints them.
# review and run example
digits = range(10)
print("digits =", list(digits), "\n")
for count in digits:
print(count)
# [ In this code, a sub_total variable is created with a value of 0. This variable is
# later added with each item in the range 0 - 9. The different subtotals are printed. ]
# review and run example
sub_total = 0
for item in range(10):
sub_total += item
print("sub_total:", sub_total)
print("Total =", sub_total)
# [ In this code, the variable hal_1 contains the lenght of the spell_list divided by two.
# Then, this lenght is placed as the stop value in the range function, which prints the
# items in the spell_list up to the half-lenght value, which is 3. ]
# 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])
# [ In this code, each number of the range 0 - 5 is printed because the stop value 6 is
# not included. The argument is x since x cointains the stop value. Each item is printed.]
# for x = 6, use range(x) to print the numbers 1 through 5
x = 6
for number in range(x):
print(number)
# [ In this code, the for...in loop iterates through each item in the sequence of integers
# generated by the range() function (with a start value of 1, a stop value of 8). Then each item is multiplied to get a total value. ]
# using range(x) multiply the numbers 1 through 7
# 1x2x3x4x5x6x7= 5040
total = 1
for number in range(1,8):
total *= number
print(total)
# [ In this code, the second half of the spell list is printed. The range used in the
# for loop is from 0 to 7. Snnce the middle of spell_list is 3, the words that are less
# than the middle are not printed. However, teh words statrting from the middle until
# the end are printed using an if/else statement. half and ends at the last item in the
# last. Each word is written in a new line. ]
# 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"]
half_2 = int(len(spell_list)/2)
print("The second half starts at index",half_2)
for word in range(7):
if word < 3:
pass
else:
print(spell_list[word])
# [ In this for..in loop, the range specifies the start value is 5 and the stop value is 9
# (10 is not included). Therefore, the numbers printed are 5 - 9. ] review and run example
for count in range(5,10):
print(count)
# [ In this code, the range starting at 5 and ending at 10 is used. Each of the integers
# in range are added together continously to get a total value. In the for loop, the
# sub_total variable adds the items and the temp variavble contains the value of
# sub_total. ] 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)
# [ In this code, the second half of the spell_list is printed by using a for..in loop.
# The range() function has its start value at half of the spell list and a stop value of
# the actual lenght of the spell_list. ] 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])
# In this code, integers 5 - 15 are added to the five_fifteen list. The for loop iterates
# through the integer sequence defined by range(5,16). Each of the integers are appended
# to the five_fifteen list.
# [ ] using range(start,stop), .append() the numbers 5 to 15 to the list: five_fifteen
# [ ] print list five_fifteen
five_fifteen = []
for number in range(5,16):
five_fifteen.append(number)
print(five_fifteen)
# [ The for loop iterates through the integer sequence defined by range(2,5). So this is
# used to slice the spell_list and print the words that are located at index 2, 3, 4 ]
# 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])
# In this code, the items at indexes 4 - 7 of the spell_list are printed. First, I found
# the index of the item "Annual". Then, I used it in the range function to iterate and
# print the items starting at "Annual" until the last item in spell_list.
# [ ] 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"]
x = spell_list.index("Annual")
for word in range(x,7):
print(spell_list[word])
# [ In this code, the for...in loop iterates through the sequence of integers defined
# by reange() that starts at 25, skips by 25, until it reaches 100 (101 not included)]
# review and run example
for count in range(25,101,25):
print(count)
# [ In this code, the range starting at 25, skipping by 5, and ending at 5 is used. Each
# of the integers in range are added together continously to get a total value. In the
# for loop, thesub_total variable adds the items, and the temp variavble contains the
# value of sub_total. ] 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)
# [ The for loop uses the range starting at 0, skypping by 2, and ending at the last
# item of spell list to print each item of the last skipping by 2. ]
# 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])
# [The range starting at 1, skypping by 2, and ending at 19 is casted into a list and
# assigned to the variable odd_list. The list is printed. ]
# review and run example casting range to list
odd_list = list(range(1,20,2))
print(odd_list)
# [ In this code, each integer in the range starting at 10, skipping by 2, and ending at
# 20 is printed using a for..in loop. ] print numbers 10 to 20 by 2's using range
for integer in range(10,21,2):
print(integer)
# [ In this code, each integer in the range starting at 20 and ending at 10 is printed
# using a for..in loop. The step is -1 because it reverses the order. The list starts at
# 20 and ends at 10. ] print numbers 20 to 10 using range (need to countdown)
# Hint: start at 20
for integer in range(20,9,-1):
print(integer)
# [ Using the for..in loop, the words starting at index 0, skipping by 3, and ending at
# the last item of spell_list is printed. Each word is printed in a new line. ]
# print first and every third word in spell_list
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
for word in range(0, len(spell_list),3):
print(spell_list[word])
# In this program, the user is asked to enter a word. Then with the for..in loop and
# range() function the letters are sorted out into even and odd and placed into their
# list. The .append() method is used to place the letters in the lists created. The range
# for the odd letters starts at 0, skips by 2, and ends at the last letter of the word
# entered. The range for the even letters starts at 1, skips by 2, and ends at the last
# letter of the word entered.
# [ ] complete List of letters program- test with the word "complexity"
odd_letters = []
even_letters = []
word = input("Enter a word:")
w_length = len(word)
for odd in range(0, w_length, 2):
odd_letters.append(word[odd])
for even in range(1, w_length, 2):
even_letters.append(word[even])
print(odd_letters)
print(even_letters)
# [ The error in this code was that the range function had square brackets instead of
# parentheses. To fix this, I placed parentheses in the range function. ]
# fix the error printing odd numbers 1 - 9
for num in range(1,10,2):
print(num)