Python Modules

Python Modules, What is Module?, Benefits of Modules, Types of Modules, Built-in Modules, User-defined Modules, and how to use modules.

Python Step by Step Videos, Python Installation, Python Language Syntax, Python Language Fundamentals, and Python Object-Oriented Programming.

Python Language Modules

1. What is Python Module?
2. Purpose of Modules
3. Types of Modules
4. How to use Modules
5. User-defined Modules in Python

1. What is Python Module?

• A module is a file consisting of Python code. It can define functions, classes, and variables, and can also include runnable code. Any Python file can be referenced as a module.

• A file containing Python code, for example, test.py, is called a module, and its name would be tested..

Module vs. Function

Function: it’s a block of code that you can use/reuse by calling it with a keyword. Eg. print() is a function.

Module: it’s a .py file that contains a list of functions (it can also contain variables and classes). Eg. in statistics.mean(a), mean is a function that is found in the statistics module.

2. Purpose of Modules

• As our program grows more in the size we may want to split it into several files for easier maintenance as well as reusability of the code. The solution to this is Modules.

• We can define your most-used functions in a module and import it, instead of copying their definitions into different programs. A module can be imported by another program to make use of its functionality. This is how you can use the Python standard library as well.

3. Types of Modules

Python provides us with some built-in modules, which can be imported by using the “import” keyword.

• Python also allows us to create your own modules and use them in your programs.

4. How to use 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.

>>> help(‘modules’)

There is a Python Standard Library with dozens of built-in modules. From those, five important modules,

random, statistics, math, datetime, csv

Python math module

This contains factorial, power, and logarithmic functions, but also some trigonometry and constants.

i. import math

And then:

math.factorial(5)
math.pi
math.sqrt(5)
math.log(256, 2)

ii. import math as m

And then:

m.factorial(5)
m.pi
m.sqrt(5)
m.log(256, 2)

iii. from math import factorial

Here, we first call the from a keyword, then math for the module. Next, we use the import keyword and call the specific function we would like to use.

print (factorial(5))

iv. from math import *

print(factorial(5))
print(pi)
print(sqrt(5)
print(log(256, 2))

Python datetime module

import datetime

today = datetime.date.today()
print(“Today is: “, today)

print (‘This Year: ‘, today.year)
print (‘This Month: ‘, today.month)
print (‘This Week Day: ‘, today.day)

Python os module

This module has functions to perform many tasks of operating system.

i. mkdir():

We can create a new directory using mkdir() function from os module.

import os
os.mkdir(“C:\\Users\\gcreddy\\Desktop\Python”)

ii. getcwd():
This function in returns name off current working directory.

import os
print (os.getcwd())

iii. chdir():
To change current working directory to use chdir() function.

import os
print (os.getcwd())

os.chdir(“D:\\Dump”)

print (os.getcwd())

iv. rmdir():

The rmdir() function in os module removes a specified directory, However it should not be the current working directory and it should be empty.

import os
os.rmdir(“C:\\Users\\gcreddy\\Desktop\\Python”)

v. listdir():

The os module has listdir() function which returns list of all files in specified directory.

import os
print (os.listdir(“C:\\Users\\gcreddy\\Desktop”))

Python random module

i. random() 

random() function returns a random float number between 0.0 to 1.0. The function doesn’t need any arguments

import random
print(random.random()) # 0.755173688207591

ii. randint()

randint(): Returns a random integer between the specified integers

import random
print(random.randint(1,100)) # 58

5. User-defined Modules in Python

i. Create a module

# A simple module, calc.py

price=1000

def add(x, y):
return (x+y)

def sub(x, y):
return (x-y)

def mul(x, y):
return (x*y)

ii. Use Module

# importing module calc.py

import calc

print calc.add(10, 2)

# from calc import mul

print(add(10, 2))


 

Follow me on social media: