Sitemap
Rajat Singh

Rajat Singh

Modules In Python

--

Press enter or click to view image in full size

The module is nothing but a python(.py) file. Which contain function, class, and variables. It is a stand-alone python program that can be called in other python programs. Basically, it is for the reusebility of a code.

Inbuilt Modules in Python:

1. Math: It is one of the important modules in Python. Basically, this math module contains all the functions which are related to mathematics.

2. Help: It always returns a doc string about the specific module.

3. Random: It is used to generate any random values.

4. OS module: This provides a function to interact with the operating system.

5. sys module: This provides various functions and variables that are used to manipulate different parts of the Python runtime environment.

Get Rajat Singh’s stories in your inbox

Join Medium for free to get updates from this writer.

If you want to know more about a module you can try the help module. help(math)

A different way of importing techniques

  1. import math as mIn this ‘as’ work as an alias
  2. from math import sqrt This imports only the sqrt function from the module
  3. from math import * it will import all the functions from the math module

How to create a custom module:

So creating a module is the same as writing a function! You just have to save this code in a .py file.

return x + y```so when you will save the above code in a .py file this will work as module.

What is a package?

The purpose of the package is to keep all the similar kinds of things at the same place. The package is going to keep all the modules inside it.

Press enter or click to view image in full size

The init.py file will be available by default in a package. If this init.py will be by default available inside any file we call it a package.

You can write something inside init.py or keep it blank.

The file inside a package is known as a sub-package. The sub-packages have modules inside them.
For every package or sub-package, It should have an init.py module.

How to access a particular module inside a package or sub-package?

import robot.design.face as F

What is a library?

A library is nothing but a package after getting published successfully. Example: Numpy, Pandas, and Matplotlib.

--

--