Class 11 Exam  >  Class 11 Notes  >  Textbook - Functions, Computer Science (Python), Class 11

Textbook - Functions, Computer Science (Python), Class 11 PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 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

FAQs on Textbook - Functions, Computer Science (Python), Class 11

1. What is a function in computer science?
A function in computer science is a block of code that performs a specific task. It is a reusable piece of code that can be called and executed multiple times within a program. Functions help in organizing and simplifying code by breaking it down into smaller, manageable parts.
2. How do you define a function in Python?
In Python, a function is defined using the "def" keyword followed by the function name and parentheses. Any parameters that the function takes are specified within the parentheses. The function body, which contains the code to be executed, is indented below the function definition. Example: ``` def my_function(parameter1, parameter2): # code to be executed ```
3. What is the purpose of a return statement in a function?
The return statement in a function is used to specify the value that the function should output or return when called. It is used to pass data or results back to the calling code. When a return statement is encountered in a function, the function immediately stops executing and returns the specified value. Example: ``` def add_numbers(a, b): return a + b result = add_numbers(5, 3) print(result) # Output: 8 ```
4. What is a recursive function in Python?
A recursive function in Python is a function that calls itself during its execution. It is a technique where a problem is solved by breaking it down into smaller subproblems and solving each subproblem recursively until a base case is reached. Recursive functions are useful for solving problems that can be naturally divided into smaller, similar subproblems. Example: ``` def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) result = factorial(5) print(result) # Output: 120 ```
5. Can a function have multiple return statements in Python?
Yes, a function in Python can have multiple return statements. However, only one return statement will be executed during the function's execution. When a return statement is encountered, the function immediately stops executing and returns the specified value. Having multiple return statements can be useful in certain scenarios where different conditions need to be checked and different values need to be returned based on those conditions.
Download as PDF

Top Courses for Class 11

Related Searches

MCQs

,

Computer Science (Python)

,

Class 11

,

Computer Science (Python)

,

Computer Science (Python)

,

Objective type Questions

,

pdf

,

Free

,

mock tests for examination

,

Textbook - Functions

,

Previous Year Questions with Solutions

,

video lectures

,

Summary

,

Sample Paper

,

past year papers

,

Class 11

,

Class 11

,

Semester Notes

,

Important questions

,

Exam

,

Textbook - Functions

,

ppt

,

Textbook - Functions

,

Extra Questions

,

practice quizzes

,

study material

,

shortcuts and tricks

,

Viva Questions

;