0% found this document useful (0 votes)
19 views9 pages

XIICSTYPES_OF_FUNCTIONS

t

Uploaded by

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

XIICSTYPES_OF_FUNCTIONS

t

Uploaded by

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

TYPES OF FUNCTIONS

1. Built in functions:- These are the pre defined function in Python and always
available for use whanever required in a program without importing any library
or module.
Examples:- input(), print(), len(), lower(), upper(), islower(), isupper(), isdigit(),
round(), int(), float(), ord(), chr(), min(), max()

2. Function defined in Modules:- These functions are predefined in particular


module and can only be used after importing the corresponding module where
the function is defined.
Examples:- sqrt(), ceil(), floor(), sin() – math module
random(), randrange(), randint(), uniform(),choice()–random module

3. User defined functions:- These functions are made/defined by the programmer


whenever required to solve a particular problem.
Examples:- area(), change(), factorial(), prime() etc.
Important Built in functions
1. round() :- This function takes two arguments : (i) number to be rounded and (ii) ndigits –
number after decimal up to which number is to be rounded and it is optional.
Examples:-
x = round(25.4532,1) x = round(25.4532,2)
print(x) print(x)
Output :- 25.5 Output :- 25.45

x = round(25.4532,0) x = round(25.4532)
print(x) print(x)
Output :- 25.0 Output :- 25

2. int() :- This function return the integer part of the given number.
Example:- a = int(234.75)
print(a) #Output :- 234

3. float() :- This function return the given number in form of floating point number.
Example:- a = float(435)
print(a) #Output :- 435.0
Important Built in functions
4. ord() :- This function accepts the character as an argument and return the ASCII/Unicode of
that character.
Examples:-
x = ‘A’ x = ‘b’
y = ord(x) y = ord(x)
print(y) print(y)
Output :- 65 Output :- 98

5. chr() :- This function accepts a Unicode/ASCII code as an argument and return the
corresponding character/integer.
Example:-
x = 66 x = 90
y = chr(x) y = chr(x)
print(y) print(y)
Output :- B Output :- Z
Important Built in functions
6. min() :- 7. max() :-
This function return the minimum value from This function return the maximum value from
the specified sequence. the specified sequence.

Examples:- Examples:-
a = [4,6,2,8,3] a = [4,6,2,8,3]
b = min(a) b = max(a)
print(b) print(b)
Output :- 2 Output :- 8
a = (9,6,5,8,20) a = (9,6,5,8,20)
b = min(a) b = max(a)
print(b) print(b)
Output :- 5 Output :- 20
a = “5439786” a = “5439786”
b = min(a) b = max(a)
print(b) print(b)
Output :- 3 Output :- 9
Functions defined in random module
 random module :- random(), randrange(), randint(), uniform(), choice()
All the functions defined in random modules are used o generate a number randomly.

 Need of random number :- While programming some situations come when we have to
adopt non-deterministic approach. In these type of situations
random numbers are extensively used.
Examples:- Computer games like throwing dice etc
Recaptcha – login form

1. random() :- This function generates a random floating point number between 0 and 1
(including 0, but excluding 1)
x=random() then 0<=x<1 i.e. x=0.0 (min value) and x = 0.9999(max value)

import random import random as rd


a = random.random() a = rd.random()
print(a) print(a)
Output :- 0.1245854 Output :- 0.8654254
Functions defined in random module
2. randrange() :- This function generates an integer between its lower and upper arguments.
By default lower argument is 0. (including lower value, but excluding upper)
Syntax:- p = random.randrange(x,y) then x<=p<y
Examples:-
import random import random
a = random.randrange(5,20) # 5<=a<20 a = random.randrange(10) # 0<=a<10
print(a) print(a)
Output :- 7 Output :- 8

3. randint() :- This function generates an integer from its lower to upper arguments
Syntax:- p = random.randint(x , y) then x<=p<=y
Example:-
import random
a = random.randint(11,20) # 11<=a<=20 i.e. a = 11,12,13,....20
print(a)
Output :- 13
Functions defined in random module
4. uniform() :- This function generates a floating point random number between its lower and
upper arguments. (including lower value, but excluding upper)
Syntax:- p = random.uniform(x, y) then x<=p<y Here p will be float
Examples:- import random
a = random.uniform(5,20) # 5<=a<20 i.e a = 5.123,5.234,6.23455, ........etc
print(a)
Output :- 8.1232445434

5. choice() :- This function is used for making a random selection from the specified sequence
like list, tuple or string.
Syntax:- p = random.choice(list/tuple/string)
Example:- import random import random
L=[2,5,3,7] P=(9,3,5,8,2,1)
a = random.choice(L) a = random.choice(P)
print(a) print(a)
Output :- 5 Output :- 8
import random import random
P = “1212” P = “ABCD”
a = random.choice(P) a = random.choice(P)
print(a) print(a)
Output :- 1 Output :- A
Find the Output
Q.1 : Read the following code and find the possible output(s) from the given options.

import random as rd
Area = ["North","South","East", "West"]
for i in range(0,3):
t = rd.randrange(0,2)
print(Area[t], end = “:")

(i) South:South:South
(ii) North:South:South
(iii)North:South:East
(iv) North:North:South
Find the Output
Q.1 : Read the following code and find the possible output(s) from the given options.

import random as rd
Area = ["North","South","East", "West"]
for i in range(0,3): #0,1,2
t = rd.randrange(0,2) #0,1
print(Area[t], end = “:")

Answers: (i), (ii), (iv)


(i) South:South:South
(ii) North:South:South
(iii)North:South:East
(iv) North:North:South

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