DEV Community

Ajith R
Ajith R

Posted on

What is python🐍modules 🤔

A module is simply Python file with a .py a extension that can be imported inside another Python program.

In Python, there are two types of modules.
**1. Built-in Modules

  1. User-defined Modules**

Built-in Modules
Built-in modules come with default Python installation.
Some commonly used Python built-in modules are datetime, os, math, sys, random, etc.

User Defined Modules
The modules which the user defines or create are called a user-defined module.
We can create our own module, which contains classes, functions, variables, etc., as per our requirements.

How to import Modules?

The import statement is used to import the whole module.

import math

You can import multiple modules by seperating a comma between them.

# Import more than 1 modules
import math, random

*Create Modules in Python
*

To create a module, write Python code in the file, and save that file with the.py extension.
Now you can use functions defined in your file in another file by just importing module and not writing whole code again.

See all functions of any module at once.

dir() is a built-in function. This function is used to list all members of the current module.

import math
print(dir(math))
['__doc__', '__loader___', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma' 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'loglp', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trung 1

Top comments (0)