What Is The Difference Between Python Function And Python Module
Solution 1:
Python Function :
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.
# Function definition is heredefprintme(str):
"This prints a passed string into this function"printstrreturn;
# Now you can call printme function
printme("Call user defined function")
printme("Again Call user defined function")
Output :
Calluser defined function
Again Calluser defined function
Python Module :
A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.
Here's an example of a simple module that define in support.py
defprint_func( par ):
print"Hello : ", par
return
For example, to import the module support.py, you need to put the following command at the top of the script −
# Import module supportimport support
# Now you can call defined function that module as follows
support.print_func("Muhammad Usman")
Output :
Hello :MuhammadUsman
Solution 2:
In programming, function refers to a segment that groups code to perform a specific task.
A module is a software component or part of a program that contains one or more routines.
That means, functions are groups of code, and modules are groups of classes and functions.
Post a Comment for "What Is The Difference Between Python Function And Python Module"