0% found this document useful (0 votes)
7 views20 pages

Understanding Python Modules and Functions

Uploaded by

lavanyaparashar9
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)
7 views20 pages

Understanding Python Modules and Functions

Uploaded by

lavanyaparashar9
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

Introduction to

Python Modules
GRADE 11
INDRAJA ALAND
MODULES
 It refers to Python file that contains statements and
functions (definitions).
 It is available in Python library.
 Functions can be used within the code of a program.
 Own modules can be also created.
 Modules are Python files containing functions, classes, and
variables.
 They help organize code logically, making it easier to
manage and reuse.
 This promotes clarity in programming
ADVANTAGES OF MODULES
 Reusability

Modules promote reusability of code by allowing


functions and variables to be reused across programs,
saving time and effort.

 Simplicity and Scoping


Modules simplify code by breaking it into manageable
parts. They also provide a scope for variables,
preventing naming conflicts.
STATEMENTS USED FOR
MODULE
IMPORT
It can be used to add a module in your code.
Example:
import math
print(‘Value of pi is: ‘ , [Link])
OUTPUT:
Value of pi is: 3.141592653589793
STATEMENTS USED FOR
MODULE
FROM
It is used to add specific attributes or function from the
module.
Example:
from math import pi
print('Value of pi is:', pi )
OUTPUT:
Value of pi is: 3.141592653589793
RENAMING A MODULE
Renaming a module is called alias.
It can be used within the program only for
which it is been renamed.
Example:
import math as m
print(‘Value of pi is :’ , [Link])
OUTPUT:
Value of pi is: 3.141592653589793
MATH MODULE
These functions help in performing various mathematical
operations.
FUNCTION PURPOSE EXAMPLE
sqrt ( ) It returns the square import math
root of a given number print([Link](25))
Square root of negative OUTPUT:
number is not possible. 5.0

import math
print([Link](-25))
OUTPUT:
ValueError: math
domain error
MATH MODULE
These functions help in performing various mathematical
operations.
FUNCTION PURPOSE EXAMPLE
ceil ( ) It rounds off a floating point import math
number to the next largest print(math. ceil(5.4))
integer OUTPUT:
6

import math
print([Link](-5.6))
OUTPUT:
-5
MATH MODULE
FUNCTION PURPOSE EXAMPLE
floor ( ) It takes a number and import math
rounds it down to the print([Link](5.6))
nearest integer that is not OUTPUT:
greater than the input 5
value
import math
print([Link](-5.6))
OUTPUT:
-6
MATH MODULE
FUNCTION PURPOSE EXAMPLE
pow (base,exp ) It evaluates the base import math
raised to the power exp print([Link](2,3))
and returns the result. OUTPUT:
8.0

import math
print([Link](-2,3))
OUTPUT:
-8.0
MATH MODULE
FUNCTION PURPOSE EXAMPLE
fabs ( ) It returns the absolute import math
value of the number x , print([Link](5))
where x is numeric. OUTPUT:
It also returns absolute 5.0
value in float .
import math
print([Link](5+2))
OUTPUT:
7.0

import math
print([Link](-5))
OUTPUT:
5.0
MATH MODULE
FUNCTION PURPOSE EXAMPLE
degrees ( x ) It converts angle x from import math
radians to degrees, where x print([Link](2))
must be numeric. OUTPUT:
114.59155902616465

import math
print([Link](-2))
OUTPUT:
-114.59155902616465
MATH MODULE
FUNCTION PURPOSE EXAMPLE
sin ( ) It returns the sine of a import math
number x. print([Link]([Link](30)
But, ))
To find sine of degree it OUTPUT:
must be first converted 0.49999999999999994
into radians.
cos ( ) It returns the cosine of import math
value x. print([Link]([Link](30
But, )))
To find cosine of degree OUTPUT:
it must be first 0.8660254037844387
converted into radians.
MATH MODULE
FUNCTIO PURPOSE EXAMPLE
N
tan ( ) It returns the tangent of import math
a number. print([Link]([Link](30))
But, )
To find tangent of
degree it must be first OUTPUT:
converted into radians 0.5773502691896257
RANDOM MODULE
Functions like random() and randint() generate random numbers, useful
in simulations and games.
FUNCTION PURPOSE EXAMPLE
random ( ) It returns a float number import random
between 0.0 to 1.0. a=[Link]()
It does not take any initial print(a)
value as an argument. OUTPUT:
0.3625356813756664
randint ( ) It generates random import random
integer values between a=[Link](1,6)
the specific integer print(a)
values. OUTPUT:
It works only on integer 4
values.
RANDOM MODULE
randrange() provides a range of random numbers, enhancing
unpredictability in applications.

FUNCTION PURPOSE EXAMPLE


randrange ( ) It generates a random import random
numbers within the range a=[Link](1,6,2
randrange(start specified by start and stop )
,stop,step) values. print(a)
The step value is optional.
By default, OUTPUT:
Start=0 1
Step=1

It accepts integer value only.


STATISTIC MODULE
Functions like mean() and median() aid in data analysis, calculating
average and middle values effectively.

FUNCTION PURPOSE EXAMPLE


mean ( ) It calculates arithmetic import statistics
mean of the numbers in a=[Link]([2,2,2])
a sequence or set. print(a)
OUTPUT:
2
median ( ) It returns the middle import statistics
value of numeric data in a=[Link]([1,5,7]
a sequence or set )
print(a)
OUTPUT:
STATISTIC MODULE
FUNCTION PURPOSE EXAMPLE
mode ( ) It returns the most repeated import statistics
value in the sequence or a=[Link]([1,5,7,5,7]
set. )
print(a)
OUTPUT:
5
Conclusion
Python modules enhance code
organization, promote reusability,
and provide powerful built-in
functions. Embracing modules
will lead to more efficient and
manageable code.

You might also like