Showing posts with label Modules in Python. Show all posts
Showing posts with label Modules in Python. Show all posts

Aug 21, 2020

Modules in Python | PYTHON Language | Coding Winds

Modules

A module is a file containing Python definitions and statements. To create modules, you just need to set your coding game and save the files as .py

Look at the code below,


Save it as fibo.py

We have successfully created a module and will use it in our code.

Using modules

Now we can use the module we just created, by using the import statement:

Output here will yield all Fibonacci numbers till 200.

Renaming modules

We can simply rename any module as per our way of usage.

Create an alias for mymodule called mx:

Built-in Modules

Built-in modules are written in C and integrated with the Python interpreter. Each built-in module contains resources for certain system-specific functionalities such as OS management, disk IO, etc. The standard library also contains many Python scripts (with the . py extension) containing useful utilities.

There are several built-in modules in Python, which you can import whenever you like.

Output

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:

Note: The dir() function can be used on all modules, also the ones you create yourself.