# [ ] review and run example
# the list before Insert
party_list = ["Joana", "Alton", "Tobias"]
print("party_list before: ", party_list)
# the list after Insert
party_list[1] = "Colette"
print("party_list after: ", party_list)
# [ ] review and run example
party_list = ["Joana", "Alton", "Tobias"]
print("before:",party_list)
# modify index value
party_list[1] = party_list[1] + " Derosa"
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
party_list[3] = "Alton"
print(party_list)
# [ ] review and run example changes the data type of an element
# replace a string with a number (int)
single_digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
print("single_digits: ", single_digits)
print("single_digits[3]: ", single_digits[3], type(single_digits[3]),"\n")
# replace string with an int
single_digits[3] = 3
print("single_digits: ", single_digits)
print("single_digits[3]: ", single_digits[3], type(single_digits[3]))
# [ ] complete "replace items in a list" task
three_num=[2, 4, 6]
print(three_num)
if three_num[0] <5:
three_num[0]="small"
else:
three_num[0]="large"
print(three_num)
[2, 4, 6]
['small', 4, 6]
# [ ] create challenge function
int_list=[2,3,4,5,6]
def str_replace(int_list,index):
if int_list[index] < 4:
int_list[index]="small"
else:
int_list[index]="large"
str_replace(int_list,0)
print(int_list)
['small', 3, 4, 5, 6]
# [ ] complete coding task described above
three_words = ["Happy","Shoe","Rainbow"]
print(three_words)
three_words[0] = three_words[0].upper()
three_words[2] = three_words[2].swapcase()
print(three_words)
['Happy', 'Shoe', 'Rainbow']
['HAPPY', 'Shoe', 'rAINBOW']
# [ ] review and run example
# the list before Insert
party_list = ["Joana", "Alton", "Tobias"]
print("party_list before: ", party_list)
print("index 1 is", party_list[1], "\nindex 2 is", party_list[2], "\n")
# the list after Insert
party_list.insert(1,"Colette")
print("party_list after: ", party_list)
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)
party_list = ["Joana", "Alton", "Tobias"]
party_list.insert(2,"Bubba")
# [ ] print the updated list
print(party_list)
['Joana', 'Alton', 'Bubba', 'Tobias']
# [ ] Fix the Error
tree_list = ["oak"]
print("tree_list before =", tree_list)
tree_list.insert(1,"pine")
print("tree_list after =", tree_list)
tree_list before = ['oak']
tree_list after = ['oak', 'pine']