FUNCTIONS IN PYTHON
Introduction
Galler Messag Watsap
y e p
Myfile Googl Calculat
s e or
• A function is a collection of statements to perform a particular task.
• Large programs are broken down into smaller units/modules known as functions.
• A function is a named unit of a group of program statements. To execute a function, we have to
call it in the program as and when requires.
• Advantage of using functions is that it makes the program handling easier as only a small part of
the program is dealt with at a time.
• Another advantage is that it reduces the program size. Once written, a function can be used in
other programs as library functions.
Types of Functions
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 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
pi : Returns the value of pi.
import math
math.pi returns 3.141592653589793
sin()/cos()/tan() : Returns the sin/cos/tan values of angles measured in
radians.
Statistics Module
• This module provides functions for calculating statistics data.
mean(): Returns the mean/average of the given data.
Syntax: statistics.mean(<sequence of values>)
Example: import statistics
statistics.mean([2,5,4,8]) returns 4.75
median(): Returns the middle value of the given data.
syntax: statistics.median(<sequence of values>)
* if the number of elements in the sequence is odd, median() returns middle value. If it is
even, then median() returns the mean of two central numbers.
import statistics
statistics.median( [10,20,30,40,50]) returns 30.0
statistics.median( [10,20,30,40,50,60]) returns 35.0
mode(): Returns the most repeated value in the given sequence of data.
syntax: statistics.mode(<sequence of values>)
import statistics
statistics.mode( [10,20,30,10,40,20,10])) returns 10.
• If there are multiple numbers with same frequency, then mode returns the first on
encountered.
statistics.mode([10,20,30,10,20,10,20]) returns 10
• If we are not passing any data into the statistics functions, then it returns Statistics
Error.
Random Module
• The programming concept involved situations where we aware of the definite
output to be obtained when a particular task to be executed.
• There are certain situations where we require some random numbers which are
not known earlier. For example,
*Pseudo random numbers on Lottery scratch cards
*Captcha in login forms
* Computer games involving throwing of dice, picking a number or flipping a
coin.
*Shuffling deck of playing cards
• In the above situations we need random numbers.
• In order to generate random numbers, we have to import random module.
import random
Types of functions in random module
random()
• This function generates floating value between 0 & 1 including 0 and excluding 1.
• random() takes no arguments.
• Example: import random
random.random() returns 0.33651376240554043
random.random() returns 0.9690849208188809
random.random() returns 0.32623498836182896
• Each time random() generates a different number.
• Example: >>>random.random()*5 generates random number between 0 &5.
• >>>random.random()*5+10 generates random number between 10 & 15.
>>>random.random()*900+100 generates random number between 100 & 1000.
randrange()
• This method always returns the integer between lower and upper argument
excluding upper argument.
• Syntax: randrange (start, stop, step)
• Example: To generate random numbers from 0 to 30
>>>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
• from <module name> import <function name>
• Example: from math import pi
from random import randrange
• Example: from math import pi
print(pi) returns 3.141592653589793
from math import floor
print(floor(4.5)) returns 4
Activity
• Find errors:
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=“&”)
a) Blue & Green &
b) Green & Yellow & Orange &
c) Indigo & Blue & Green &
d)Yellow & Orange & Red &
Activity
• Find the output and specify the maximum and minimum value that can be
assigned to pos variable.
import random
languages=[‘Python’, ‘Java’, ‘PHP’, ‘HTML’, ‘C++’]
pos=random.randint(1,4)
for c in range(0, pos+1):
print(languages[c],end=“&”)
i) Python&Java&PHP&HTML&C++&
ii) Java&PHP&HTML&C++&
iii) Python&Java&
iv) Python&Java&PHP&