Functions-Builtin & Function defined in Modules (3)
Functions-Builtin & Function defined in Modules (3)
Introduction
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.
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 closest integer that is greater than or equal to 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 closest integer that is less than or equal to x. ‘x’ can be a negative or
positive integer or floating point number.
import math
math.floor(6.8) returns 6
math.floor(6.3) 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.
import math
math.pow(2,3) returns 8.0
math.pow(2.5,1.5) returns 3.9528
[ Note: Built-in function pow returns the value not as a float type. And also it can take
maximum 3 parameters. Example : pow(2,3,5) returns the value of (2^3)%5 ( 3rd parameter
is optional).]
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
>>>import random
>>>random.randrange(30) returns 27
>>>random.randrange(30) returns 18
• To generate random numbers between 10 to 100 with the multiple of 5.
import random
x=random.randrange(10,100,5)
print(x) returns 30
randint()
• It takes 2 arguments a & b in which ‘a’ is lower & ‘b’ is the upper limit.
• This function returns any integer between ‘a’ and ‘b’ including both.
• Syntax: random. randint(a,b)
• Example: import random
x=random.randint(0,5)
The above program generates an integer between 0 and 5 i.e, either 0,1,2,3,4 or 5.
y=random.randint(1,10)+10
The above program generates an integer between 11 and 20 i.e, either
11,12,13,14,15,16,17,18,19 or 20.
uniform()
• random. uniform(x, y) It generates a random floating point number which is greater
than x and less than y.
• Example: random.uniform(1,100) returns 39.56970658693995
choice()
• This method is used for making a random selection from a sequence like list, tuple or string.
• Syntax: random.choice(sequence)
• Example: import random
x=random.choice([1,2,3,4,5])
print(x) returns an element 1,2,3,4 or 5
shuffle()
• This method is used to shuffle the contents of a list.
• Syntax: random.shuffle(list)
• Example: import random
fruits=[‘Apple’,’Mango’,’Orange’,’Grapes’]
random.shuffle(fruits)
print(fruits) returns ['Mango', 'Orange', 'Apple', 'Grapes']
Another way of importing a module into the program
1) import math
math.power(4,2)
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&