What are modules in python full explained with examples | Python tutorial lesson - 46
What is a Module?
Consider a module to be the same as a code library. A file
containing a set of functions you want to include in your application. module is
created on a separate page and then we can import that page in our file. The
name of a Python file is the name of the model.
You can name the module file whatever you like, but it must
have the file extension .py
Create a Module
To create a module just save the code you want in a file
with the file extension .py:
Example
Save this code in a file named mod.py
def myf1(name):
print("Hello, My name is : " + name)
Use a Module
Now we can use the module we just created, by using
the import statement in new python
file:
Example
Import the module named mod,
and call the myf1 function:
import mod
mod.myf1("Manoj")
Output:
Hello,
My name is Manoj
Note: When using a function from a module, use
the syntax: module_name.function_name.
Variables in Module
The module can contain functions, as already described, but
also variables of all types (arrays, dictionaries, objects etc):
Example
Save this code in the file mod.py
dic = {
"name": "Manoj",
"age": 40,
"country": "India"
}
Example
Import the module named mob,
and access the the dictionary:
import mod
a = mod.dic["age"]
print(a)
Output:
40
Re-naming a Module (alias)
You can create an alias when you import a module, by using
the as keyword:
Example
Create an alias for mod called m:
import mod as m
a = m.the["age"]
print(a)
Output:
40
Import From Module
You can choose to import only parts from a module, by using
the from keyword.
Example
The module named mod has
one function and one dictionary:
def myf1(name):
print("Hello, My name is: " + name)
dic = {
"name": "Manoj",
"age": 40,
"country": "India"
}
Example
Import only the dic
dictionary from the module:
from mod import dic
print (dic["age"])
Output:
40
Note: When importing using the from
keyword, do not use the module name when
referring to elements inf the module. Example: dic["age"]
, not mod.dic["age"]
Example
The module named mod has
all function and all dictionary:
def myf1(name):
print("Hello, My name is: " + name)
dic = {
"name": "Manoj",
"age": 40,
"country": "India"
}
def myf2(a,b):
print(a+b)
Example
Import the all
dictionary and functions from the mod
module:
from mod import *
print (dic["age"])
mod.myf1("Manoj")
mod.myf2(10,20)
Output:
40
Hello, My name is Manoj
30
Built-in Modules
There are several built-in modules in Python, which you can
import whenever you like.
Example
Import and use the platform module:
import platform
x = platform.system()
print(x)
Output:
Windows
Using the dir() Function
There is a built-in function to list all the function names
(or variable names) in a module. The dir() function:
Example
List all the defined names belonging to the platform module:
import platform
x = dir(platform)
print(x)
Output:
'WIN32_CLIENT_RELEASES',
'WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__',
'__doc__', '__file__', '__loader__', '__name__', '__package __', '__spec__',
'__version__', '_default_architecture', '_dist_try_harder', '_follow_symlinks',
'_ironpython26_sys_version_parser', '_ironpython_sys_version_parser',
'_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version',
'_mac_ver_xml', '_node', '_norm_version', '_perse_release_file', '_platform',
'_platform_cache', '_pypy_sys_version_parser', '_release_filename',
'_release_version', '_supported_dists', '_sys_version', '_sys_version_cache',
'_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver',
'_uname_cache', '_ver_output', 'architecture', 'collections', 'dist',
'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node',
'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build',
'python_compiler', 'python_implementation', 'python_revision',
'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys',
'system', 'system_aliases', 'uname', 'uname_result', 'version', 'warnings',
'win32_ver']
Note: The dir() function can be used on all modules, also the ones you create yourself.
Post a Comment