0% found this document useful (0 votes)
37 views11 pages

Modules

This document discusses Python modules and packages. It defines a module as a file containing functions, variables, and classes that can be imported and used in other Python programs. Every Python file acts as a module. It provides examples of importing modules and accessing their contents using the module name, as well as importing specific contents using from ... import. It also discusses the math and random modules, their functions, and how to import and use them. Finally, it defines a package as a collection of related modules grouped together in a folder, with an empty __init__.py file marking it as a Python package.

Uploaded by

Ram Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views11 pages

Modules

This document discusses Python modules and packages. It defines a module as a file containing functions, variables, and classes that can be imported and used in other Python programs. Every Python file acts as a module. It provides examples of importing modules and accessing their contents using the module name, as well as importing specific contents using from ... import. It also discusses the math and random modules, their functions, and how to import and use them. Finally, it defines a package as a collection of related modules grouped together in a folder, with an empty __init__.py file marking it as a Python package.

Uploaded by

Ram Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Modules

A group of functions, variables and classes saved to a file, which is nothing but
module.
Every Python file (.py) acts as a module Test.py
program1.py
x=888

def add(a,b): import program1


print("The Sum:",a+b) print(program1.x)
program1.add(10,20)
def product(a,b): program1.product(10,20)
print("The Product:",a*b)

 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()

• Note: whenever we are using a module in our program, for that module
compiled file will be generated and stored in the hard disk permanently.
Renaming a module at the time of import
(module aliasing):
• Eg: import program1 as m
• here func is original module name and m is alias name. We can access
members by using alias name m

Test.py

import program1 as m
print(m.x)
m.add(10,20)
m.product(10,20)
from ... import:
• We can import particular members of module by using from ... import .
The main advantage of this is we can access members directly without
using module name.

We can import all members of a module as follows from func import *


Eg: from program1 import x,add
print(x)
add(10,20)
product(10,20)==> NameError: name 'product'
from program1 import *
is not defined
print(x)
add(10,20)
product(10,20)
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) from math import *
• 3. floor(x) print(sqrt(4))
• 4. fabs(x) print(ceil(10.1))
• 5.log(x) print(floor(10.1))
• 6. sin(x) print(fabs(-10.6))
• 7. tan(x) print(fabs(10.6))
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

random() function:
This function always generate some float value between 0 and 1 ( not
inclusive) 0<x<1
from random import *
for i in range(10):
print(random())
randint() function:
from random import *
for i in range(10):
print(randint(1,100)) # generate random int value between 1 and 100(inclusive)
Packages
• It is an encapsulation mechanism to group related modules into a
single unit.
• package is nothing but folder or directory which represents collection
of Python modules.
• Any folder or directory contains __init__.py file,is considered as a
Python package.
• This file can be empty.
• A package can contains sub packages also.

You might also like