Exercise 2-6 Solution – Python Crash Course Chapter 2: Variables and Simple Data Types

 

📘 Python Crash Course – Chapter 2: Variables and Simple Data Types – Exercise 2-6 Solution

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


📝 Exercise 2-6

Task:Repeat Exercise 2-5, but this time store the famous person’s name in a variable called famous_person . Then compose your message and store it in a new variable called message . Print your message.

✅ Solution Code:


famous_person = "Tony Stark"
message="Learn from youe mistakes"

print(f'{famous_person} said : "{message}"')

🧠 Code Explanation:

This Python code prints a quote by a famous person, with the person's name and the message stored in separate variables. Here's how it works:

  1. Store the famous person's name:

    The variable famous_person is assigned the string "Tony Stark", representing the name of the person.

  2. Store the message:

    The variable message is assigned the string "Learn from your mistakes", which is the quote to be displayed.

  3. Print the formatted message:

    The print() function is used to display the quote in a formatted way, combining the famous_person and message variables. The output will be:

This code demonstrates how to use variables to store data and format a message using an f-string in Python.

🔍 Output:


Tony Stark said : "Learn from youe mistakes"

📚 Related Exercises from Chapter 2:


🔗 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

Previous Post Next Post

Contact Form