π Python Crash Course – Chapter 8: Functions – Exercise 8-16 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 8-16
Task:
Using a program you wrote that has one function in it, store that
function in a separate file . Import the function into your main program file, and
call the function using each of these approaches:
-
import module_name
-
from module_name import function_name
-
from module_name import function_name as fn
-
import module_name as mn
-
from module_name import *
✅ 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_16.py
# Importing the function from the helper file
import helper_8_15
print(helper_8_15.your_name("John", "Doe"))
# Importing the function using different approaches
from helper_8_15 import your_name
print(your_name("Jane", "Smith"))
from helper_8_15 import your_name as fn
print(fn("Alice", "Johnson"))
import helper_8_15 as mn
print(mn.your_name("Bob", "Brown"))
from helper_8_15 import *
print(your_name("Charlie", "Davis"))
π§ Code Explanation:
-
Import the entire module:
The statement
import helper_8_15
imports the entire module. Functions from the module are accessed using dot notation, such ashelper_8_15.your_name()
. -
Import a specific function:
The statement
from helper_8_15 import your_name
imports only theyour_name
function from the module. The function can then be called directly without using dot notation. -
Import a function with an alias:
The statement
from helper_8_15 import your_name as fn
imports theyour_name
function and assigns it an aliasfn
. The function can then be called using the alias, such asfn()
. -
Import the module with an alias:
The statement
import helper_8_15 as mn
imports the entire module and assigns it an aliasmn
. Functions from the module are accessed using the alias, such asmn.your_name()
. -
Import all functions from the module:
The statement
from helper_8_15 import *
imports all functions from the module. Functions can then be called directly without using dot notation or aliases.
π Output:
Your name is :John Doe
Your name is :Jane Smith
Your name is :Alice Johnson
Your name is :Bob Brown
Your name is :Charlie Davis
π 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