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

 

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

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


📝 Exercise 2-9

Task:Store your favorite number in a variable . Then, using that variable, create a message that reveals your favorite number . Print that message

✅ Solution Code:


no=10
msg="My favourite no is"
print(f"{msg} {no}")

🧠 Code Explanation:

This Python code demonstrates how to store a favorite number in a variable and create a message using that variable. Here's how it works:

  1. Store the favorite number:

    The variable no is assigned the value 10, which represents the favorite number.

  2. Store the message:

    The variable msg is assigned the string "My favourite no is", which is the message to be displayed.

  3. Print the message with the number:

    The print() function is used with an f-string to combine the message and the number into a single formatted string.

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

🔍 Output:


My favourite no is 10

📚 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