π 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:
-
Store the favorite number:
The variable
no
is assigned the value10
, which represents the favorite number. -
Store the message:
The variable
msg
is assigned the string"My favourite no is"
, which is the message to be displayed. -
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:
- ➤ Exercise 2-1 | Simple Message Part 1
- ➤ Exercise 2-2 | Simple Messages Part 2
- ➤ Exercise 2-3 | Personal Message
- ➤ Exercise 2-4 | Name Cases
- ➤ Exercise 2-5 | Famous Quote
- ➤ Exercise 2-6 | Famous Quote 2
- ➤ Exercise 2-7 | Stripping Names
- ➤ Exercise 2-8 | Number Eight
- ➤ Exercise 2-9 | Favorite Number
- ➤ Exercise 2-10 | Adding Comments
- ➤ Exercise 2-11 | Zen of Python
π 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
π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