Exercise 3.2 Solution Python Crash Course Chapter 3: Introducing Lists

 

πŸ“˜ Python Crash Course – Chapter 3: Introducing Lists – Exercise 3-2 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


πŸ“ Exercise 3-2

Task: Start with the list you used in Exercise 3-1, but instead of just printing each person’s name, print a message to them . The text of each mes sage should be the same, but each message should be personalized with the person’s name

✅ Solution Code:


name = ["jarvis" ,'friday' , 'hulk', 'natasha', 'captain']

message = "Get Ready to fight"

print(f"Get ready to fight {name[0]}")
print(f"Get ready to fight {name[1]}")
print(f"Get ready to fight {name[2]}")
print(f"Get ready to fight {name[3]}")
print(f"Get ready to fight {name[4]}")

🧠 Code Explanation:

This Python code demonstrates how to personalize messages for each element in a list. Here's how it works:

  1. Create a list:

    The variable name is assigned a list containing the names ["jarvis", "friday", "hulk", "natasha", "captain"]. Each name is an element in the list.

  2. Store a message:

    The variable message is assigned the string "Get Ready to fight", which will be personalized for each name in the list.

  3. Print personalized messages:

    The print() function is used with an f-string to combine the message and each name from the list. Each name is accessed using its index (e.g., name[0], name[1], etc.), and a personalized message is printed for each name.

πŸ” Output:


Get ready to fight jarvis
Get ready to fight friday
Get ready to fight hulk
Get ready to fight natasha
Get ready to fight captain

πŸ“š 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