XIICSTYPES_OF_FUNCTIONS
XIICSTYPES_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()
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)
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 = “:")