Functions
A function is a group of statements that exits within a program for the purpose of
performing a specific task.
Advantages of functions are:
Maximizing code reusability
Reducing duplication of code
Make programs simpler to read and understand etc.
Functions types:
Functions can be categorized into three types:
i. Built-in function
ii. Functions defined in modules
iii. User defined functions
Built-in Functions: Built-in functions are the pre-defined functions that are always available
in the standard library to make programing easier, faster and more powerful.
Some built-in functions in python are:
Type conversion functions: int(), str(), float() etc
input function
eval function
min and max functions
abs function
type function
len function
round function
range function
Functions defined in modules: these functions are pre-defined in particular modules and
can only be used when the corresponding module is imported. A module is a file containing
functions and variable defined in separate file.
Import module in a program:
Python provides two important methods to import modules in a program
i. import statement: To import entire module
syntax: import <module name1>, <module name2>, ….
Example:
import math
x=4
print(math.sqrt(x))
Output:
2
ii. from: To import all function or selected ones
syntax: from <module name> import <function name>
example:
from math import pow
print(pow(5,2))
Output:
25
random module (Generating Random Numbers)
import random
randrange(): This function generate an integer between its lower and upper argument.
print(random.randrange(10))
This statement generate random numbers from 0 to 9
print(random.randrange(5,10))
This statement generate random numbers from 5 to 9
print(random.randrange(1,8,2))
This statement generate random numbers: 1, 3, 5 and 7
print(random.randrange(2,8,2))
This statement generate random numbers: 2, 4 and 6
#to select a random name from a list
import random Output1:
Arun
name=["Amit","Arun","Ajay","Anjali"]
lt=len(name) Output2:
n=random.randrange(0,lt) Ajay
print(name[n])
random(): This function generates a random number from 0 to 1 such as
0.9715177405160377. It takes no parameters and returns values between 0 and 1(including 0,
but excluding 1).
import random import random
print(random.random()) n=random.random())
Output: print(round(n,5))
i. 0.7606971546672353 Output:
ii. 0.6686850133435913
i. 0.42998
ii. 0.40281
randint(): this function accept two parameters, a and b as the lowest and highest number,
returns a number randomly in the inclusive range [a,b].
import random from random import randint
print(random.randint(1,10)) print(randint(1,10))
Output: Output:
i. 3 i. 5
ii. 1 ii. 10
iii. 6 iii. 9
uniform(): this fuction returns a random floating-point number in between two numbers.
Syntax: random.uniform(x, y)
The returns random floating point number is greater than or equal to x and less than y.
import random import random
print(random.uniform(1,50)) print(random.uniform(1,50))
Output: 49.946894603207824 Output: 12.785557566457092
choice(): this method is used for making a random selection from a sequence like a string, list or
tuple.
Syntax: random.choice(sequence)
#to select a random name from a list
import random
name=["Amit","Arun","Ajay","Anjali"]
ch_name=random.choice(name)
print(ch_name)
Output 1: Arun
Output 2: Anjali
User defined functions: These are defined by the programmer as per requirement.
Define and call a faction in python:
A user-defined function is defined by def statement followed by function name and
parentheses( () ).
Syntax:
def <function name>([comma separated list of parameters]):
""" function’s docstring """
<statements>
A function definition consists of the following components:
keyword def marks the start of function header
A function name to uniquely identify it. Function naming follows the same rules as rules
of writing identifiers in python.
Parameters are optional
A colon (:) to marks the end of function header
Optinal docstring to describe what function does.
One or more python statement.
An optional return statement to return a value from function.
Example:
Keyword def Function Name
name
def opsmsg( ):
def opsmsg( ): Fuction Hearder
Function
Definition
print(" wel come to oxford public school")
print(" Welcome to Oxford Public School") Function Body
opsmsg( ) Function Call
Output: Welcome to Oxford Public School
Keyword def Function Name
name
def opsmsg( ):
def sum( ): Fuction Hearder
a=int(input("Enter 1st no:"))
Function print(" wel come to oxford public school")
Definition b=int(input("Enter 2nd no:")) Function Body
c=a+b
print("Sum of two no: ",c)
sum( )
Function Call
sum( )
Output 1: Output 2:
Enter 1st no: 30 Enter 1st no: 50
Enter 2nd no: 20 Enter 2nd no: 40
Sum of two no: 50 Sum of two no: 90
Function with return value:
def sum( ):
def sum( ):
st
a=int(input("Enter 1 no:"))
a=int(input("Enter 1st no:"))
b=int(input("Enter 2nd no:"))
b=int(input("Enter 2nd no:"))
c=a+b
c=a+b
return
return c
s=sum()
s=sum()
print("sum of two numbers: ",s)
print("sum of two numbers: ",s)
Output: sum of two numbers: None
#program to calculate addition, and subtraction of two no.
def calc( ):
a=int(input("Enter 1st no:"))
b=int(input("Enter 2nd no:"))
add=a+b
sub=a-b
return add, sub
#.........ma p …………
add, s=calc()
print("addition of two numbers: ", add)
print("subtraction of two numbers: ", s)
Output:
Enter 1st no: 80
Enter 2nd no: 30
Addition of two numbers: 110
Subtraction of two numbers: 50
#program to calculate addition, and subtraction of two no.
def calc( ):
a=int(input("Enter 1st no:"))
b=int(input("Enter 2nd no:"))
add=a+b
sub=a-b
return add, sub
print("End")
#.........main prog…………
result=calc()
print("Addition and Subtraction of two numbers: ", result)
Output:
Enter 1st no: 80
Enter 2nd no: 30
Addition and Subtraction of two numbers: (110, 50)