# Zhi Yuan "Oscar" Lin
# 23 October 2021
# In this lesson, I learned how to modify and insert element(s) in a list through examples and tasks. Additionally, I
# learned more concerning the mistakes a programmer should look out for when using a list(s). For example, I learned a
# new error type called, "AttributeError".
# There were no major problems or questions throughout this lesson.
# [ ] review and run example
# the list before Insert
# The variable "party_list" is assigned to a list of strings.
party_list = ["Joana", "Alton", "Tobias"]
# This function displays the string "party_list before: " and the value assigned to the party_list variable in the output.
print("party_list before: ", party_list)
# the list after Insert
# This line of code replaces the valaue in the index position 1, that is in the value assigned to the party_list variable,
# with the string "Colette".
party_list[1] = "Colette"
# This function displays the string "party_list after: " and the value assigned to the party_list in the output.
print("party_list after: ", party_list)
# [ ] review and run example
# The variable "party_list" is assigned to a list of strings.
party_list = ["Joana", "Alton", "Tobias"]
# This function displays the string "before:" and the value assigned to party_list variable in the output.
print("before:",party_list)
# modify index value
# This line of code replaces the value in the index position 1, that is in the value assigned to the party_list variable,
# with the concatenated string formed from the value in the index position 1 and the string " Derosa".
party_list[1] = party_list[1] + " Derosa"
# This function displays the stirng "after:" and the value assigned to the party_list variable in the output. Additionally,
# they are displayed on the next line due to the escape character "/n".
print("\nafter:", party_list)
# IndexError Example
# [ ] review and run example which results in an IndexError
# if result is NameError run cell above before running this cell
# IndexError trying to append to end of list
# This examples provides an example of what an IndexError is. This example contains an IndexError due to the line of code
# written to replace the value in the index position 3, that is in the in the value assigned to the party_list variable,
# when there is no value in the index position 3.
# This line of code replaces the value in the index position 3, that is in the value assigned to the party_list variable,
# with the string "Alton".
party_list[3] = "Alton"
# This function displays the value assigned to the party_list variable in the output.
print(party_list)
# [ ] review and run example changes the data type of an element
# replace a string with a number (int)
# The variable "single_digits" is assigned to a list of strings.
single_digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
# This function displays the string "single_digits: " and the value assigned to the single_digits variable in the output.
print("single_digits: ", single_digits)
# This function displays the string "single_digits[3]", the value that is in the index position 3 of the value assigned
# to the single_digits variable, and the type of the value that is in the index position 3 of the value assigned to the
# single_digits variable, in the output. Additionally, a blank line will be displayed on the next line of the output due
# to the escape character "\n".
print("single_digits[3]: ", single_digits[3], type(single_digits[3]),"\n")
# replace string with an int
# This line of code replaces the value in the index position 3, that is in the value assigned to the single_digits variable,
# with the integer "3".
single_digits[3] = 3
# This function displays the string "single_digits: " and the value assigned to the single_digits variable in the output.
print("single_digits: ", single_digits)
# This function displays the string "single_digits[3]", the value that is in the index position 3 of the value assigned
# to the single_digits variable, and the type of the value that is in the index position 3 of the value assigned to the
# single_digits variable, in the output.
print("single_digits[3]: ", single_digits[3], type(single_digits[3]))
# [ ] complete "replace items in a list" task
# The variable "three_num" is assigned to a list of integers.
three_num = [2, 4, 6]
# This function displays the value assigned to the three_num variable in the output.
print(three_num)
# This IF statement determines if the value in the index position 0, that is in the value assigned to the three_num
# variable, is less than, <, the integer "5". If true, the value in index position 0 will be replaced by the string
# small. If false, the program moves on to the else statement with the string "large" replacing the value in the
# index position 3.
if three_num[0] < 5:
three_num[0] = "small"
else:
three_num[0] = "large"
# This function displays the value assigned to the three_num variable in the output.
print(three_num)
# [ ] create challenge function
# The variable "int_list" is assigned to a list of integers.
int_list = [2, 4, 8]
# This program defines a function named "str_replace", that has the parameters "int_list" and "index", with an IF statement.
# The if statement determines if the value in the index position, which depends on the integer assigned to the index
# parameter, in the value assigned to the int_list variable is less than, <, the integer "5". If true, the value in that
# index position in the value assigned to the int_list variable will be replace by the string "small". If false, the
# program moves on to the else statement and replaces the value in that index position in the value assigned to the int_list
# variable will be replace by the string "large". Subsequently, the value assigned to int_list variable will be return by
# the keyword "return".
def str_replace(int_list, index):
if int_list[index] < 5:
int_list[index] = "small"
else:
int_list[index] = "large"
return int_list
# This line of code calls the str_replace() function with the arguments "int_list", and "0". Due to their placement, the
# int_list variable is assigned to the int_list parameter parameter while the integer "0" is assigned to the index parameter.
str_replace(int_list, 0)
# [ ] complete coding task described above
# The variable "three_words" is assigned to a list of strings.
three_words = ["Apple", "Orange", "Lemon"]
# This function displays the value assigned to the three_words variable in the output.
print(three_words)
# This line of code replaces the value in the index position 0, in the value assigned to the three_words variable,
# with value in the index position 0 being changed into uppercases due to it being attached to the string method
# ".upper()".
three_words[0] = three_words[0].upper()
# This line of code replaces the value in the index position 2, in the value assigned to the three_words variable,
# with value in the index position 2 being changed from lowercases to uppercases and uppercases to lowercases due to
# it being attached to the string method ".swapcase()".
three_words[2] = three_words[2].swapcase()
# This function displays the value assigned to the three_words variable in the output.
print(three_words)
# [ ] review and run example
# the list before Insert
# The variable "party_list" is assigned to a list of strings.
party_list = ["Joana", "Alton", "Tobias"]
# This function displays the string "party_list before: " and the value assigned to the party_list variable in the output.
print("party_list before: ", party_list)
# This function displays the string "index 1 is", the value in the index position 1 that is in the list of strings of the
# party_list variable, the string "index 2 is", and the value in the index position 2, in the output. Additionally, the
# string "index 2 is" and the value in the index position 2 will be displayed on the next line in the output due to the
# escape character "\n". After that, there will also be a blank line on the next line in the output because of the escape
# character "\n".
print("index 1 is", party_list[1], "\nindex 2 is", party_list[2], "\n")
# the list after Insert
# This line of code adds the string "Colette" to the index position 1 that is in the value assigned to the party_list
# variable due to the ".insert()" method.
party_list.insert(1,"Colette")
# This function displays the string "party_list after: " and the value assigned to the party_list in the output.
print("party_list after: ", party_list)
# This function displays the string "index 1 is", the value in the index position 1 that is in the list of strings of the
# party_list variable, the string "index 2 is", the value in the index position 2 , the string "index 3 is", and the value
# in the index position 3, in the output. Additionally, the string "index 2 is" and the value in the index position 2 will
# be displayed on the next line in the output due to the escape character "\n". After that, the string "index 3 is" and the
# value in the index position 3 will be on the next line in the output because of the escape character "\n".
print("index 1 is", party_list[1], "\nindex 2 is", party_list[2], "\nindex 3 is", party_list[3])
# [ ] insert a name from user input into the party_list in the second position (index 1)
# The variable "party_list" is assigned to a list of strings.
party_list = ["Joana", "Alton", "Tobias"]
# The entry the user inputs, when presented with the string "Enter a name: ", will be add to the index position 1
# in the value assigned to the party_list variable due to the ".insert()" method.
party_list.insert(1, input("Enter a name: "))
# [ ] print the updated list
# This function displays the value assigned tnhn o the party_list variable in the output.
print(party_list)
# [ ] Fix the Error
# This program originally contained an AttributeError due to the code that tries to add the string "pine" to the index
# position 1 in the value assigned to the tree_list variable. However, the value assigned to the tree_list variable is not a
# list but a string. This is fixed by changing the string" "oak" into a list by placing '"oak"' within sqaure brackets.
# The variable "tree_list" is assigned to a list that has one string.
tree_list = ["oak"]
# This function displays the string "tree_list before =" and the value assigned to the tree_list variable in the output.
print("tree_list before =", tree_list)
# This line of code adds the string "pine" to the index position 1 in the value assigned to the tree_list variable due to
# the ".insert " method.
tree_list.insert(1,"pine")
# This function displays the string "tree_list after =" and the value assigned to the tree_list variable in the output.
print("tree_list after =", tree_list)