# Zhi Yuan "Oscar" Lin
# 29 October 2021
# In this lesson, it was mainly review of what was taught in the previous lessons. The following are some examples of what
# were reviewed: commenting, creating a list, slicing a list, using the list methods (".append()", ".insert()", ".reverse,"
# and ".pop"), defining and calling a function, initializing and using a variable, using relational operators, using a while
# loop, using the "return" and "del" keywords, and using built-in functions ("print()" and "input()").
# There were no major problems or questions throughout this lesson.
# [ ] create and populate list called days_of_week then print it
# The variable "days_of_week" is assigned to a list of strings.
days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# [ ] after days_of_week is run above, print the days in the list at odd indexes 1,3,5
# This function only displays the 2nd element of the value assigned to the days_of_week variable along with the characters
# that are every two places after the 2nd element until the last element, in the output. In this case, the 4th and 6th
# element will also be display in the output.
print(days_of_week[1:7:2])
# [ ] create and populate list called phone_letters then print it
# The variable "phone_letters" is assigned to a list of strings.
phone_letters = [' ', "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"]
# This function displays the string assigned phone_letters variable in the output.
print(phone_letters)
# [ ] create a variable: day, assign day to "Tuesday" using days_of_week[]
# [ ] print day variable
# The variable "day" is assigned to the element that is within the index position 1 in the value assigned to the
# days_of_week variable.
day = days_of_week[2]
# This function displays the value assigned to the day variable in the output.
print(day)
# PART 2
# [ ] assign day to days_of_week index = 5
# [ ] print day
# The variable "day" is assigned to the element that is within the index position 5 in the value assigned to the
# days_of_week variable.
day = days_of_week[5]
# This function displays the value assigned to the day variable in the output.
print(day)
# [ ] Make up a new day! - append an 8th day of the week to days_of_week
# [ ] print days_of_week
# This line of code adds the string "Doomsday" to the end of the value assigned to the days_of_week variable due to the
# list method ".append()".
days_of_week.append("Doomsday")
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# [ ] Make up another new day - insert a new day into the middle of days_of_week between Wed - Thurs
# [ ] print days_of_week
# The variable "day" is assigned to the string "Pancakeday".
day = "Pancakeday"
# This line of code inserts the value assigned to the day variable into the index positon 3 of the value assigned to the
# days_of_week variable.
days_of_week.insert(4, day)
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# [ ] Extend the weekend - insert a day between Fri & Sat in the days_of_week list
# [ ] print days_of_week
# The variable "day" is assigned to the string "Syrupday".
day = "Syrupday"
# This line of code inserts the value assigned to the day variable into the index positon of the value assigned to the
# days_of_week variable.
days_of_week.insert(7, day)
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# [ ] print days_of_week
# [ ] modified week is too long - pop() the last index of days_of_week & print .pop() value
# [ ] print days_of_week
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# This function displays the element that was removed from the value assigned to the days_of_week variable due to the
# list method ".pop()". In this case, the the value in the last index position in the value assigned to the party_list
# is removed due to the list method".pop()" not having an index specified.
print(days_of_week.pop())
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# [ ] print days_of_week
# [ ] delete (del) the new day added to the middle of the week
# [ ] print days_of_week
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# This line of code deletes the element that is within the index position 4 in the value assigned to the days_of_week
# variable.
del days_of_week[4]
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# [ ] print days_of_week
# [ ] programmers choice - pop() any day in days_of week & print .pop() value
# [ ] print days_of_week
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# This function displays the value assigned to the days_of_week variable after the element in the index position 1 in the
# value assigned to the days_of_week is removed due to the list method ".pop()".
print(days_of_week.pop(1))
# This function displays the value assigned to the days_of_week variable in the output.
print(days_of_week)
# [ ] create let_to_num()
# This program defines a function named "let_to_num" with the variable "letter", that will be assign to the entry the user
# inputs when presented with the string "Single letter, space or empty string: ". Additionally, the variable "key" is
# assigned to the integer "0". The function also contains a while loop. In this case, the while loop is follow by the
# statement "key < 10". Hence, it cannot evaluate anything but true. Additionally, the codes that are indented and follow
# the while loop will be part of the loop. Thus, the loop starts with determining if the value assigned to the letter
# variable, after being changed into uppercases due to the letter variable being attached to the string method ".upper()",
# is in the value, that is the index position given from value assigned to the key variable, assigned to the phone_letters
# variable. If true, the value assigned to the key variable will be return by the return keyword and the loop will end. If
# false, the program moves on to the else statement with the key variable being reassigned to the sum of the value assigned
# to the key variable incremented by 1. If no value is return by the return keyword, this loop will continue until the
# statement "key < 10" is false. When the loop ends, the string "Not Found" will be return by the return keyword.
def let_to_num():
letter = input("Single letter, space or empty string: ")
key = 0
while key < 10:
if letter.upper() in phone_letters[key]:
return key
else:
key = key + 1
return "Not Found"
# This function displays the returned value when calling the let_to_num() function, in the output.
print(let_to_num())
# [ ] Challenge: write the code for "reverse a string from a list"
# The variable "str" is assigned to a list of strings.
str = ["French Toast", "Pancakes", "Waffles", "Bagel", "Muffin"]
# The variable "rev_str" is assigned to an empty list.
rev_str = []
# This function displays the string "List before:" and the value assigned to the str variable in the output.
print("List before:", str)
# In this case, the while loop is follow by the variable "str". Hence, as long as a value is assigned to the str variable,
# it will evaluate true. Additionally, the codes that are indented and follow the while loop will be part of the loop.
# Thus, the loop starts with the substring, that is in the index position 0 of the value assigned to the str variable, being
# removed and assigned to the rev variable, due to the list method ".pop()". Subsequently, the value assigned to the rev
# variable will be inserted into the index positon 0 of the value assigned to the rev_str variable, due to the list method
# ".insert()". This loop will continue until it evaluates false.
while str:
rev = str.pop(0)
rev_str.insert(0,rev)
# This function displays the string "List after:" and the value assigned to the str variable in the output.
print("List after:", str)
# This function displays the string "Reversed list:" and the value assigned to the rev_str variable in the output.
print("Reversed list:", rev_str)