Provide your own examples of the following using Python lists. Create your own examples. Do not copy them from another source.
Nested lists
The “*” operator
List slices
The “+=” operator
A list filter
A list operation that is legal but does the "wrong" thing, not what the programmer expects
Provide the Python code and output for your program and all your examples
# Nested list
nested_list = [1,2,3,4,[5,6],7]
# operator
my_list = ['cat','dog','parrot']*3
# List slices
my_slice = my_list[2:4]
# += operator
my_list += ['rat', 'giraffe']
# List filter
boolean = [True, False, False, True]
list(filter(None,boolean))
# A list operation that is legal but does the "wrong" thing, not what the programmer expects
a = [2, 3, 5, 7, 11, 13]
a = a.append(17)
# suppose to add number 17 to the end of list, so a = [2, 3, 5, 7, 11, 13, 17]
print(a)
#instead, the result is a = None
Comments
Leave a comment