0% found this document useful (0 votes)
14 views4 pages

16.python Module

A Python module is a file containing a set of functions and variables that can be included in applications. Modules are created by saving code in a .py file and can be imported using the import statement, allowing access to their functions and variables. Additionally, modules can be renamed using aliases and can be partially imported using the from keyword.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

16.python Module

A Python module is a file containing a set of functions and variables that can be included in applications. Modules are created by saving code in a .py file and can be imported using the import statement, allowing access to their functions and variables. Additionally, modules can be renamed using aliases and can be partially imported using the from keyword.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

TOPIC : Python Module

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.

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 [Link]

def greeting(name):
print("Hello, " + name)
Use a Module

Now we can use the module we just created, by using


the import statement:

Example

Import the module named mymodule, and call the greeting function:

import mymodule

[Link]("Jonathan")
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 [Link]

person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import the module named mymodule, and access the person1
dictionary:
import mymodule

a = mymodule.person1["age"]
print(a)

Naming a Module

You can name the module file whatever you like, but it must have
the file extension .py

Re-naming a Module

You can create an alias when you import a module, by using


the as keyword:

Example

Create an alias for mymodule called mx:


import mymodule as mx
a = mx.person1["age"]
print(a)

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 = [Link]()
print(x)

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)
Import From Module

You can choose to import only parts from a module, by using


the from keyword.
Example

def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import only the person1 dictionary from the module:
from mymodule import person1
print (person1["age"])

You might also like