Python – Modules

After reading this Python modules topic, you will know the user-defined module theory, and examples, and you will understand how to use import keyword and __builtins__ modules.

A Python module is basically a file that contains Python code. The Python standard library contains thousands of functions distributed throughout the modules.

User-defined Module

The user-defined module is written by the user at the time of program writing.

How to create a User-defined Module?

To create a module just write a Python code in a file with file extension as .py:

Example

Program (1): To demonstrate how to create a module in a file with extension as module_name.py in Python.

def accept_int():
    val = int(input("Please enter any integer integer: "))
    print('The integer value is', val)

How to access a User-defined Module?

Now we will access the module that we created earlier, by using the import statement.

Example

Program (1): To demonstrate how to import the module in Python.

import module_name

Output (1)

Please enter any integer integer: 22
The integer value is 22

Explanation: for Output

  • The import statement as import module_name used to access the file named as module_name.py.

Access Standard Functions using import keyword

In Python Programming user place import keyword in the statement with module name to use the corresponding library function. In general, all similar types of library functions are grouped under one module means to say library functions like pow(), sin(), etc. all are used for performing the mathematical operation defined in only math module.

General Form (Syntax):

The syntax to access standard functions using import keyword is given below,

from module import function1_name, function2_name,...,function_name

where, import keyword is used to access the function1_name, function2_name, ….function_name from the module.

The statement in python is given by

from math import sqrt, pow

Explanation:

  • sqrt represent function1_name and pow represent function2_name
  • math is the module.
  • import is a keyword.

Example

Program (1): To demonstrate how to access standard functions using import keyword.

from math import sqrt,pow
# input number the user
num = float(input("Enter number: "))
# Calculate the square root
root = sqrt(num)
# display result
print("Square root of", num, "=", root)

Output (1)

Enter number: 4

Square root of 4.0 = 2.0

Importing the entire module

We can import the entire module in Python in order to call functions present under the same module.

Example

Program (1): To demonstrate how to import the entire module in Python.

import math
print(math.sqrt(4))
print(math.log10(10))

Explanation

  • The import statement as import math makes all of the functions under math module available to the program.
  • math is a module which we want to access.
  • import is a keyword.
  • Two functions such as sqrt and log10 called from the math module.

Output (1)

2.0
1.0

The __builtins__ module

The functions like print, input, int, float, etc which we used many times earlier exist in a module named __builtins__. The __builtins__ module is special because of its functions accessible to any Python program without import keyword required in the statement.

Example

Program (1): To demonstrate how to access standard functions without import keyword.

# display text as python prgramming
print("python prgramming")

Explanation

  • The print function exists in a module named __builtins__ so we can directly access the program.

Output (1)

python programming

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *