Exercise 3-6 Solution Python Crash Course Chapter 3: Introducing Lists

 

πŸ“˜ 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:

  1. Create an initial guest list:

    The variable guest is assigned a list containing the names of the initial guests: ["Iron man", "Captain", "Hulk"].

  2. Send initial invitations:

    The print() function is used with f-strings to send personalized invitations to each guest in the original list.

  3. Announce additional guests:

    A message is printed to inform everyone that three more guests will be added to the list.

  4. 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.
  5. 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:


πŸ”— 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

“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