Functions Defined in Modules
Functions Defined in Modules
Introduction
Gallery Message Watsapp
Types of Functions
Functions User
Built-in
defined in Defined
Functions
Modules Functions
Built-in Functions
• These are pre-defined functions that are always available for use.
• We can directly use it in our program.
• There is no need to import any packages to use this built-in functions.
• Examples: print(), input(), len(), type() …..
Functions Defined in Modules
• In Python, a library is a collection of modules and packages that together cater to a
specific type of applications or requirements.
• A python module is a file containing variables, statements and functions related to
a particular task.
• Functions defined in modules are pre-defined functions in particular modules.
• These functions can be used in our programs only after importing the
corresponding module using import statement.
• Examples: math module, random module, statistics module
Math Modules
• It contains different types of mathematical functions.
• Most of the functions in this module returns float values.
• It can be used in the program by using the following statement:
import math
• Some of the functions in math module are:
sqrt(x): It returns the square root of x. ‘x’ can be a positive integer or floating
point number.
import math
math.sqrt(169) returns 13.0
math.sqrt(-68) returns value Error : math domain error
math.sqrt(0) returns 0.0
ceil(x) : It returns the smallest integer not less than x. ‘x’ can be positive integer or
negative integer or floating point number.
import math
math.ceil(2.1) returns 3
math.ceil(2.8) returns 3
math.ceil(2) returns 2
math.ceil(-2.6) returns -2
floor(x): It returns the largest integer not greater than x. ‘x’ can be a negative or positive
integer or floating point number.
import math
math.floor(6.5) returns 6
math.floor(6) returns 6
math.floor(-6.5) returns -7
pow(x,y): It returns the value x raised to power y in float type. x & y can be an integer or
floating point numbers.
• Built-in function pow returns the value not as a float type. And also it can take maximum 3
prameters. Example : pow(2,3,5) returns the value of (2^3)%5 ( 3rd parameter is
optional).
import math
math.pow(2,3) returns 8.0
math.pow(2.5,1.5) returns 3.9528
pow(2,3) returns 8
fabs(x): It returns the absolute value of float x. x can be integer or any floating point
number.
import math
math.fabs(45) returns 45.0
math.fabs(-45) returns 45.0
math.fabs(-45.6) returns 45.6
2) import random
random.randint(4,16,4)
3) import random
random.random(2,10)
4) import math
math.sqrt(-256)
Activity
• Find the output and what is the minimum and maximum values for the variables end
and begin?
import random
colors=[“Violet”,”Indigo”,”Blue”,”Green”,”Yellow”,”Orange”,”Red”]
end=random.randrange(2)+5
begin=random.randint(1,end)+1
for i in range(begin,end):
print(colors[i],end=“&”)
i) Python&Java&PHP&HTML&C++&
ii) Java&PHP&HTML&C++&
iii) Python&Java&
iv) Python&Java&PHP&