# Zhi Yuan "Oscar" Lin
# 26 October 2021
# In this lesson, I learned more concerning the use of the range() function through examples and tasks. Additionally,
# I learned how to use the range() function with the for...in loop. Furthermore, through trials and errors, I learned how
# important it is to remember the format for the start, stop, and step value when using the range() function.
# There were no major problems or questions throughout this lesson.
# [ ] review and run example
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value of 10, and a step value of 1), is iterated and displayed on separate lines in the output one at a time. This loop
# will continue until each element generated by the range() function is iterated.
for count in range(10):
print(count)
# review and run example
# The variable "digits" is assigned to the sequence of integers, 1-9, generated by the range() function.
digits = range(10)
# This function displays the string "digits =" and the value assigned to the digits variable as a list, by using the list()
# function, in the output. Additionally, a blank line will be displayed on the next line of the output due to the escape
# character "\n".
print("digits =", list(digits), "\n")
# By using the for...in loop, each integer in the value assigned to the digits variable is iterated and display on different
# lines in the output one at a time. This loop will continue until each element in the value assigned to the digit variable
# is iterated.
for count in digits:
print(count)
# [ ] review and run example
# The variable "sub_total" is assigned to the integer "0".
sub_total = 0
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, stop value
# of 10, and step value of 1), is iterated and added to the value assigned to the sub_total variable. Subsequently, that
# sum will be reassign to the sub_total variable. Additionally, the string "sub_total:" and the value assigned to the
# sub_total variable will be display in the output. This loop will continue until each element in the value assigned to
# the item variable is iterated.
for item in range(10):
sub_total += item
print("sub_total:", sub_total)
# This function displays the string "Total =" and the value assigned to the sub_total variable.
print("Total =", sub_total)
# [ ] review and run example
# print the first half of a spelling list
# The variable "spell_list" is assigned to a list of strings.
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
# find length of 1st half of list (must be int)
# The variable "half_1" is assigned to the value of the given value, when the string method "len()" determines the length
# of the value assigned to the spell_list variable, being divided by the integer "2", and converted into an integer due
# them being within the parentheses of the int() function.
half_1 = int(len(spell_list)/2)
# By using the for...in loop, a sequence of integers generated by the range() function with the arugment of the half_1
# variable (with a start value of 0, a stop value of 3, and a step value of 1), is iterated. Subsequently, the value
# assigned to the word variable will serve as the index position for the spell_list variable, and be display the value in
# that index position in the value assigned to the spell_list variable in the output. This loop will continue until each
# element generated by the range() function is iterated.
for word in range(half_1):
print(spell_list[word])
# [ ] for x = 6, use range(x) to print the numbers 1 through 6
# The variable "x" is assigned to the integer "6".
x = 6
# By using the for...in loop, a sequence of integers generated by the range() function with the arugment of the x variable
# (with a start value of 0, a stop value of 6, and a step value of 1), is iterated and added to the integer "1".
# Subsequently, that sum will be assigned to the count variable. Additionally, the value assigned to the count variable
# will be display in the output. This loop will continue until each element generated by the range() function is iterated.
for count in range(x):
count += 1
print(count)
# [ ] using range(x) multiply the numbers 1 through 7
# 1x2x3x4x5x6x7 = 5040
# The variable "t" is assigned to the integer 1.
t = 1
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 1, a stop
# value of 8, and a step value of 1), is iterated and multiplied to the value assigned to the t variable. Subsequently, that
# product will be reassign to the t variable. This loop will continue until each element generated by the range() function
# is iterated.
for count in range(1,8):
t = t * count
# This function displays the value assigned to the t variable in the output.
print(t)
# [ ] print the second half of a spelling list using a range(stop) loop to iterate the list
# The variable "spell_list" is assigned to a list of strings.
spell_list = ["Wednesday", "Tuesday", "February", "November", "Annual", "Calendar", "Solstice"]
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value of 7, and a step value of 1), is iterated and determined by an IF statement. The IF statement determines if the
# value assigned word variable is less than the integer "3". If true, nothing will be execute due to the keyword "pass".
# If false, the program moves on to the else statement with the value assigned to the word variable serving as the index
# position for the spell_list variable. Consequently, the value in that index position in the value assigned to the
# spell_list variable will be display in the output. This loop will continue until each element generated by the range()
# function is iterated.
for word in range(7):
if word < 3:
pass
else:
print(spell_list[word])
# [ ] review and run example
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 5, a stop
# value of 10, and a step value of 1), is iterated and displayed on separate lines in the output one at a time. This loop
# will continue until each element generated by the range() function is iterated.
for count in range(5,10):
print(count)
# [ ] review and run example
# The variable "sub_total" is assigned to the integer "0".
sub_total = 0
# The variable "temp" is assigned to the integer "0".
temp = 0
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 5, a stop
# value of 11, and a step value of 1), is iterated. Subsequently, the value assigned to the sub_total variable is assign
# to the temp variable. Following that, the sub_total variable is reassigned to the value assigned to the sub_total
# variable added to the value assigned to the item variable. Additionally, the string "sub_total", the value assigned to
# the temp variable, the string "=" the value assigned to the item variable, the string "=", and the variable assigned to
# the sub_total variable, will be display in the output. This loop will continue until each element generated by the range()
# function is iterated.
for item in range(5, 11):
temp = sub_total
sub_total += item
print("sub_total:", temp, "+", item, "=",sub_total)
# This function displays the string "Total =" and the value assigned to the sub_total variable in the output.
print("Total =", sub_total)
# [ ] review and run example
# The varaible "spell_list" is assigned to a list of strings.
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
# find length list
# The variable "spell_len" is assigned to the given value when determining the length of the value assigned to the
# spell_list by using the len() function.
spell_len = len(spell_list)
# find lenght of 1st half (aka - start of 2nd half)
# The variable "half_1" is assigned to the quotient, which is from the value assigned to the spell_len variable divided by the
# integer "2", after being converted into an integer due to it being within the parentheses of the int() function.
half_1 = int(spell_len/2)
# print 2nd half list
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value being the value
# assigned to the half_1 variable, a stop value being the value assigned to the spell_len variable, and a step value of 1),
# is iterated. Subsequently, the value assigned to the word variable will serve as the index position for the spell_list
# variable, and displayed the value in that index position in the value assigned to the spell_list variable in the output.
# This loop will continue until each element generated by the range() function is iterated.
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
# The variable "five_fifteen" is assigned to an empty string.
five_fifteen = []
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 5, a stop
# value of 16, and a step value of 1), is iterated. Subsequently, the value assigned to the count variable will be add
# to the end of the value assigned to the five_fifteen variable due to the list method ".append()". This loop will continue
# until each element generated by the range() function is iterated.
for count in range(5,16):
five_fifteen.append(count)
# This function displays the value assigned to the five_fifteen variable in the output.
print(five_fifteen)
# [ ] using range(start,stop) - print the 3rd, 4th and 5th words in spell_list
# output should include "February", "November", "Annual"
# The variable "spell_list" is assigned to a list of strings.
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 2, a stop
# value of 5, and a step value of 1), is iterated. Subsequently, the value assigned to the word variable will serve as the
# index position for the spell_list variable, and displayed the value in that index position in the value assigned to the
# spell_list variable in the output. This loop will continue until each element generated by the range() function is
# iterated.
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
# The variable "spell_list" is assigned to a list of strings.
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
# The variable "x" is assigned to the given value when determining the index position of the string "Annual" in the value
# assigned to the spell_list variable, by using the list method ".index()".
x = spell_list.index("Annual")
# The variable "y" is assigned to the value the list method ".pop()" removed from the value assigned to the spell_list
# variable, when given the index position of the value assigned to the x variable.
y = spell_list.pop(x)
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value of 7, and a step value of 1), is iterated. Subsequently, the value assigned to the y variable will be add to the
# value assigned to the spell_list vairbale. Additionally, the value assigned to the word variable will serve as the
# index position for the spell_list variable, and display the value in that index position in the value assigned to the
# spell_list variable in the output. This loop will continue until each element generated by the range() function is
# iterated.
for word in range(0,7):
spell_list.append(y)
print(spell_list[word])
# [ ] review and run example
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 25, a stop
# value of 101, and a step value of 25), is iterated and displayed on separate lines in the output one at a time. This loop
# will continue until each element generated by the range() function is iterated.
for count in range(25,101,25):
print(count)
# [ ] review and run example
# The variable "sub_total" is assigned to the integer "0".
sub_total = 0
# The variable "temp" is assigned to the integer "0".
temp = 0
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 25, a stop
# value of 46, and a step value of 5), is iterated. Subsequently, the value assigned to the sub_total variable is assign
# to the temp variable. Following that, the sub_total variable is reassigned to the value assigned to the sub_total
# variable added to the value assigned to the item variable. Additionally, the string "sub_total", the value assigned to
# the temp variable, the string "=" the value assigned to the item variable, the string "=", and the variable assigned to
# the sub_total variable, will be display in the output. This loop will continue until each element generated by the range()
# function is iterated.
for item in range(25,46,5):
temp = sub_total
sub_total += item
print("sub_total:", temp, "+", item, "=",sub_total)
# This function displays the string "Total =" and the value assigned to the sub_total variable in the output.
print("Total =", sub_total)
# [ ] review and run example printing the 1st and then every other word in spell_list
# The variable "spell_list" is assigned to a list of strings.
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value being the given value when determining the length of the value assigned to the spell_list variable by using the
# len() function, and a step value of 2), is iterated. Subsequently, the value assigned to the index variable will serve as
# the index position for the spell_list variable, and display the value in that index position in the value assigned to
# the spell_list variable in the output. This loop will continue until each element generated by the range() function is
# iterated.
for index in range(0,len(spell_list),2):
print(spell_list[index])
# [ ] review and run example casting range to list
# The variable "odd_list" is assigned to the sequence of integers generated by the range() function (with a start value of
# 1, stop value of 20, and step value of 2), after being changed into a list by the list() function.
odd_list = list(range(1,20,2))
# This function displays the value assigned to the odd_list variable in the output.
print(odd_list)
# [ ] print numbers 10 to 20 by 2's using range
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 10, a stop
# value of 21, and a step value of 2), is iterated and displayed on separate lines in the output one at a time. This loop
# will continue until each element generated by the range() function is iterated.
for count in range(10,21,2):
print(count)
# [ ] print numbers 20 to 10 using range (need to countdown)
# Hint: start at 20
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 20, a stop
# value of 9, and a step value of -1), is iterated and displayed on separate lines in the output one at a time. This loop
# will continue until each element generated by the range() function is iterated.
for count in range(20,9,-1):
print(count)
# [ ] print first and every third word in spell_list
# The variable "spell_list" is assigned to a list of strings.
spell_list = ["Tuesday", "Wednesday", "February", "November", "Annual", "Calendar", "Solstice"]
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value of 7, and a step value of 3), is iterated. Subsequently, the value assigned to the word variable will serve as the
# index position for the spell_list variable, and display the value in that index position in the value assigned to the
# spell_list variable in the output. This loop will continue until each element generated by the range() function is
# iterated.
for word in range(0,7,3):
print(spell_list[word])
# [ ] complete List of letters program- test with the word "complexity"
# The variable "word" will be assign to the entry the user inputs when presented with the string "Enter a word: ".
word = input("Enter a word: ")
# The variable "odd_letters" is assigned to an empty list.
odd_letters = []
# The variable "even_letters" is assigned to an empty list.
even_letters = []
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value being the given value when determining the length of the value assigned to the word variable by using the len()
# function, and a step value of 2), is iterated. Subsequently, the value assigned to the odd variable will serve as the
# index position for the word variable, and be added to the value assigned to the odd_letters variable due to the list
# method ".append()"". This loop will continue until each element generated by the range() function is iterated.
for odd in range(0, len(word), 2):
odd_letters.append(word[odd])
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 1, a stop
# value being the given value when determining the length of the value assigned to the word variable by using the len()
# function, and a step value of 2), is iterated. Subsequently, the value assigned to the even variable will serve as the
# index position for the word variable, and be added to the value assigned to the even_letters variable due to the list
# method ".append()"". This loop will continue until each element generated by the range() function is iterated.
for even in range(1, len(word), 2):
even_letters.append(word[even])
# This function displays the value assigned to the odd_letters variable in the output.
print(odd_letters)
# This function displays the value assigned to the even_letters variable in the output.
print(even_letters)
# [ ] fix the error printing odd numbers 1 - 9
# This function originally contained an TypeError due to the values connected to the range() function are within square
# brackets instead of parentheses. This issue is fixed by changing the square brackets into parentheses.
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 1, a stop
# value of 10, and a step value of 2), is iterated and displayed on separate lines in the output one at a time. This loop
# will continue until each element generated by the range() function is iterated.
for num in range(1,10,2):
print(num)