Python Modules

In this article, you will learn how to create and download a custom Python module. In addition, there are various techniques for the import and use of the inner module and the Python technology.


Table of Contents

  • What are the modules in Python?
  • How to import modules in Python?
    • Python import statement
    • Import with renaming
    • Python from...import statement
    • Import all names
  • Python Module Search Path
  • Reloading a module
  • The dir() built-in function
What are the modules in python?
The module refers to the files that contain Python's description and description.
For example, in the Python code, for example, for example, it is called modules and name module and will be an example.
We use the software system to suppress large files in smaller and manageable areas. In addition, the code gives the code reuse.
We can determine our most commonly used module and import, rather than the definition of its various programs.
We want to make a module. Write the following as an example.

# Python Module example
def add(a, b):
  """This program adds two
  numbers and return the result"""
   result = a + b
   return result

How to import modules in Python?


We can refer to the process of defining another way or interpersonal interaction with Python.
We use the keyword import tool to do this. To download the pre-defined system of people listed in the Python origin background.

>>> import example

This does not include the names of jobs described in the laboratory directly on the current table. Just enter the name of the module in the module.
By using the module name you can get the time (.) Functionality. For example:

>>> example.add(4,5.5)
9.5

Python import statement:


We can download the module from the import report and find the employer's description using the site as described above. Below is an example.

# import statement example
# to import standard module math
import math
print("The value of pi is", math.pi)

Once the program is implemented, production will be:

The value of pi is 3.141592653589793

Import with renaming:


I can output the module when changing the name in the following way.

# import module by renaming it
import math as m
print("The value of pi is", m.pi)

Python from...import statement:


We can extract the names of specific formats without deleting the full module. Below is an example.

# import only pi from math module
from math import pi
print("The value of pi is", pi)

We have just downloaded the module.
For example, if we use the company. We can produce several of these features.

>>> from math import pi, e
>>> pi
3.141592653589793
>>> e
2.718281828459045

Import all names:


We can generate all the names (definitions) of the module using the following format.

# import all names from the standard module math
from math import *
print("The value of pi is", pi)

Python Module Search Path:


When you download the module, Python searches in several places. English first searches for complex and subsequent (if found) list of advisers identified in your check. Find out in this series.
• Current record.
• PYTHONPATH (variable with a book list).
• The connected installation system.

>>> import sys
>>> sys.path
['',
'C:\\Python33\\Lib\\idlelib',
'C:\\Windows\\system32\\python33.zip',
'C:\\Python33\\DLLs',
'C:\\Python33\\lib',
'C:\\Python33',
'C:\\Python33\\lib\\site-packages']

Reloading a module:


Translation interpreter for Python interpreter only. This makes it useful. Below is a guide to show how this is.
Tell us we have the following code in the module called my_module.

# This module shows the effect of
#  multiple imports and reload
print("This code got executed")

Now we see the impact of the importance.

>>> import my_module
This code got executed
>>> import my_module
>>> import my_module

The dir() built-in function:


We can use the dir () function to find the name of the course in the module.
For example, we will have another function () in the example of the module we started.

>>> dir(example)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__initializing__',
'__loader__',
'__name__',
'__package__',
'add']

Here, we can see a list of names that are separated (accompanied by coordination). All other names are based on the features that come under the characteristics of the standard Python module (we do not know ourselves).
For example, __name__ the attribute contains the name of the module.

>>> import example
>>> example.__name__
'example'


All the names defined in the name, which can now be omitted from the use of dir () for no reason.

>>> a = 1
>>> b = "hello"
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']

To getting expect level training for Python training in your Location – Python training in Chennai | Python training institute in Chennai | Python Course institute in Chennai | Python training in Bangalore | Python training institute in Bangalore Python training in Electronic City Python training in Jaya Nagar | Python training in Pune | Python training institute in Pune | Python training in OMR | Python training in Velachery | Python training in Tambaram Python training in Annanagar | Python training in Marathahalli Python training in Btm | Python Online Training | Online Certification Course Python Course in Chennai Python interview questions and answers | Python tutorials | Python training in Indira Nagar | Python Course in Btm | Python Course in Marathahalli Python training institute in Marathahalli Python course institute in Btm Python course in Electronic City | Python online training in Chennai | Python online training in Bangalore | Python Interview Questions & Answers.

Comments

Post a Comment

Popular posts from this blog

Python – Functions

What is Django in python?