π Python Crash Course – Chapter 3: Introducing Lists – Exercise 3-6 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 3-6
Task: You just found a bigger dinner table, so now more space is
available . Think of three more guests to invite to dinner .
• Start with your program from Exercise 3-4 or Exercise 3-5 . Add a print
statement to the end of your program informing people that you found a
bigger dinner table .
• Use insert() to add one new guest to the beginning of your list .
• Use insert() to add one new guest to the middle of your list .
• Use append() to add one new guest to the end of your list .
• Print a new set of invitation messages, one for each person in your list
✅ Solution Code:
# We use exercise 3.4
guest = ["Iron man" , "Captain" , "Hulk"]
print(f'Hello {guest[0]} Thanks for attending My Party')
print(f'Hello {guest[1]} Thanks for attending My Party')
print(f'Hello {guest[2]} Thanks for attending My Party')
print("Hello Everyone\nWe are happy to inform you that we have Three more guests")
guest.insert(0,"Hwakeye")
guest.insert(2,"Natasha")
guest.append("Nick Fury")
#New invitation
print("New Invitation")
print(f'Hello {guest[0]} Thanks for attending My Party')
print(f'Hello {guest[1]} Thanks for attending My Party')
print(f'Hello {guest[2]} Thanks for attending My Party')
print(f'Hello {guest[3]} Thanks for attending My Party')
print(f'Hello {guest[4]} Thanks for attending My Party')
print(f'Hello {guest[5]} Thanks for attending My Party')
π§ Code Explanation:
This Python code demonstrates how to modify a guest list by adding new guests and sending updated invitations. Here's how it works:
-
Create an initial guest list:
The variable
guest
is assigned a list containing the names of the initial guests:["Iron man", "Captain", "Hulk"]
. -
Send initial invitations:
The
print()
function is used with f-strings to send personalized invitations to each guest in the original list. -
Announce additional guests:
A message is printed to inform everyone that three more guests will be added to the list.
-
Add new guests:
guest.insert(0, "Hwakeye")
: Adds "Hwakeye" to the beginning of the list.guest.insert(2, "Natasha")
: Adds "Natasha" to the middle of the list (at index 2).guest.append("Nick Fury")
: Adds "Nick Fury" to the end of the list.
-
Send updated invitations:
The
print()
function is used again with f-strings to send personalized invitations to each guest in the updated list, which now includes six guests.
π Output:
Hello Iron man Thanks for attending My Party
Hello Captain Thanks for attending My Party
Hello Hulk Thanks for attending My Party
Hello Everyone
We are happy to inform you that we have Three more guests
New Invitation
Hello Hwakeye Thanks for attending My Party
Hello Iron man Thanks for attending My Party
Hello Natasha Thanks for attending My Party
Hello Captain Thanks for attending My Party
Hello Hulk Thanks for attending My Party
Hello Nick Fury Thanks for attending My Party
π Related Exercises from Chapter 3:
- ➤ Exercise 3-1 -> Names
- ➤ Exercise 3-2 -> Greetings
- ➤ Exercise 3-3 -> Your Own List
- ➤ Exercise 3-4 -> Guest List
- ➤ Exercise 3-5 -> Changing Guest List
- ➤ Exercise 3-6 -> More Guests
- ➤ Exercise 3-7 -> Shrinking Guest List
- ➤ Exercise 3-8 -> Seeing the World
- ➤ Exercise 3-9 -> Dinner Guests
- ➤ Exercise 3-10 -> Every Function
π Connect With Us:
“The only way to learn a new programming language is by writing programs in it.”
– Dennis Ritchie
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips #introducingLists
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment