All the IPython Notebooks in Python Functions lecture series by Dr. Milaan Parmar are available @ GitHub
In this class, you will learn to create and import custom modules in Python. Also, you will find different techniques to import and use custom and built-in modules in Python.
Module containing a set of codes or a set of functions which can be included to an application. Modules refer to the Python file, which contains Python code like Python statements, classes, functions, variables, etc. A file with Python code is defined with extension.py
For example: In main.py
, where the main
is the module name.
In Python, large code is divided into small modules. The benefit of modules is, it provides a way to share reusable functions.
A file containing Python code, for example: example.py
, is called a module, and its module name would be example
.
We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code.
We can define our most used functions in a module and import it, instead of copying their definitions into different programs.
The module contains Python code like classes, functions, methods, but it also has variables. A variable can list, tuple, dict, etc.
Let us create a module. Type the following and save it as example.py
.
# Python Module example
>>> def add(a, b):
>>> """This program adds two numbers and return the result"""
>>> result = a + b
>>> return result
Here, we have defined a function add()
inside a module named example
. The function takes in two numbers and returns their sum.
We can import the definitions inside a module to another module or the interactive interpreter in Python.
We use the import
keyword to do this. To import our previously defined module example, we type the following in the Python prompt.
>>> import example
This does not import the names of the functions defined in example
directly in the current symbol table. It only imports the module name example
there.
Using the module name we can access the function using the dot .
operator. For example:
# Example 1:
import example
example.add(3,6.6)
9.6
Python has tons of standard modules. You can check out the full list of Python standard modules and their use cases. These files are in the Lib directory inside the location where you installed Python.
Standard modules can be imported the same way as we import our user-defined modules.
There are various ways to import modules. They are listed below:
# Example 2:
The Python code for a module named a name normally resides in a file main.py. But first create a function in test.py. Here is an example of a simple module,
test.py -
>>> def fun():
>>> print("something here inside fun()")
main.py −
>>> import test
>>> from test import fun
>>> fun()
Output of above example: Run main.py
something here inside fun()
For test.ipynb or test1.ipynb and main.ipynb
Open anaconda prompt and type pip install import-ipynb
see: Downloading import-ipynb-0.1.3.tar.gz (4.0 kB)
test.py or test1.ipynb -
>>> def fun():
>>> print("something here inside fun()")
main.ipynb −
>>> import import_ipynb
>>> import test1
>>> from test1 import fun
>>> fun()
Output of above example: Run main.ipynb
something here inside fun()
# Example 3:
fibo.py -
# Fibonacci numbers module
>>> def fib(n): # return Fibonacci series up to n
>>> result = []
>>> a, b = 0, 1
>>> while b < n:
>>> result.append(b)
>>> a, b = b, a + b
>>> return result
main.py −
>>> import fibo
>>> from fibo import fib
>>> print(fib(100))
Output of above example: Run main.py
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
For test.ipynb or test1.ipynb and main.ipynb
Open anaconda prompt and type pip install import-ipynb
see: Downloading import-ipynb-0.1.3.tar.gz (4.0 kB)
fibo1.ipynb or fibo.py -
# Fibonacci numbers module
>>> def fib(n): # return Fibonacci series up to n
>>> result = []
>>> a, b = 0, 1
>>> while b < n:
>>> result.append(b)
>>> a, b = b, a + b
>>> return result
main.ipynb −
>>> import import_ipynb
>>> import fibo1
>>> from fibo1 import fib
>>> print(fib(100))
Output of above example: Run main.ipynb
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
# Example 4:
equal.py -
#function definition
>>> def equal(a, b):
>>> if(a == b):
>>> return True
main.py −
>>> from equal import *
>>> a, b = 10, 10
>>> print(equal(a, b))
Output of above example: Run main.py
True
For equal.ipynb or equal1.ipynb and main.ipynb
Open anaconda prompt and type pip install import-ipynb
see: Downloading import-ipynb-0.1.3.tar.gz (4.0 kB)
equal.ipynb or equal1.ipynb -
#function definition
>>> def equal(a, b):
>>> if(a == b):
>>> return True
main.ipynb −
>>> import import_ipynb
>>> from equal1 import *
>>> a, b = 10, 10
>>> print(equal(a, b))
Output of above example: Run main.ipynb
True
In Python, each and every built-in module has a large number of predefined functions to perform specific tasks. For instance, Python has a built-in module named operator
, inside which the function called eq()
is defined. This function returns the boolean value of True
if the given two input values are equal. Else, returns False
.
So now, we can make use of this operator module in our main()
program to check if two numbers are equal. This means we would no longer need that equal.py
module.
# Example 5:
#main function
from operator import *
a, b = 10, 10
print(eq(a, b))
True
import
statement¶We can import a module using the import
statement and access the definitions inside it using the dot operator .
as described above. Here is an example.
# Example 1:
# import statement example to import standard module math
import math
print("The value of pi is", math.pi)
The value of pi is 3.141592653589793
# Example 2:
import math
# use math module functions
print(math.sqrt(6)) # Output 2.449489742783178
2.449489742783178
import
multiple modules¶If we want to use more than one module, then we can import multiple modules. This is the simplest form of import
a keyword that we already use in the above example.
# Example 1: Import two modules
import math, random
print(math.factorial(6))
print(random.randint(10, 30))
720 28
from-import
statement¶To import particular classes or functions, we can use the from-import
statement. It is an alternate way to import
. By using this form, we can import individual attributes and methods directly into the program.We can import specific names from a module without importing the module as a whole.
# Example 1: import only factorial function from math module
from math import factorial
print(factorial(6))
720
# Example 2:
from math import pi, e
e
2.718281828459045
# Example 3:
# import only pi from math module
from math import pi
print("The value of pi is", pi)
The value of pi is 3.141592653589793
Here, we imported only the pi
attribute from the math
module.
In such cases, we don't use the dot operator. We can also import multiple attributes as follows:
# Example 4:
from math import pi, e
pi
3.141592653589793
import
with renaming¶We can import a module by renaming it as follows:
# Example 1:
# import module by renaming it
import math as m
print("The value of pi is", m.pi)
The value of pi is 3.141592653589793
We have renamed the math
module as m
. This can save us typing time in some cases.
Note: that the name
math
is not recognized in our scope. Hence,math.pi
is invalid, andm.pi
is the correct implementation.
# Example 2: Import a module by renaming it
import random as rand
print(rand.randrange(10, 20, 2))
12
# Example 3: import a method by renaming it
# rename randint as random_number
from random import randint as random_number
# Gives any random number from range(10, 50)
print(random_number(30, 60))
49
import
all names¶We can import
all functions and attributes of a specific module, then instead of writing all function names and attribute names, we can import all using an asterisk *
.
# Example 1:
# import all names from the standard module math
from math import *
print("The value of pi is", pi)
The value of pi is 3.141592653589793
Here, we have imported all the definitions from the math
module. This includes all names visible in our scope except those beginning with an underscore(private definitions).
Importing everything with the asterisk *
symbol is not a good programming practice. This can lead to duplicate definitions for an identifier. It also hampers the readability of our code.
# Example 2:
from math import *
print(pow(6,3))
print(factorial(6))
print(pi*3)
print(sqrt(100))
216.0 720 9.42477796076938 10.0
While importing a module, Python looks at several places. Interpreter first looks for a built-in module. Then(if built-in module not found), Python looks into a list of directories defined in sys.path
. The search is in this order.
PYTHONPATH
(an environment variable with a list of directories).import sys
sys.path
['C:\\Users\\Deepak\\01_Learn_Python4Data\\04_Python_Functions', 'C:\\ProgramData\\Anaconda3\\python38.zip', 'C:\\ProgramData\\Anaconda3\\DLLs', 'C:\\ProgramData\\Anaconda3\\lib', 'C:\\ProgramData\\Anaconda3', '', 'C:\\Users\\Deepak\\AppData\\Roaming\\Python\\Python38\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\locket-0.2.1-py3.8.egg', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\Deepak\\.ipython']
We can add and modify this list to add our own path.
The Python interpreter imports a module only once during a session. This makes things more efficient. Foe example:
# Example 1:
Suppose we have the following code in a module named my_module
.
# This module shows the effect of
# multiple imports and reload
print("This code got executed")
import my_module
This code got executed
We can see that our code got executed only once. This goes to say that our module was imported only once.
Now if our module changed during the course of the program, we would have to reload it.One way to do this is to restart the interpreter. But this does not help much.
Python provides a more efficient way of doing this. We can use the reload()
function inside the imp
module to reload a module. We can do it in the following ways:
import imp
import my_module
import my_module
imp.reload(my_module)
This code got executed
<module 'my_module' from 'C:\\Users\\Deepak\\01_Learn_Python4Data\\04_Python_Functions\\my_module.py'>
# Example 2:
First, create a Python module with the name greet_module.pynb
and write the below code in that file.
>>> print("Welcome to Dr Parmar's Python4DataScience class")
Now, create a Python file with the name, main.py and write the below code in it and import the module test_module.py. See the following code.
>>> import import_ipynb
>>> import time
>>> from importlib import reload
>>> # load 1st time
>>> import test_module
>>> time.sleep(25)
>>> # reload
>>> reload(test_module)
>>> time.sleep(25)
>>> # reload again
>>> reload(test_module)
>>> print("This is test file..")
Output of above example: Run main.py
Welcome to Dr Parmar's Python4DataScience class
Welcome to Dr Parmar's Python4DataScience class
Welcome to Dr Parmar's Python4DataScience class
This is test file..
dir()
built-in function¶We can use the dir()
function to find out names that are defined inside a module. When we use this function with any object (an object can be sequence like list, tuple, set, dict or can be class, function, module, etc. ), it returns properties, attributes, and method.
For example, we have defined a function add()
in the module example that we had in the beginning.
We can use dir in example module in the following way:
# Example 1:
dir(example)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add']
Here, we can see a sorted list of names (along with add
). All other names that begin with an underscore are default Python attributes associated with the module (not user-defined).
For example, the __name__
attribute contains the name of the module.
# Example 2:
import example
example.__name__
'example'
All the names defined in our current namespace can be found out using the dir()
function without any arguments.
# Example 3:
a = 1
b = "hello"
import math
dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']
['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']
# Example 4:
import math
print(dir(math))
['__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', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
Using Python built-in function help()
, we can obtain the list of built-in modules available in Python. On executing the line help ('modules') in Python IDE, you can see all the Python built-in modules. Some of the frequently used ones are discussed below.
Module | Description |
---|---|
Operator |
This module provides a set of pre-defined functions corresponding to operators in Python. |
decimal |
This module is used to print the complete decimal value when one number is divided by another number. |
random | random module is used to generate random numbers. Some of the pre-defined functions of this module are randint() , choice() , uniform , etc. |
string |
string module provides a set of functions that are used to perform certain operations on characters. This module has pre-defined functions like capwords, ascii_letters, etc. |
math | math module is used to perform mathematical operations. This module provides some pre-defined mathematical functions like sqrt, factorial, etc. |
os
Module¶Using python os
module it is possible to automatically perform many operating system tasks. The os
module in Python provides functions for creating, changing current working directory, and removing a directory (folder), fetching its contents, changing and identifying the current directory.
# Example 1:
# import the module
import os
# Creating a directory
os.mkdir('directory_name')
# Changing the current directory
os.chdir('path')
# Getting current working directory
os.getcwd()
# Removing directory
os.rmdir()
--------------------------------------------------------------------------- FileExistsError Traceback (most recent call last) <ipython-input-23-5aa728677edf> in <module> 4 import os 5 # Creating a directory ----> 6 os.mkdir('directory_name') 7 # Changing the current directory 8 os.chdir('path') FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'directory_name'
sys.argv
function returns a list of command line arguments passed to a Python script. The item at index 0 in this list is always the name of the script, at index 1 is the argument passed from the command line.
import sys
sys.path
['C:\\Users\\Deepak\\01_Learn_Python4Data\\04_Python_Functions', 'C:\\ProgramData\\Anaconda3\\python38.zip', 'C:\\ProgramData\\Anaconda3\\DLLs', 'C:\\ProgramData\\Anaconda3\\lib', 'C:\\ProgramData\\Anaconda3', '', 'C:\\Users\\Deepak\\AppData\\Roaming\\Python\\Python38\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\locket-0.2.1-py3.8.egg', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\Deepak\\.ipython']
First, create a Python module with the name script.py
and write the below code in that file.
>>> import sys
>>> # print(sys.argv[0], argv[1],sys.argv[2]) # this line would print out: filename argument1 argument2
>>> print('Welcome {}. Enjoy {} for Data Science ^_^'.format(sys.argv[1], sys.argv[2]))
Second, open Anaconda Prompt (Anaconda3)
Third, type system path after cd
sy
cd C:\\Users\\Deepak\\01_Learn_Python4Data\\04_Python_Functions
Forth, type
sy
python script.py Milaan Python
Output:
sy
Welcome Milaan. Enjoy Python for Data Science ^_^
Some useful sys
commands to type in:
# to exit sys
sys.exit()
# To know the largest integer variable it takes
sys.maxsize
# To know environment path
sys.path
# To know the version of python you are using
sys.version
operator
Module¶# Example 1:
from operator import *
a, b = 10, 20
#prints the product of the values 'a' and 'b'
print(mul(a, b))
#prints True if the value of 'a' is greater than 'b'. Else, False
print(gt(a, b))
#prints the remainder value, when the value of 'a' is divided by 'b'
print(mod(a, b))
#concatenates and prints the given two strings
print(concat("Hello ", "World"))
200 False 10 Hello World
# Example 1:
from string import *
print(capwords("hello world")) #capitalizes the first letter of each words
print(ascii_letters) #prints all lowercase and uppercase letters
Hello World abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
# Example 2:
import string
print(string.ascii_letters) # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
print(string.digits) # 0123456789
print(string.punctuation) # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
decimal
Module¶# Example 1:
from decimal import *
a, b = 10, 3
c = a / b
print(c)
print(Decimal(c)) #prints the complete decimal value of c
3.3333333333333335 3.333333333333333481363069950020872056484222412109375
random
Module¶The random module is used to generate the random numbers. It provides the following two built in functions:
Function | Description |
---|---|
random() |
It returns a random number between 0.0 and 1.0 where 1.0 is exclusive. |
randint(x,y) |
It returns a random number between x and y where both the numbers are inclusive. |
# Example 1:
from random import *
print(randint(10, 20)) #prints a random number between the given range
list1 = [30, 23, 45, 16, 89, 56]
print(choice(list1)) #prints a random element from the given iterator
print(uniform(10, 20)) #prints a random float number between two given values
10 16 13.03505598723366
# Example 2:
import random
print(random.random())
print(random.randint(2,8))
0.2946014910624595 8
# Example 3:
from random import randint # import randint function
print(random()) # it doesn't take any arguments; it returns a value between 0 and 0.9999
# call randint function to get random number
print(randint(10, 30)) # it returns a random integer number between [10, 30] inclusive
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-31-92fab552c144> in <module> 3 from random import randint # import randint function 4 ----> 5 print(random()) # it doesn't take any arguments; it returns a value between 0 and 0.9999 6 7 # call randint function to get random number TypeError: 'module' object is not callable
math
Module¶Using math module, you can use different built in mathematical functions.
Function | Description |
---|---|
ceil(n) |
It returns the next integer number of the given number. |
sqrt(n) |
It returns the Square root of the given number. |
exp(n) |
It returns the natural logarithm e raised to the given number. |
floor(n) |
It returns the previous integer number of the given number. |
log(n,baseto) |
It returns the previous integer number of the given number. |
pow(baseto, exp) |
It returns baseto raised to the exp power. |
sin(n) |
It returns sine of the given radian. |
cos(n) |
It returns cosine of the given radian. |
tan(n) |
It returns tangent of the given radian. |
# Example 1:
from math import *
print(sqrt(16)) # prints the square root of the value 16 in the form of a floating-point value
print(factorial(5)) # prints the factorial of the value 5
4.0 120
# Example 2:
import math
a=4.6
print(math.ceil(a)) # 5, rounding to the highest
print(math.floor(a)) # 4, rounding to the lowest
b=9
print(math.sqrt(b)) # 3.0, square root
print(math.exp(3.0)) # 20.085536923187668
print(math.log(2.0)) # 0.6931471805599453
print(math.pow(2.0,3.0)) # 8.0, exponential function
print(math.sin(0)) # 0.0
print(math.cos(0)) # 1.0
print (math.tan(45)) # 1.6197751905438615
print(math.log10(100)) # 2.0, logarithm with 10 as base
5 4 3.0 20.085536923187668 0.6931471805599453 8.0 0.0 1.0 1.6197751905438615 2.0
The math module provides two constants for mathematical Operations:
math.pi
: Returns constant Pi = 3.14159...math.e
: Returns constant e= 2.71828...# Example 3:
import math
print("math.pi : ",math.pi) # 3.141592653589793, pi constant
print("math.e : ",math.e) # 2.718281828459045, e constant
math.pi : 3.141592653589793 math.e : 2.718281828459045
statistics
Module¶The statistics module provides functions for mathematical statistics of numeric data. The popular statistical functions which are defined in this module: mean
, median
, mode
, stdev
etc.
# Example 1:
from statistics import * # importing all the statistics modules
ages = [20, 20, 4, 24, 25, 22, 26, 20, 23, 22, 26]
print(mean(ages)) # ~22.9
print(median(ages)) # 23
print(mode(ages)) # 20
print(stdev(ages)) # ~2.3
21.09090909090909 22 20 6.106628291529549
Since Python provides a lot of built-in modules, it is advisable to use built-in modules rather than user-created modules to perform basic operations.
Writ a function which generates a six digit/character random_user_id
.
print(random_user_id()); '1ee33d'
Modify the previous task. Declare a function named user_id_gen_by_user
. It doesn’t take any parameters but it takes two inputs using input()
. One of the inputs is the number of characters and the second input is the number of IDs which are supposed to be generated.
print(user_id_gen_by_user()) # user input: 5 5
```
- ```py
print(user_id_gen_by_user()) # 16 5
```
Write a function named rgb_color_gen
. It will generate rgb colors (3 values ranging from 0 to 255 each).
print(rgb_color_gen())
```
Write a function list_of_hexa_colors
which returns any number of hexadecimal colors in an array (six hexadecimal numbers written after #
. Hexadecimal numeral system is made out of 16 symbols, 0-9 and first 6 letters of the alphabet, a-f. Check the task 6 for output examples).
Write a function list_of_rgb_colors
which returns any number of RGB colors in an array.
Write a function generate_colors
which can generate any number of hexa or rgb colors.
generate_colors('hexa', 3) # ['#a3e12f','#03ed55','#eb3d2b'] generate_colors('hexa', 1) # ['#b334ef'] generate_colors('rgb', 3) # ['rgb(5, 55, 175','rgb(50, 105, 100','rgb(15, 26, 80'] generate_colors('rgb', 1) # ['rgb(33,79, 176)']
shuffle_list
, it takes a list as a parameter and it returns a shuffled list