x

Python Module

PREV    NEXT

1. Module is nothing but categorised larger parts as a smaller parts.
2 .In Python Module is nothing but Python File,Where Functions,Variables,objects and classes are defined
3. By using this we can use the existing things instead of  writing new.


Advantages:

1. By using the module concept we can reuse the modules directly in new applications
2. Same type of attributes we can place in one module

a. predefined modules

Python is having huge set of predefined libraries,few are the examples from there datetime,random,math,os,time,mailbox etc

i. Methods in math module

ii. Constants in Math Module

iii. Method in random module

  • Random Module is used to generate random numbers between interval,this random module having multiple methods to generate.
  • Look in to below are the methods to generate the random numbers

1. Randrange() Function:

This methods we can use in different ways .They are

i. Single argument Randrange() function

This randrange will take the input as single argument.This is used to produces a random integer values less than they specified value as an argument

Syntax:

            random.randrange(stop)

stop:
It is a bounadry value to generate random number

Note:
If the stop will not  zero and negative numbers as an argument,if in case proveide it will gives the valueError

 i. Three argument Randrange() function

This randrange will take the input as three arguments.This is used to produces a random integer values less than they specified value as an argument(start,stop) vales.

Syntax:

            random.randrange(start,stop,step)

start:
It is the start value of the range function and may will include in selection

stop:
It is a bounadry value to generate random number,but will not include in selection

step:
This is the value which we want increment ,But if step not specified any value it will take as zero

Note:
If the stop will not  zero and negative numbers as an argument,if in case proveide it will gives the valueError


b. User defined Modules

In python we can create user defined set of modules also,here we can use python file n ame as a module suppose we order.py,bill.py,collection.py this file names we can use as a modules like order,bill,collection

Example:

Consider the below example ,save the script with the name of “calculator.py”

How Import Modules:

There are multiple ways to import the modules into the python code.They are

1. By using import keyword:

import keyword is used to import a module.

Syntax:

            import module1,module2,module3…..modulen

a. Import predefined modules and using

Example:

Output:

b. Import user defined module

Please consider the above “calculator.py” example

Example:

Output:

2. By using from and import keyword:

by using from and import keyword we can import specific attribute from the module.

Syntax:

from module_name import  attribute1,attribute2………attributen

a. Predefined modules:

Example:

from random import randrange,randint

from math import ceil,floor,pow,sqrt,exp,sin,cos,tan,log

from datetime import datetime

Output:

b. User defined modules:

 Example:

from calculator import sume,subs,mult,divi

Output:

3. By using import complete module:

By using this method we can import all attributes in that module

Syntax:

            from module_name import *

a. Predefined modules:

Example:

from random import *

from math import *

from datetime import *

Output:

b. User defined modules:

Example:

from calculator import *

Output:

PREV    NEXT



Like it? Please Spread the word!