π Python Crash Course – Chapter 8: Functions – Exercise 8-15 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 8-15
Task:
Put the functions for the example print_models.py in a
separate file called printing_functions.py . Write an import statement at the top
of print_models.py, and modify the file to use the imported functions
✅ Solution Code:
helper_8_15.py
# this help to solve the exercise 8.15 and 8.16
def your_name(first, last):
full_name = f"{first} {last}"
return f'Your name is :{full_name}'
exercise_8_15.py
import helper_8_15
# Import the function from the helper file
print(helper_8_15.your_name("John", "Doe"))
π§ Code Explanation:
-
Create a separate module:
A separate file named
helper_8_15.py
is created to store reusable functions. This helps in organizing the code and keeping the main file clean and modular. -
Import the module:
The
import
statement is used to import thehelper_8_15
module into the current file. This allows access to all the functions defined in the module. -
Call a function from the module:
The function
your_name()
is called from thehelper_8_15
module using dot notation (helper_8_15.your_name()
). This demonstrates how to use a function from an imported module. -
Pass arguments to the function:
The function is called with arguments
"John"
and"Doe"
, which are passed to the function defined in the module. The function processes these arguments and performs its task.
π Output:
Your name is :John Doe
π Related Exercises from Chapter 8:
- ➤ Exercise 8-1 | Message
- ➤ Exercise 8-2 | Favorite Book
- ➤ Exercise 8-3 | T-Shirt
- ➤ Exercise 8-4 | Large Shirts
- ➤ Exercise 8-5 | Cities
- ➤ Exercise 8-6 | City Names
- ➤ Exercise 8-7 | Album
- ➤ Exercise 8-8 | User Albums
- ➤ Exercise 8-9 | Magicians
- ➤ Exercise 8-10 | Great Magicians
- ➤ Exercise 8-11 | Unchanged Magicians
- ➤ Exercise 8-12 | Sandwiches
- ➤ Exercise 8-13 | User Profile
- ➤ Exercise 8-14 | Cars
- ➤ Exercise 8-15 | Printing Models
- ➤ Exercise 8-16 | Imports
- ➤ Exercise 8-17 | Styling Functions
π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