Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Define A Module And Import A Specific Function In That Module To Another Program
Title :
Write A Python Program To Define A Module And Import A Specific Function In That Module To Another Program
Description :
Hi in this article we define a new module named as greet.py that has two functions: First is to say hello and then next function is for displaying a welcome message.
Contains of greet.py file
def say_hello(name):
msg = "Hello, " + name
return msg
def greet_welcome(name):
msg = "Welcome, " + name
return msg
In the main program we just import the greet module and use the above defined function Let's take a string variable and ask the user to enter his name, Then call the functions present in the great module to print the welcome or hello messages as shown below.
import greet
text = input("Enter a Your Name : ")
print(greet.greet_welcome(text))
print(greet.say_hello(text))
Complete Python Code :
Contains of greet.py
def say_hello(name):
msg = "Hello, " + name
return msg
def greet_welcome(name):
msg = "Welcome, " + name
return msg
Contents of main.py
import greet
text = input("Enter a Your Name : ")
print(greet.greet_welcome(text))
print(greet.say_hello(text))
Output :
Enter a Your Name : Reshma
Welcome, Reshma
Hello, Reshma
Output :
Enter a Your Name : Rakesh
Welcome, Rakesh
Hello, Rakesh
Conclusion :
The above Python program uses a user defined Module for Generating greeting string out of a given input name.
This program can also be solution for write a program illustrating use of user defined package in python
So in the main program I will be importing the given functions from the module and then using it into the main program to generate the greet string
Comment it down below if you have any query is regarding the above Python program and also suggestions to improve the above Python program
0 Comments