Question #249970

Write a Python program that does the following. 

  • Create a string that is a long series of words separated by spaces. The string is your own creative choice. It can be names, favorite foods, animals, anything. Just make it up yourself. 
  • Turn the string into a list of words using split
  • Delete three words from the list, but delete each one using a different kind of Python operation. 
  • Sort the list. 
  • Add new words to the list (three or more) using three different kinds of Python operation. 
  • Turn the list of words back into a single string using join
  • Print the string. 

Part 2

Create your own examples using python lists

  • 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. 

 

 



Expert's answer

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]

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS