Ticker

6/recent/ticker-posts

Python: Modules - math Modules, random Modules

A group of functions, variables, and classes is saved to a file, which is nothing but a module.

Every Python file (.py) acts as a module

Eg: durgamath.py 

1) x=888 
2) 
3) def add(a,b): 
4) print("The Sum:",a+b) 
5) 
6) def product(a,b): 
7) print("The Product:",a*b) 

durgamath module contains one variable and 2 functions.

If we want to use members of module in our program then we should import that module.

import modulename

We can access members by using module name.
modulename.variable
modulename.function()

Renaming a module at the time of import (module aliasing):

Eg:

import durgamath as m

here durgamath is original module name and m is alias name.

We can access members by using alias name m

test.py:

1) import durgamath as m 
2) print(m.x) 
3) m.add(10,20) 
4) m.product(10,20) 

Various possibilties of import:

import modulename
import module1,module2,module3
import module1 as m
import module1 as m1,module2 as m2,module3
from module import member
from module import member1,member2,memebr3
from module import memeber1 as x
from module import *

member aliasing:

from durgamath import x as y,add as sum
print(y)
sum(10,20)

Once we defined as alias name,we should use alias name only and we should not use original name

Eg:

from durgamath import x as y
print(x)==>NameError: name 'x' is not defined

Reloading a Module:

By default module will be loaded only once eventhough we are importing multiple multiple times.

Demo Program for module reloading:

1) import time 
2) from imp import reload 
3) import module1 
4) time.sleep(30) 
5) reload(module1) 
6) time.sleep(30) 
7) reload(module1) 
8) print("This is test file")

module1.py:

print("This is from module1")

test.py

1) import module1 
2) import module1 
3) import module1 
4) import module1 
5) print("This is test module") 
6) 
7) Output
8) This is from module1 
9) This is test module 

In the above program test module will be loaded only once eventhough we are importing multiple times.

The problem in this approach is after loading a module if it is updated outside then updated version of module1 is not available to our program.

We can solve this problem by reloading module explicitly based on our requirement.

We can reload by using reload() function of imp module.

Finding members of module by using dir() function:

Python provides inbuilt function dir() to list out all members of current module or a specified module.

dir() ===>To list out all members of current module
dir(moduleName)==>To list out all members of specified module

Working with math module:

Python provides inbuilt module math.

This module defines several functions which can be used for mathematical operations.

The main important functions are

  1. sqrt(x)
  2. ceil(x)
  3. floor(x)
  4. fabs(x)
  5. log(x)
  6. sin(x)
  7. tan(x)

Eg:

1) from math import * 
2) print(sqrt(4)) 
3) print(ceil(10.1)) 
4) print(floor(10.1)) 
5) print(fabs(-10.6)) 
6) print(fabs(10.6)) 
7) 
8) Output
9) 2.0 
10) 11 
11) 10 
12) 10.6 
13) 10.6

Eg:

import math
help(math)

Working with random module:

This module defines several functions to generate random numbers.

We can use these functions while developing games,in cryptography and to generate random numbers on fly for authentication.

1. random() function:

 This function always generate some float value between 0 and 1 ( not inclusive)

 0<x<1

Eg:

1) from random import * 
2) for i in range(10): 
3) print(random()) 
4) 
5) Output
6) 0.4572685609302056 
7) 0.6584325233197768 
8) 0.15444034016553587 
9) 0.18351427005232201 
10) 0.1330257265904884 
11) 0.9291139798071045 
12) 0.6586741197891783 
13) 0.8901649834019002 
14) 0.25540891083913053 
15) 0.7290504335962871 

2. randint() function:

To generate random integer beween two given numbers(inclusive)

Eg:

1) from random import * 
2) for i in range(10): 
3) print(randint(1,100)) # generate random int value between 1 and 100(inclusive) 
4) 
5) Output
6) 51 
7) 44 
8) 39 
9) 70 
10) 49 
11) 74 
12) 52 
13) 10 
14) 40 
15) 8 

3. uniform():

 It returns random float values between 2 given numbers(not inclusive)

Eg:

1) from random import * 
2) for i in range(10): 
3) print(uniform(1,10)) 
4) 
5) Output
6) 9.787695398230332 
7) 6.81102218793548 
8) 8.068672144377329 
9) 8.567976357239834 
10) 6.363511674803802 
11) 2.176137584071641 
12) 4.822867939432386 
13) 6.0801725149678445 
14) 7.508457735544763 
15) 1.9982221862917555 

random() ===>in between 0 and 1 (not inclusive)

randint(x,y) ==>in between x and y ( inclusive)

uniform(x,y) ==> in between x and y ( not inclusive)

4. randrange([start],stop,[step])

 returns a random number from range

 start<= x < stop
 start argument is optional and default value is 0
 step argument is optional and default value is 1
randrange(10)-->generates a number from 0 to 9
randrange(1,11)-->generates a number from 1 to 10
randrange(1,11,2)-->generates a number from 1,3,5,7,9

Eg 1:

1) from random import * 
2) for i in range(10): 
3) print(randrange(10)) 
4) 
5) Output
6) 9 
7) 4 
8) 0 
9) 2 
10) 9 
11) 4 
12) 8 
13) 9 
14) 5 
15) 9

5. choice() function:

It wont return random number.

It will return a random object from the given list or tuple.

Eg:

1) from random import * 
2) list=["Sunny","Bunny","Chinny","Vinny","pinny"] 
3) for i in range(10): 
4) print(choice(list)) 
Output
Bunny 
pinny 
Bunny 
Sunny 
Bunny 
pinny 
pinny 
Vinny 
Bunny 
Sunny


"Python Modules Math"

"Python Math Module List"

"Python Math Module Example"

"Python Math Module Random"

"Random Module Methods In Python"

"Python Random Modules"