Page 1
127
Chapter 2
Functions
After studying this lesson, students will be able to:
? Understand and apply the concept of module programming
? Write functions
? Identify and invoke appropriate predefined functions
? Create Python functions and work in script mode.
? Understand arguments and parameters of functions
? Work with different types of parameters and arguments
? Develop small scripts involving simple calculations
Introduction
Remember, we earlier talked about working in script mode in chapter-1 of this unit to
retain our work for future usage. For working in script mode, we need to write a function
in the Python and save it in the file having .py extension.
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.
Functions can be categorized as belonging to
i. Modules
ii. Built in
iii. User Defined
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
Page 2
127
Chapter 2
Functions
After studying this lesson, students will be able to:
? Understand and apply the concept of module programming
? Write functions
? Identify and invoke appropriate predefined functions
? Create Python functions and work in script mode.
? Understand arguments and parameters of functions
? Work with different types of parameters and arguments
? Develop small scripts involving simple calculations
Introduction
Remember, we earlier talked about working in script mode in chapter-1 of this unit to
retain our work for future usage. For working in script mode, we need to write a function
in the Python and save it in the file having .py extension.
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.
Functions can be categorized as belonging to
i. Modules
ii. Built in
iii. User Defined
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
128
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:
i. import
ii. 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 „math.py?.
(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= math.sqrt (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. We have already seen many function
invoke statement(s), such as
Page 3
127
Chapter 2
Functions
After studying this lesson, students will be able to:
? Understand and apply the concept of module programming
? Write functions
? Identify and invoke appropriate predefined functions
? Create Python functions and work in script mode.
? Understand arguments and parameters of functions
? Work with different types of parameters and arguments
? Develop small scripts involving simple calculations
Introduction
Remember, we earlier talked about working in script mode in chapter-1 of this unit to
retain our work for future usage. For working in script mode, we need to write a function
in the Python and save it in the file having .py extension.
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.
Functions can be categorized as belonging to
i. Modules
ii. Built in
iii. User Defined
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
128
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:
i. import
ii. 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 „math.py?.
(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= math.sqrt (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. We have already seen many function
invoke statement(s), such as
129
>>> type ( )
>>> int ( ), etc.
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)
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.
Note: You normally put all import statement(s) at the beginning of the Python file but
technically they can be anywhere in program.
Lets explore some more functions available in math module:
Name of the function Description Example
ceil( x )
It returns the smallest
integer not less than x,
where x is a numeric
expression.
math.ceil(-45.17)
-45.0
math.ceil(100.12)
101.0
math.ceil(100.72)
101.0
Page 4
127
Chapter 2
Functions
After studying this lesson, students will be able to:
? Understand and apply the concept of module programming
? Write functions
? Identify and invoke appropriate predefined functions
? Create Python functions and work in script mode.
? Understand arguments and parameters of functions
? Work with different types of parameters and arguments
? Develop small scripts involving simple calculations
Introduction
Remember, we earlier talked about working in script mode in chapter-1 of this unit to
retain our work for future usage. For working in script mode, we need to write a function
in the Python and save it in the file having .py extension.
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.
Functions can be categorized as belonging to
i. Modules
ii. Built in
iii. User Defined
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
128
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:
i. import
ii. 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 „math.py?.
(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= math.sqrt (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. We have already seen many function
invoke statement(s), such as
129
>>> type ( )
>>> int ( ), etc.
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)
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.
Note: You normally put all import statement(s) at the beginning of the Python file but
technically they can be anywhere in program.
Lets explore some more functions available in math module:
Name of the function Description Example
ceil( x )
It returns the smallest
integer not less than x,
where x is a numeric
expression.
math.ceil(-45.17)
-45.0
math.ceil(100.12)
101.0
math.ceil(100.72)
101.0
130
floor( x )
It returns the largest
integer not greater than x,
where x is a numeric
expression.
math.floor(-45.17)
-46.0
math.floor(100.12)
100.0
math.floor(100.72)
100.0
fabs( x ) It returns the absolute
value of x, where x is a
numeric value.
math.fabs(-45.17)
45.17
math.fabs(100.12)
100.12
math.fabs(100.72)
100.72
exp( x ) It returns exponential of x:
e
x
, where x is a numeric
expression.
math.exp(-45.17)
2.41500621326e-20
math.exp(100.12)
3.03084361407e+43
math.exp(100.72)
5.52255713025e+43
log( x )
It returns natural
logarithm of x, for x > 0,
where x is a numeric
expression.
math.log(100.12)
4.60636946656
math.log(100.72)
4.61234438974
log10( x )
It returns base-10
logarithm of x for x > 0,
where x is a numeric
expression.
math.log10(100.12)
2.00052084094
math.log10(100.72)
2.0031157171
pow( x, y )
It returns the value of x
y
,
where x and y are numeric
expressions.
math.pow(100, 2)
10000.0
math.pow(100, -2)
Page 5
127
Chapter 2
Functions
After studying this lesson, students will be able to:
? Understand and apply the concept of module programming
? Write functions
? Identify and invoke appropriate predefined functions
? Create Python functions and work in script mode.
? Understand arguments and parameters of functions
? Work with different types of parameters and arguments
? Develop small scripts involving simple calculations
Introduction
Remember, we earlier talked about working in script mode in chapter-1 of this unit to
retain our work for future usage. For working in script mode, we need to write a function
in the Python and save it in the file having .py extension.
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.
Functions can be categorized as belonging to
i. Modules
ii. Built in
iii. User Defined
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
128
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:
i. import
ii. 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 „math.py?.
(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= math.sqrt (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. We have already seen many function
invoke statement(s), such as
129
>>> type ( )
>>> int ( ), etc.
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)
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.
Note: You normally put all import statement(s) at the beginning of the Python file but
technically they can be anywhere in program.
Lets explore some more functions available in math module:
Name of the function Description Example
ceil( x )
It returns the smallest
integer not less than x,
where x is a numeric
expression.
math.ceil(-45.17)
-45.0
math.ceil(100.12)
101.0
math.ceil(100.72)
101.0
130
floor( x )
It returns the largest
integer not greater than x,
where x is a numeric
expression.
math.floor(-45.17)
-46.0
math.floor(100.12)
100.0
math.floor(100.72)
100.0
fabs( x ) It returns the absolute
value of x, where x is a
numeric value.
math.fabs(-45.17)
45.17
math.fabs(100.12)
100.12
math.fabs(100.72)
100.72
exp( x ) It returns exponential of x:
e
x
, where x is a numeric
expression.
math.exp(-45.17)
2.41500621326e-20
math.exp(100.12)
3.03084361407e+43
math.exp(100.72)
5.52255713025e+43
log( x )
It returns natural
logarithm of x, for x > 0,
where x is a numeric
expression.
math.log(100.12)
4.60636946656
math.log(100.72)
4.61234438974
log10( x )
It returns base-10
logarithm of x for x > 0,
where x is a numeric
expression.
math.log10(100.12)
2.00052084094
math.log10(100.72)
2.0031157171
pow( x, y )
It returns the value of x
y
,
where x and y are numeric
expressions.
math.pow(100, 2)
10000.0
math.pow(100, -2)
131
0.0001
math.pow(2, 4)
16.0
math.pow(3, 0)
1.0
sqrt (x ) It returns the square root
of x for x > 0, where x is a
numeric expression.
math.sqrt(100)
10.0
math.sqrt(7)
2.64575131106
cos (x) It returns the cosine of x in
radians, where x is a
numeric expression
math.cos(3)
-0.9899924966
math.cos(-3)
-0.9899924966
math.cos(0)
1.0
math.cos(math.pi)
-1.0
sin (x) It returns the sine of x, in
radians, where x must be a
numeric value.
math.sin(3)
0.14112000806
math.sin(-3)
-0.14112000806
math.sin(0)
0.0
tan (x) It returns the tangent of x
in radians, where x must be
a numeric value.
math.tan(3)
-0.142546543074
math.tan(-3)
0.142546543074
math.tan(0)
0.0
Read More