Modules and Packages (24!12!2022)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

What are Modules?

In [ ]: --> Collections of classes , variables and functions is called a modules.


--> Each and Every file in Python is a Module.
--> Each and Every file that is having .py extension is a module.

What are Packages?


In [ ]: Collections of modules is known as a Package.(Folder that is having n number of Module).

Types of Modules
In [ ]: Two Types of Modules:

Builtin Modules -->That modules which we can directly use by importing it. There is no need
to define that module.Internally Pvm knows the variables/functions/classes that are present in
that module.

Example:
math module
random module

User Defined Modules --> that modules which are prepared by the developer as per the
business requirement.
Example:
addf
subt

About Import Module?


In [ ]: --> If we want to import any Python File(Module) to my current File then we need to use import keyword.
--> import keyword to make code in one module available in another.
--> Imports in Python are important for structuring your code effectively

Syntax:
import Module_Name

Syntax for Calling any Method/Variable of that Module:


Module_name.FunctionName/Variable_Name

Few important Builtin Module

Help Function
In [ ]: Help is a function that is used to give the description of a module

Math Module
In [15]: import math
#print(help(math))
print(math.sqrt(144)) #math.sqrt() is used to return square root of a number
print(math.factorial(5)) #math.factorial() is used to return the factorial of a number
print(math.sin(200))
print(math.tanh(200))
print(math.pi) #math.pi is used to return the pi value
print(math.ceil(5.4)) #math.ceil() is used to return the ceil value
print(math.floor(6.5)) #math.floor() is used to return the floor value

12.0
120
-0.8732972972139946
1.0
3.141592653589793
6
6

Random Module
In [ ]: In Random Module Three Functions are Very Important:
1.random.random()
2.random.randint()
3.random.uniform()

random.random() Function
In [ ]: random.random() --> Function is used to generate a random number from 0 to 1

In [20]: import random


#print(help(random))
for i in range(10):
print(random.random()) #random number between 0 to 1

0.8416114160074224
0.8294094026760904
0.7685731903672596
0.21528987083822326
0.49567720618502364
0.5204319481599796
0.504787493191886
0.014157233756902698
0.34142647030943163
0.7745919869020532

random.randint()
In [ ]: random.randint(start,end) --> Function is used to return a random number between a given range.
--> It is always expecting Two Arguments(start and end)

In [36]: for i in range(5):


print(random.randint(1,200))

61
138
20
12
149

In [2]: import random


for i in range(5):
print(random.randint(999,9999))

1367
6619
4028
8524
7026

In [44]: x=random.randint(999,9999)
print(x)
y=int(input("Enter OTP"))
if x==y:
print("Successfully Logged In")
else:
print("Invalid OTP")

8180
Enter OTP99909
Invalid OTP

random.uniform() Function
In [ ]: random.uniform(start,end) --> Function is used to return a random floating number between a given
range.
--> It is always expecting two arguments to be passed(start,end)

In [3]: #uniform function --> return random number within a given range(floating number)
for i in range(10):
print(random.uniform(1,200))

70.01275628183346
144.11944035004856
184.36375642622662
48.615149521991526
31.0108295962854
101.71892255984008
153.53749459066776
25.38816577813439
155.46502401635854
53.661418316957594

User Defined module


In [ ]: --> Create any file with variables/functions/classes and the extension of the file must be .py only.
--> After creating a python file open a new Python file/notebook and import the previous python file
--> With the help of import statement you can use these functions variables and classes of one file
into another.

Example:
In [52]: import info
print(help(info))
print(info.college_name)
print(info.Calculater_info(20,40))
print(info.wish())

Help on module info:

NAME
info

FUNCTIONS
Calculater_info(x, y)

wish()

DATA
college_name = 'Edyoda Digital university'

FILE
c:\users\praty\info.py

None
Edyoda Digital university
Addition is 60
Subtraction is -20
Division is 0
Modulus is 20
Exponent is 10995116277760000000000000000000000000000000000000000

Hello How are You


Hope Everything is Going well
None

In [55]: import info


import importlib
importlib.reload(info)
print(help(info))
print(info.college_name)
print(info.Calculater_info(20,40))
print(info.wish())
print(info.welcome("name","Batch"))

Help on module info:

NAME
info

FUNCTIONS
Calculater_info(x, y)

welcome(name, Batch)

wish()

DATA
Batch = 'DS290922A'
college_name = 'Edyoda Digital university'
name = 'Jappan Singh Anand'

FILE
c:\users\praty\info.py

None
Edyoda Digital university
Addition is 60
Subtraction is -20
Division is 0
Modulus is 20
Exponent is 10995116277760000000000000000000000000000000000000000

Hello How are You


Hope Everything is Going well
None
Hi nameWelcome to edyoda

In [62]: import salary


#print(help(salary))
print(salary.company)
#print(salary.Employee())
print(salary.salary_Grade())

Edyoda Digital University


Enter your salary(In Lakhs):20
You are a Boss

Advantage of Modules is Code Reuseability


In [ ]: --> That means if you are creating one module then you can use the functionality and variables
of that modules in any other file.

User Defined Packages


In [ ]: Packages --> collections of that modules which are prepared by the developer as per the
business requirement.
Syntax:
import package_name.module_name

Example:
In [67]: import Python.info
print(Python.info.college_name)
print(Python.info.Calculater_info(10,20))
print(Python.info.wish())

Edyoda Digital university


Addition is 30
Subtraction is -10
Division is 0
Modulus is 10
Exponent is 100000000000000000000

Hello How are You


Hope Everything is Going well
None

Another Way to import a Module


In [ ]: #Second way to import a module in current file
Syntax:
from module_name import Function_name
from module_name import *

Benefits of Importing module with from keywords is:


You need not to write whole module name while calling the function/variables/classes of the module.

Example
In [75]: from Python import *
print(info.college_name)
print(info.Calculater_info(10,20))
print(info.wish())
print(salary.Employee())

Edyoda Digital university


Addition is 30
Subtraction is -10
Division is 0
Modulus is 10
Exponent is 100000000000000000000

Hello How are You


Hope Everything is Going well
None
Enter Employee Namename
Enter Employee Age23
Enter Employee Citylko
Enter Employee Salary20
Enter Employee Postlko
Employee Name is : name
Employee Age is : 23
Employee City is : lko
Employee Designation is : lko

In [69]: from info import *


import importlib
importlib.reload(info)
print(help(info))
print(college_name)
print(Calculater_info(20,40))
print(wish())
print(welcome("name","Batch"))

Help on module Python.info in Python:

NAME
Python.info

FUNCTIONS
Calculater_info(x, y)

welcome(name, Batch)

wish()

DATA
Batch = 'DS290922A'
college_name = 'Edyoda Digital university'
name = 'Jappan Singh Anand'

FILE
c:\users\praty\python\info.py

None
Edyoda Digital university
Addition is 60
Subtraction is -20
Division is 0
Modulus is 20
Exponent is 10995116277760000000000000000000000000000000000000000

Hello How are You


Hope Everything is Going well
None
Hi nameWelcome to edyoda

In [72]: from info import Calculater_info


print(college_name)
print(Calculater_info(20,40))
print(wish())
print(welcome("name","Batch"))

Edyoda Digital university


Addition is 60
Subtraction is -20
Division is 0
Modulus is 20
Exponent is 10995116277760000000000000000000000000000000000000000

Hello How are You


Hope Everything is Going well
None
Hi nameWelcome to edyoda

In [74]: from name import *


print(name())
print(roll())

Hello
None
World
None

In [ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy