Python Modules

 

Python Modules

Python modules are python files that contain python code that we can use within our python files ensuring simplicity and code reusability.

 

Here are some popular python built-in modules:


csv, datetime, json, math, random, sqlite3, statistics, tkinter, turtle, etc.

 

Example: importing math module:


import math

print("Sin(0) =", math.sin(0))
print("Sin(30) =", math.sin(math.pi/6))
print("Sin(45) =", math.sin(math.pi/4))
print("Sin(60) =", math.sin(math.pi/3))
print("Sin(90) =", math.sin(math.pi/2))


Output:

Sin(0) = 0.0
Sin(30) = 0.49999999999999994
Sin(45) = 0.7071067811865476
Sin(60) = 0.8660254037844386
Sin(90) = 1.0

Apart from this we can create our own modules and use them within other python files.

 

A. Creating and using module:

Write code and save file with extension .py .

 

Example: creating file module.py and writing some code:

def add(a, b):
    return (a+b)

def sub(a, b):
    return (a-b)

def mul(a, b):
    return (a*b)

def div(a, b):
    return (a/b)

 

Now we can use this code in a different python file by simply using the import statement.


Example: creating file calc.py and importing module.py

import module 

 

Now we can write code to call the functions from module.py in calc.py


Example:

import module 

num1 = int(input("Enter first number:"))
num2 = int(input("Enter second number:"))

print("Addition", module.add(num1, num2))
print("Subtraction", module.sub(num1, num2))
print("Multiplication", module.mul(num1, num2))
print("Division", module.div(num1, num2))


Output:

Enter first number:6
Enter second number:2
Addition 8
Subtraction 4
Multiplication 12
Division 3.0

 

B. Using an alias:

We can also use alias while importing module. This way we do not need to write the whole name of the module while calling it. 


Example:

import math as m

print("Square Root of 36 :", m.sqrt(36))
print("3 to the power of 4 :", m.pow(3,4))

Output:

Square Root of 36 : 6.0
3 to the power of 4 : 81.0

 

C. import from module:

You can also import specific parts that you need from a module instead of importing the entire module.

 

Example: creating file module.py and writing some code:

def add(a, b):
    return (a+b)

def sub(a, b):
    return (a-b)

def mul(a, b):
    return (a*b)

def div(a, b):
    return (a/b)

 

Example: now we will only import add() and sub() function into our calc.py file.

from module import add, sub 

num1 = int(input("Enter first number:"))
num2 = int(input("Enter second number:"))

print("Addition", add(num1, num2))
print("Subtraction", sub(num1, num2))
print("Multiplication", mul(num1, num2))
print("Division", div(num1, num2))


Output:

Enter first number:12
Enter second number:4
Addition 16
Subtraction 8
Traceback (most recent call last):
  File "d:\Python\Codes\calc.py", line 9, in <module>
    print("Multiplication", mul(num1, num2))
NameError: name 'mul' is not defined


As we can see that, we have only imported add() and sub() functions from module.py file. Hence we get output for these functions, but we get error if we use any other functions.

 

But what if want to import everything from a module but we do not need to prefix module name again and again?


Use an asterisk(*) while importing.

 

Example:

from module import *

num1 = int(input("Enter first number:"))
num2 = int(input("Enter second number:"))

print("Addition", add(num1, num2))
print("Subtraction", sub(num1, num2))
print("Multiplication", mul(num1, num2))
print("Division", div(num1, num2))


Output:

Enter first number:12
Enter second number:4
Addition 16
Subtraction 8
Multiplication 48
Division 3.0

As we can see, by using an asterisk(*), we can directly call the functions from the imported module without prefixing it with module name.


D. dir() function:

The dir() function lists all the function names (or variable names) in a module.


Example:

from operator import mod
import math

lst1 = dir(math)
print(lst1)

Output:

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']