Part 2
Provide your own examples of the following using Python lists. Create your own examples. Do not copy them from another source.
Provide the Python code and output for your program and all your examples.
# Nested lists
nested_list = ['a', 'b', ['c', 'd', ['11', '55']], 'h', 'l','u','z']
print("nested_list: ")
print(nested_list)
# The "*" operator
petNames=["Charlie","Max","Buddy"]*2
print("The \"*\" operator: ")
print(petNames)
# List slices
print("List petNames slices: ")
print(petNames[1:3])
# The "+=" operator
print("The \"+=\" operator: ")
petNames+=["Tod","Tommy"]
print(petNames)
# A list filter
print("A list filter")
filteredPetNamesList = filter(lambda name: name !="Tod", petNames)
print(list(filteredPetNamesList))
# A list operation that is legal but does the "wrong" thing, not
# what the programmer expects
# I wanted to sort the "petNames" List, but the result is None
print("Sort the \"petNames\" List")
petNames = petNames.sort()
print(petNames)
Example:
Comments
Leave a comment