# Zhi Yuan "Oscar" Lin
# 24 October 2021
# In this lesson, I learned more about the del statement, and the list methods, ".pop()" and ".remove()", through examples
# and tasks. Additionally, I learned a new error called, "ValueError".
# There were no major problems or questions throughout this lesson.
# [ ] review and run example
# the list before delete
# The variable "sample_list" is assigned to a list of integers.
sample_list = [11, 21, 13, 14, 51, 161, 117, 181]
# This function displays the string "sample_list before: " and the value assigned to the sample_list variable in the output.
print("sample_list before: ", sample_list)
# This line of code deletes the value in the index position 1 in the value assigned to the sample_list variable due to the
# keyword "del".
del sample_list[1]
# the list after delete
# This function displays the string "sample_list after: " and the value assigned to the sample_list variable in the output.
print("sample_list after: ", sample_list)
# [ ] review and run example Multiple Times
# [ ] consider how to reset the list values?
# Answer: Run the program above to reset the list values.
# This function displays the string "sample_list before: " and the value assigned to the sample_list variable in the output.
print("sample_list before:", sample_list)
# This line of code deletes the value in the index position 0 in the value assigned to the sample_list variable due to the
# keyword "del".
del sample_list[0]
# This function displays the string "sample_list after: " and the value assigned to the sample_list variable in the output.
print("sample_list after:", sample_list)
# [ ] review and run example
# The variable "mixed_types" is assigned to a list of mixed data types.
mixed_types = [1, "cat"]
# append number
# This line of code adds the integer "3" to the end of the value assigned to the mixed_types variable.
mixed_types.append(3)
# This function displays the string "mixed_types list: " and the value assigned to the mixed_types variable in the output.
print("mixed_types list: ", mixed_types)
# append string
# This line of code adds the string "turtle" to the end of the value assigned to the mixed_types variable.
mixed_types.append("turtle")
# This function displays the string "mixed_types list: " and the value assigned to the mixed_types variable in the output.
print("mixed_types list: ", mixed_types)
# [ ] print ft_bones list
# [ ] delete "cuboid" from ft_bones
# [ ] reprint list
# The variable "ft_bones" is assigned to a list of strings.
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
# This function displays the value assigned to the ft_bones variable in the output.
print(ft_bones)
# This line of code deletes the string "cuboid" from the value assigned to the ft_bones variable due to the keyword "del".
del ft_bones[2]
# This function displays the value assigned to the ft_bones variable in the output.
print(ft_bones)
# [ ] print ft_bones list
# [ ] delete "cuboid" from ft_bones
# [ ] delete "navicular" from list
# [ ] reprint list
# [ ] check for deletion of "cuboid" and "navicular"
# The variable "ft_bones" is assigned to a list of strings.
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
# This function displays the value assigned to the ft_bones variable in the output.
print(ft_bones)
# This line of code deletes the string "cuboid" from the value assigned to the ft_bones variable due to the keyword "del".
del ft_bones[2]
# This line of code deletes the string "navicular" from the value assigned to the ft_bones variable due to the keyword "del".
del ft_bones[2]
# This function displays the value assigned to the ft_bones variable in the output.
print(ft_bones)
# [ ] review and run example
# pop() gets the last item by default
# The variable "party_list" is assigned to a list of strings.
party_list = ["Joana", "Alton", "Tobias"]
# This function displays the value assigned to the party_list variable in the output.
print(party_list)
# This function displays the string "Hello" and the value assigned to the party_list variable in the output, after the value
# in the last index position in the value assigned to the party_list is removed by the list method ".pop()", that does not
# have an index specified.
print("Hello,", party_list.pop())
# This function displays the value assigned to the party_list variable in the output. Additionally, before the value is
# displayed there will be a blank line displayed in the previous line in the output due to the escape character "\n".
print("\n", party_list)
# This function displays the string "Hello" and the value assigned to the party_list variable in the output, after the value
# in the last index position in the value assigned to the party_list is removed by the list method".pop()", that does not
# have an index specified.
print("Hello,", party_list.pop())
# This function displays the value assigned to the party_list variable in the output. Additionally, before the value is
# displayed there will be a blank line displayed in the previous line in the output due to the escape character "\n".
print("\n", party_list)
# This function displays the string "Hello" and the value assigned to the party_list variable in the output, after the value
# in the last index position in the value assigned to the party_list is removed by the list method ".pop()", that does not
# have an index specified.
print("Hello,", party_list.pop())
# This function displays the value assigned to the party_list variable in the output. Additionally, before the value is
# displayed there will be a blank line displayed in the previous line in the output due to the escape character "\n".
print("\n", party_list)
# [ ] review and run example
# can pop specific index like pop(3)
# The variable "number_list" is assigned to a list of integers.
number_list = [11, 21, 13, 14, 51, 161, 117, 181]
# This function displays the string "before:" and the value assigned to the number_list variable in the output.
print("before:", number_list)
# This line of code just removes the value in the index position 3 in the value assigned to the number_list variable due
# to the list method".pop()".
number_list.pop(3)
# This function displays the string "after :" and the value assigned to the number_list variable in the output.
print("after :", number_list)
# [ ] review and run example
# set a variable to a poped value
# The variable "number_list" is assigned to a list of integers.
number_list = [11, 21, 13, 14, 51, 161, 117, 181]
# This function displays the string "list before:" and the value assigned to the number_list variable in the output.
print("list before:", number_list)
# This line of code removes the last value in the value assigned to the number_list variable, because there is no
# index specified, and assigned it to the variable "num_1" due to the list method ".pop()".
num_1 = number_list.pop()
# This line of code removes the last value in the value assigned to the number_list variable, because there is no
# index specified, and assigned it to the variable "num_2" due to the list method ".pop()".
num_2 = number_list.pop()
# This function displays the string "list after:" and the value assigned to the number_list variable in the output.
print("list after :", number_list)
# This function displays the string "add the popped values:", the value assigned to the num_1 variable, the string "+", the
# value assigned to the num_2 variable, the string "=", and the sum of the value assigned to the num_1 variable and the
# value assigned to the num_2 variable, in the output.
print("add the popped values:", num_1, "+", num_2, "=", num_1 + num_2)
# [ ] pop() and print the first and last items from the ft_bones list
# The variable "ft_bones" is assigned to a list of strings.
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
# This line of code removes the value in the index position 0 in the value assigned to the ft_bones variable due to
# the list method ".pop()".
ft_bones.pop(0)
# This line of code removes the last value in the value assigned to the ft_bones variable, because there is no
# index specified, due to the list method ".pop".
ft_bones.pop()
# [ ] print the remaining list
# This function displays the value assigned to the ft_bones variable in the output.
print(ft_bones)
# The variable "dog_types" is assigned to a list of strings.
dog_types = ["Lab", "Pug", "Poodle"]
# In this case, the while loop is followed by the variable "dog_types". Hence, as long the list assigned to the dog_types
# variable is not empty 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 displaying the last value in the value assigned to the dog_types variable,
# that is removed due to the list method ".pop()" not having an index specified, in the output. This loop will continue
# until the list assigned to the dog_types variable is empty.
while dog_types:
print(dog_types.pop())
#[ ] complete the Register Input task above
# The variable "purchase_amounts" is assigned to an empty list.
purchase_amounts = [ ]
# In this case, the while loop is written with the keyword "True". 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 the variable
# "price" that will be assigned to the entry the user inputs when presented with the string "Enter the price of the items
# (or "done" to end): ". Subsequently, there is an IF statement that determines if the value assigned to the price variable
# is all digits by using the string method ".isdigit()". If true, the value assigned to the price variable will be added
# to the value assigned purchase_amounts variable due to the ".append()" method. If false, the program moves on to the elif
# statement and determines if the value assigned to the price variable is equal to, ==, the string "done" after being
# changed into all lowercases due to the price variable being attached to the string method ".lower()". If true, the loop
# will end due to the following keyword "break". If false, the program moves on to the else statement with the string
# "Invalid input", that is within the parentheses of the print() function, being display in the output.
while True:
price = input('Enter the price of the items (or "done" to end): ')
if price.isdigit():
purchase_amounts.append(price)
elif price.lower() == 'done':
break
else:
print("Invalid input")
# This function displays the value assigned to the purchase_amounts variable in the output.
print(purchase_amounts)
# [ ] complete the Register Total task above
# The variable "subtotal" is assigned to the integer "0".
subtotal = 0
# In this case, the while loop is followed by the variable "purchase_amounts". Hence, as long the list assigned to the
# purchase_amounts variable is not empty 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 variable "value" being assigned to the last value
# that was removed from the value assigned to the to the purchase_amounts variable by the ".pop()" method, after it is
# changed into a float due to it being within the parentheses of the float() function. Subsequently, the subtotal variable
# will be reassigned to the sum of the value assigned to the subtotal variable and the value assigned to the value
# variable.
while purchase_amounts:
value = float(purchase_amounts.pop())
subtotal += value
# This function displays the value assigned to the subtotal variable in the output.
print(subtotal)
# [ ] review and run example
# The variable "dog_types" is assigned to a list of strings.
dog_types = ["Lab", "Pug", "Poodle"]
# This IF statement determines if the string "Pug" is within the value assigned to the dog_variable variable by using the
# keyword "in". If true, the string "Pug" will be removed from the value assigned to the dog_types variable by the list
# method ".remove()". If false, the program moves on to the else statement with the string "no Pug found", that is within
# the parentheses of the print() function, being display in the output.
if "Pug" in dog_types:
dog_types.remove("Pug")
else:
print("no Pug found")
# This function displays the value assigned to the dog_types variable in the output.
print(dog_types)
# [ ] review and run example
# The variable "dogs" is assigned to a list of strings.
dogs = ["Lab", "Pug", "Poodle", "Poodle", "Pug", "Poodle"]
# This function displays the value assigned to the dogs variable in the output.
print(dogs)
# In this case, the while loop is followed by the the statement '"Poodle" in dogs'. Hence, as long the string "Poodle" is
# within the value assigned to the dogs 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 first string "Poodle" the program
# identifies being removed from the value assigned to the dogs variable by the list method ".remove()". Subsequently,
# the value assigned to the dogs variable will be display in the output. This loop will continue until the string "Poodle"
# is no longer found within the value assigned to the dogs variable.
while "Poodle" in dogs:
dogs.remove("Poodle")
print(dogs)
# [ ] review and run example
# Change to "Lab", etc... to fix error
# This program originally contained a ValueError due to the list method ".remove()" trying to remove the string "Collie"
# from the value that is assigned to dogs variable when there is no "Collie" within the value. This error is fixed by
# replacing the string "Collie" with a string that is within the value assigned to the dogs variable.
# This line of code removes the string "Lab" that is within the value that is assigned to the dogs variable.
dogs.remove("Lab")
# This function displays the value assigned to the dogs variable in the output.
print(dogs)
# [ ] remove one "Poodle" from the list: dogs , or print "no Poodle found"
# [ ] print list before and after
# The variable "dogs" is assigned to a list of strings.
dogs = ["Lab", "Pug", "Poodle", "Poodle", "Pug", "Poodle"]
# This function displays the string "list before: " and the value assigned to the dogs variable in the output.
print("list before: ", dogs)
# This IF statement determines if the string "Poodle" is within the value assigned to the dogs variable by using the
# keyword "in". If true, the first string "Poodle" the program identifies will be remove by the list method ".remove()".
# If false, the program moves on to the else statement with the string "no Poodle found", that is within the parentheses
# of the print() function, being display in the output.
if "Poodle" in dogs:
dogs.remove("Poodle")
else:
print("no Poodle found")
# This function displays the string "list after: " and the value assigned to the dogs variable in the output.
print("list after: ", dogs)