Write a Python program that does the following.
Part 2
Create your own examples using python lists
Provide the Python code and output for your program and all your examples.
Part 1
# Part 1
pizzas = "Cheese Veggie Pepperoni Meat Margherita BBQ Hawaiian Buffalo"
words = pizzas.split()
words.remove("Veggie")
words.pop()
del words[3]
words.sort()
words.append("Works")
words.insert(1, "Supreme")
words.extend(("Pizza",))
string = ' '.join(words)
print(string)
Output:
BBQ Supreme Cheese Hawaiian Meat Pepperoni Works Pizza
Part 2:
# Nested lists
pizza_orders = [["Cheese", "Veggie"], ["Pepperoni", "Meat", "Havaiian"], ["Buffalo"]]
for order in pizza_orders:
print("Pizzas orederd:", order)
print()
# The "*" operator
print("Happy hours!")
double_order = ["Margherita"]*2
print("You get:")
print(double_order)
print()
# List slices
first_two = pizza_orders[:2]
print("First two order")
for order in first_two:
print("Pizzas ordered:", order)
print()
# The "+=" operator
pizza_orders += [double_order]
for order in pizza_orders:
print("Pizzas ordered:", order)
print()
# The filter
print("We have not Veggie, sorry")
new_orders = filter(lambda x: "Veggie" not in x, pizza_orders)
for order in new_orders:
print("Pizzas ordered:", order)
print()
# A list operation that is legal but does the "wrong" thing, not what the programmer expects
L = [[1, 1, 1], [2, 2, 2]]
print("Try to add third nested list [3, 3,3]")
L += [3, 3, 3]
print(L)
Output:
Pizzas orederd: ['Cheese', 'Veggie']
Pizzas orederd: ['Pepperoni', 'Meat', 'Havaiian']
Pizzas orederd: ['Buffalo']
Happy hours!
You get:
['Margherita', 'Margherita']
First two order
Pizzas ordered: ['Cheese', 'Veggie']
Pizzas ordered: ['Pepperoni', 'Meat', 'Havaiian']
Pizzas ordered: ['Cheese', 'Veggie']
Pizzas ordered: ['Pepperoni', 'Meat', 'Havaiian']
Pizzas ordered: ['Buffalo']
Pizzas ordered: ['Margherita', 'Margherita']
We have not Veggie, sorry
Pizzas ordered: ['Pepperoni', 'Meat', 'Havaiian']
Pizzas ordered: ['Buffalo']
Pizzas ordered: ['Margherita', 'Margherita']
Try to add third nested list [3, 3,3]
[[1, 1, 1], [2, 2, 2], 3, 3, 3]
Comments
Leave a comment