Python Modules

In this article, we are about to learn all about Python Modules.

Python Modules is nothing but a file which contains all the  Python statements and definitions as well. Every python file(.py) acts as a module.

If you want to learn how to create your own module follow this one – How to create your own package in python

Python Modules

Modules can be considered as “namespaces” which have a collection of objects which you can use when needed.

Every program you execute directly is treated as a module with a special name __main__.




So, all the variables you define, the functions you create are said to live in the namespace of __main__.

When you say the following, you are making the namespace of
math available to you.

import math

math.object

import math
type(math)

First method-

import math
print("Radians=",math.radians(45)) # no math.radians required
print("Sin value=",math.sin(30))
print("Cos value=",math.cos(30))
print("Tan value=",math.tan(45))

Output-

Radians= 0.7853981633974483
Sin value= -0.9880316240928618
Cos value= 0.15425144988758405
Tan value= 1.6197751905438615

Second Method-

using from …..import

We can import particular members of module by using from…import.

The main advantage of this we can access members directly without using module.

from math import *
print("Radians=",radians(45)) # no math.radians required
print("Sin value=",sin(30))
print("Cos value=",cos(30))
print("Tan value=",tan(45))

Output-

Radians= 0.7853981633974483
Sin value= -0.9880316240928618
Cos value= 0.15425144988758405
Tan value= 1.6197751905438615

Various possibilities of import-

  1. import modulename
  2. from module import member
  3. import module1,module2,module3
  4. from module import member1,member2,member3
  5. import module1 as m
  6. from module import member1 as x
  7. import module1 as m1,module2 as m2,module3
  8. from module import *

Above all are various possibilities to import.

Multiple methods-

from math import sin,cos,tan
#print("Radians=",radians(45)) 
print("Sin value=",sin(60))
print("Cos value=",cos(90))
print("Tan value=",tan(45))

Output-

Sin value= -0.3048106211022167
Cos value= -0.4480736161291701
Tan value= 1.6197751905438615

Built-in Modules-

  1. sys – contains tools for system arguments, OS information etc.
  2. os – for handling files, directories, executing external programs
  3. re – for parsing regular expressions
  4. datetime – for date and time conversions etc.
  5. pickle – for object serialization
  6. csv – for reading CSV table

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()
2.randint()
3.uniform()
4.randrange(start,stop,step)
5.choice()

random()-

This function always generate some float values between 0 and 1.

from random import *
for i in range(5):
    print(random())

Output-

0.01719359039286339
0.5699492665633483
0.6863463358397502
0.0032779620866966175
0.15328229146691108

randint()-

To generate random integer between 2 given numbers

from random import *
for i in range(5):
    print(randint(1,100))
    

Output-

37
3
73
37
50

uniform()-

It returns random float values between 2 given numbers.

from random import *
for i in range(5):
    print(uniform(1,10))

Output-

6.8708617228883355
7.8060221098034654
6.502421149821796
4.2188066146610606
2.9836607222793443

randrange(start,stop,step)-

Returns a random number from range.

from random import *
for i in range(5):
    print(randrange(10,50,2))

Output-

24
20
36
26
36

choice()-

It won’t return random number. It will return a random object from the given list or tuple.

from random import *
list=["sun","moon","fire","pearl","black"]
for i in range(10):
    print(choice(list))

Output-

black
black
sun
sun
sun
fire
black
fire
pearl
pearl

 


Leave a Reply

Your email address will not be published. Required fields are marked *