[PYTHON DOCUMENT] August 8, 2020
Function in Python: Introduction
Definition :
A function is a named sequence of statement(s) that performs a computation. It contains
line of code(s) that are executed sequentially from top to bottom by Python interpreter. They are the
most important building blocks for any software in Python.
A Function is a subprogram that act on data and often returns a value.
Meaning :
Let look on below polynomial
𝑓(𝑥)= 2𝑥 3
We can say 𝑓(1) = 2
𝑓(2) = 16
𝑓(3) = 54
This function shown in mathematical form can be written in Python like
def CBIC(x): #name of the function is CBIC and argument to the function is x
K=2*x**3 # body of the function
return K #statement to return computed result
a= int(input(" Enter a number :")) #taking integer type of input
print(CBIC(a)) # function call inside print
[PYTHON DOCUMENT] August 8, 2020
Python Function Types :
i. Function defined in Modules
ii. Built in functions
iii. User Defined functions
Module
A module is a file containing Python definitions (i.e. functions) and statements. Standard library of
Python is extended as module(s) to a programmer. Definitions from the module can be used within the
code of a program. To use these modules in the program, a programmer needs to import the module.
Once you import a module, you can reference (use), any of its functions or variables in your code. There
are many ways to import a module in your program, the one’s which you should know are:
a. import
b. from
Import
It is simplest and most common way to use modules in our code. Its syntax is:
import modulename1 [,modulename2, ---------]
Example
>>> import math
On execution of this statement, Python will
(i) Search for the file “[Link]”.
(ii) Create space where modules definition & variable will be created,
(iii) Then execute the statements in the module.
Now the definitions of the module will become part of the code in which the module was imported.
To use/ access/invoke a function, you will specify the module name and name of the function- separated
by dot (.). This format is also known as dot notation.
Example
>>> value= [Link] (25) # dot notation
The example uses sqrt( ) function of module math to calculate square root of the value provided in
parenthesis, and returns the result which is inserted in the value. The expression (variable) written in
parenthesis is known as argument (actual argument). It is common to say that the function takes
arguments and return the result.
This statement invokes the sqrt ( ) function.
From Statement It is used to get a specific function in the code instead of the complete module file. If
we know beforehand which function(s), we will be needing, then we may use from. For modules having
large no. of functions, it is recommended to use from instead of import. Its syntax is
>>> from modulename import functionname [, functionname…..]
Example
>>> from math import sqrt
value = sqrt (25)
[PYTHON DOCUMENT] August 8, 2020
Here, we are importing sqrt function only, instead of the complete math module. Now sqrt( ) function
will be directly referenced to. These two statements are equivalent to previous example.
from modulename import *
will import everything from the file.
Built-in function : We have already seen many function invoke statement(s), such as
>>> type ( )
>>> int ( )
>>> input( )
>>> len( ) etc.
These are pre-defined functions and are always available for use.
Use defined function : These are defined by the programmer.
Structure of a Python Program
The structure of a Python program is generally like the one shown below :
def function 1( ):
:
def function 2( ):
:
def function 3( ):
:
# top-level statement here
Statement1
Statement2
:
Let see this example to explore structure of Python Program:-
def SeeYou():
print("Time to say Jai Shree Ram")
a=155
b=a-10
print(a+3)
if b>5:
print("Value of 'a ' was more than 15 initially")
else :
print("Value of ' a' WAS 15 or less initially")
SeeYou()