# [ ] 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)
party_list before: ['Joana', 'Alton', 'Tobias']
party_list after: ['Joana', 'Colette', 'Tobias']
# [ ] 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)
before: ['Joana', 'Alton', 'Tobias']
after: ['Joana', 'Alton Derosa', 'Tobias']
# 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)
Execution error
IndexError: list assignment index out of range
# [ ] 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]))
single_digits: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
single_digits[3]: three <class 'str'>
single_digits: ['zero', 'one', 'two', 3, 'four', 'five', 'six', 'seven', 'eight', 'nine']
single_digits[3]: 3 <class 'int'>
# [ ] complete "replace items in a list" task
three_num = [16, 71, 43]
print(three_num)
if three_num[0]<16:
three_num[0] = "small"
else:
three_num[0] = "large"
print(three_num)
[16, 71, 43]
['large', 71, 43]
# [ ] create challenge function
def str_replace(int_list,index):
if int_list[index]<5:
int_list[index] = "small"
else:
int_list[index] = "large"
print(int_list)
str_replace([2,3,8],2)
[2, 3, 'large']
# [ ] complete coding task described above
three_words = ["Nice","Red","Boots"]
print(three_words)
three_words[0] = three_words[0].upper()
three_words[2] = three_words[2].swapcase()
print(three_words)
['Nice', 'Red', 'Boots']
['NICE', 'Red', 'bOOTS']
# [ ] 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])
party_list before: ['Joana', 'Alton', 'Tobias']
index 1 is Alton
index 2 is Tobias
party_list after: ['Joana', 'Colette', 'Alton', 'Tobias']
index 1 is Colette
index 2 is Alton
index 3 is Tobias
# [ ] insert a name from user input into the party_list in the second position (index 1)
party_list = ["Joana", "Alton", "Tobias"]
# [ ] print the updated list
partyname = input("Enter your name to be invited to the party: ")
party_list.insert(1,partyname)
print(party_list)
['Joana', 'Sam', 'Alton', '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']