YOU ARE TO REWRTIE THESE CODES YOURSELVES.
Question 1:
Think about the city you were born. Make a Python list of some of the interesting things to see. Use your list to print
a series of statements about these items, such as "Paris is wonderful in the spring because everyone is eating
crepes". Print one statement for each item in your list using a fstring. Print the statements using the index position
counting from the beginning and the end of the list.
ANS
#Program to list events in place of my birth
birthcity = "Jos Plateau"
jos_plateau = ["Fruits", "Cold Climate", "Rich harvest", "Festivals"]
print(jos_plateau[0], f"All year round availability of fresh fruits makes {birthcity} unique.")
print(jos_plateau[1], f"The only place in the country you have year-round cool weather is
{birthcity}.")
print(jos_plateau[2], f"The very exciting and rich festivals of {birthcity} are a joy to watch.")
print(jos_plateau[3], f"Harvest times are exciting times in {birthcity}.")
Question 2:
Step 1: You just got an important call from the new president of the Intergalactic Space Force, Priyanka Chopra. She
would like you to bring four friends on a mission to save humanity. Make a list of the five people going on the mission
(yourself included) and print out a message to each person letting them know they are going on the mission. Print a
statement using len() and an fstring showing how many people are on the mission.
ANS
# Program of the list of my mission friends
missionlist = ["Alex", "Steve", "Ugo", "Jane", "Cloe"]
print( missionlist)
# Printing the message for each of my friends
print(missionlist[1] + “ This is to inform you that you are part of the mission, congratulations!”)
print(missionlist[2] + “ This is to inform you that you are part of the mission congratulations”)
print(missionlist[3] + “ This is to inform you that you are part of the mission congratulations !”)
print(missionlist[4] + “ This is to inform you that you are part of the mission congratulations!”)
# To print the number of the listed names
print(len(missionlist))
sentence = f“There are five persons selected for this mission {missionlist} ”
print(sentence)
ADD TWO MORE NAMES AND REWRITE THIS CODE
Step 2: One of the members of the mission is unable to make it due to a bad case of Space Sneezing. Print a message
to the person letting them know they are not able to go. Find someone else who is able to go on the mission and
modify the list replacing the name of your sick space colleague with the new volunteer. Then print new messages to
everyone on the team informing them of the mission. Print a statement using len() and an fstring showing how many
people are now on the mission inside of a sentence.
ANS
# To modify the list of the chosen members of the mission
print(missionlist[2] + “ will not be able to make the team for the mission due to ill health”)
#Ugo will be replaced by John
missionlist[2] = “ John ”
print(missionlist)
print(len(missionlist))
sentence = f“The names of the five persons selected for this mission are {missionlist} ”
print(sentence)
Step 3: On short notice, a new space rocket has become available and more members of the team are able to join
the mission to space. The catch is that they must all be celebrities for publicity reasons. Use insert() to add one
celebrity to the beginning and middle of the list. Then use append() to add a celebrity to the end of the list. When
the three celebrities have been added to the list of space adventurers, print out a message to each person on the list
letting them know about the mission. Print a statement using len() and an fstring showing how many people are now
on the mission inside of a sentence.
ANS
# Adding names of Celebrities to the list
missionlist = [ “Alex”, “ Steve”, “ John”, “ Jane”, “ Cloe”]
# Inserting Kylian Mbambe
missionlist.insert ( 0, “Mbampe”)
print(missionlist)
#Adding Rehanna the famous singer in the middle of the list
missionlist.insert (3, “ Rehanna ”)
print(missionlist)
# Adding Messi at the end of the list using append
missionlist.append ( “ Messi ”)
print(missionlist)
# To print the new number and names of the list of the mission team
print(len(missionlist))
sentence = f“The names of the eight persons selected for this mission are {missionlist} ”
print(sentence)
Step 4: In yet another cruel twist of fate, Priyanka Chopra calls to let you know that your shuttle has been given to a
mission team with a more important mission. How this could be possible you have no idea. Your new vessel, the Little
Ducky, only has room for two passengers. First print a message letting everyone know only two people will be able to
go on the mission. Then use the pop() method to remove people one at a time from the list and print a message to
them letting them know that they will not be able to attend the mission. Once the list is down to the final two
people, print each of them a message letting them know they will be going on the mission. It's OK if you are not
among the final two chosen to go on the mission. Print a statement using len() and an fstring showing how many
people are now on the mission inside of a sentence.
# The new twist and unexpected change of plans for the crew members
newinfo= “Regrettably you cant make it as only two persons can be part of the mission”
print(newinfo)
missionlist = [ “Mbampe”, “Alex”, “Steve”, “Rehanna ”, “ John”, “ Jane”, “Cloe”, “Messi ”]
print((missionlist.pop(1)) +” ” + newinfo)
print(missionlist.pop(2) + “ ”+ newinfo)
print(missionlist.pop(3) + “ ”+ newinfo)
print((missionlist.pop(1)) +” ” + newinfo)
print((missionlist.pop(2)) +” ” + newinfo)
print((missionlist.pop(1)) + “ ” + newinfo)
# To print the new number and names of the list of the mission team
print(len(missionlist))
sentence = f“The names of the two persons selected for this mission are {missionlist} ”
print(sentence)
Step 5: In a final blow to your already shaky leadership, Priyanka Chopra calls and lets you know that your now
downgraded mission has been canceled entirely. Use the del() method to delete everyone from the list and print the
list to show it is empty. Print a statement using len() and an fstring showing how many people are now on the mission
inside of a sentence.
ANS
# To clear the list
missionlist = [ “Mbampe”, “Messi ”]
del missionlist[0]
del missionlist[0]
print(missionlist)
print(len(missionlist))
sentence = f“All the names selected for this mission are removed from the list {missionlist} ”
print(sentence)
Question 3:
Brazilian football star Ronaldo has been tasked with finding locations for the next World Cup. Think of five places
anywhere on earth the event could be held and store them in a list.
Print the list as you stored it
Print the list using sorted() without modifying the original list
Print the list as you stored it
Print the list using sorted() in reverse alphabetical order without modifying the original list
Print the list as you stored it
Print the list using reverse()
Print the list using reverse() a second time to bring it back to its original form
Print the list using sort() in alphabetical order
Print the list using sort() a second time to print the list in reverse alphabetical order
ANS
# Venues that can host next FIFA World Cup
myvenues = [ “Toronto”, “ Lagos”, “ Paris”, “ Houston”, “ Quebec”]
# To Print the list as I stored it
print(myvenues)
#To Sort and print in ascending order
sortedlist= myvenues.sort ()
print(“Sorted in Ascending order ” , sortedlist)
#To print as it was stored initially
myvenues = [ “Toronto”, “Lagos”, “Paris”, “Houston”, “Quebec”]
print(“Original Content ”, myvenues)
# Print the list in reverse sort
myvenues.sort (reverse = True)
print(“Sorted in reverse”, myvenues)
#To Sort to reverse the reverse
myvenues.sort (reverse = True)
print( “ The second reverse sort ”, myvenues)
#To print the list as it was stored originally
myvenues = [ “Toronto”, “Lagos”, “Paris”, “Houston”, “Quebec”]
print(“Original Content ”, myvenues)
#To print sorted list in alphabetical order again
myvenues.sort()
print(“ The new sort is”, myvenues)
#To print the second sorted list
newsort = myvenues.sort()
print( “The last sort is ”, newsort)
Question 4:
Create five or more lists (for example, places, colours, types of cheese, super heroes, time, numbers, cats, cars, food,
etc) and using the random package (import random at the top of your script, and use random.choice(list) and
random.randint(1,5) ), write a story that uses random elements from the lists and random numbers. Your story
should change each time you run it. Make sure to properly format where applicable using .title()
ANS
# Creating story
import random
# To define the lists
places = ["Paris", "New York", "London", "Tokyo", "Rome"]
colours = ["red", "blue", "green", "yellow", "purple"]
heroes = ["Superman", "Spider-Man", "Batman", "Wonder Woman", "Captain America"]
times = ["morning", "afternoon", "evening", "night"]
numbers = [1, 2, 3, 4, 5]
cars = ["Ferrari", "BMW", "Tesla", "Mercedes", "Audi"]
foods = ["pizza", "sushi", "burger", "pasta", "tacos"]
# Generate random elements
random_place = random.choice(places)
random_colour = random.choice(colours)
random_hero = random.choice(heroes)
random_time = random.choice(times)
random_number = random.randint(1, 5)
random_car = random.choice(cars)
random_food = random.choice(foods)
# Generate the story
story = f"In {random_place}, on a {random_colour} {random_time}, there was a superhero named
{random_hero}. " \
f"She had {random_number} {random_car}s and loved driving them. " \
f"Back from a difficult operation she was hungry and, she decided to eat {random_food} . " \
f"It was a random adventure full of pleasant surprises!"
#Print story
print(story)
Question 5:
Make a list of your student number (for example, student = ["B",1,1,3,3,3,5,5,5]) and write a program that adds the
numbers of your student number together using list index positions. Then, using the same list, print out your student
number by converting numbers to strings as needed using an fstring.
ANS
# Converting numbers to strings
student = ["B", 1, 1, 3, 3, 3, 5, 5, 5]
# Add the numbers of the student number together
result = student[1] + student[2] + student[3] + student[4] + student[5] + student[6] + student[7] + student[8]
# Print the result
print("Sum of the student number:", result)
# Convert numbers to strings and print the student number
student_number = f"{student[0]}{str(student[1])}{str(student[2])}{str(student[3])}{str(student[4])}
{str(student[5])}{str(student[6])}{str(student[7])}{str(student[8])}"
print("Student number:", student_number)
Question 6:
Make a list called meals, and sub-lists with at least three items of foods for breakfast, lunch, and dinner (this means
you should have at least four lists and at least nine items). Print out the first and last item in each sub-list using an
index position inside of a sentence using an fstring.
ANS
#Program to list from sublist
meals = [
["Eggs", "Toast", "Bacon"],
["Sandwich", "Salad", "Soup"],
["Fried Rice", "Mashed Potatoes", "Porridge Yam"]
]
# Print the first and last item of each sub-list
for meal in meals:
first_item = meal[0]
last_item = meal[-1]
print(f"For {meal[0]}, the first item is {first_item} and the last item is {last_item}.")
NOTE: CAREFULLY ENTER THE CODES I HAVE RUN ALL OF THEM!!