MENU

Chapter 10 Functions Solutions

Question - 61 : -
What are the various types of functions available in Python ?

Answer - 61 : -


Built-in functions are those that are built into Python and can be accessed by a programmer, e.g. raw_input, len(s),
A module is a file containing Python defintions and statements. We need to import modules to use any of its functions or variables in our code.
e.g. import math
value = math.sqrt (64).
User-defined functions are created by the programmer.
e.g. def area (side):
a = side * side
return a

Question - 62 : -
Explain the term ‘Module’.

Answer - 62 : -

Module : A module is a file containing Python definitions and statements. Standard library of Python is extended as modules 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. There are many ways to import a module in program :
(i) Import
(ii) from
(i) Import: It is simplest and most common way to use modules in our code. Its syntax is :
> > > import module name 1 [, module name 2,………. ]
Example,
> > > import math
> > > value = math.sqrt (25)
(ii) From : It is used to get a specific function in the code instead of the complete module file.
If we know before hand which functions, we will be needing, then we may use ‘from’. For modulus having large number of functions, it is recommended to use ‘from’ instead of import. Its syntax is
> > > from module name import function name 1 [, function name 2, ……….]
Example,
> > > from math import sqrt
> > > value = math . sqrt (36)

Question - 63 : -
Explain ceil(x) in python.

Answer - 63 : -

ceil(x)
Description : The method ceil( ) returns ceiling value of x-the smallest integer not less than x.
Syntax:
Following is the syntax for ceil()
method import math
math.ceil(x)
Note: This function is not accessible directly so we need to import math module and then we need to call this function using math static object.
Parameters : x—This is a numeric expression.
Return Value : This method returns smallest integer not less than x.
Example:

# !/usr/bin/py thon
import math # This will import math module
print “math.ceil(-45.17):”,
math.ceil(-45.17)
print “math.ceil(100.12):”,
math.ceil(100.12)
print “math.ceil(100.72):”,
math.ceil(100.72)
print “math.ceil(119L):”,
math.ceil(119L)
print “math.ceil(math.pi):”,
math.ceil(math.pi),
This will produce the following result
math.ceil(-45.17) :-45.0
math.ceil(100.12): 101.0
math.ceil(100.72): 101.0
math.ceil(119L): 119.0
math.ceil(math.pi): 4.0

Question - 64 : -
Explain exp(x) in python with example.

Answer - 64 : -

Description : The method exp( ) returns exponential of x : ex.
Example:
Syntax: Following is the syntax for exp() method
import math
math.exp(x)
Note: This function is not accessible directly so w e need to import math module and then we need to call this function using math static object.
Parameters : x—This is a numeric expression.
Return Value: This method returns exponential of x: ex.
Example: The following example shows the usage of exp() method

# !/usr/bin/py thon
import math # This will import math module
print “math.exp(-45.17): “,
math.exp(-45.17)
print “math.exp(100.12):”,
math.exp(100.12)
print “math.exp(100.72):”,
math.exp(100.72)
print “math.exp(119L):”,
math.exp(119L)
print “math.exp(math.pi):”,
math.exp(math.pi),

This will produce the following result

math.exp(-45.17): 2.41500621326e-20
math.exp(100.12): 3.03084361407e+43
math.exp(100.72): 5.52255713025e+43
math.exp(119L): 4.7978133273e+51
math.exp(math.pi): 23.1406926328

Question - 65 : -
Explain floor(x) in Python with example.

Answer - 65 : -

The method floor() returns floor of x – the largest integer not greater than x.
Syntax: Following is the syntax for floor() method
import math
math.floor(x)
Note: This function is not accessible directly so we need to import math module and then we need to call this function using math static object.
Parameters : x — This is a numeric expression.
Return Value: This method returns largest integer not greater than x.
Example: The following example shows the usage of floor() method.

# !/usr/bin/python
import math # This will import math module
print “math.floor(-45.17): “,
math.floor(-45.17)
print “math.floor(100.12): “,
math.floor(100.12)
print “math.floor(100.72): “,
math.floor(100.72)
print “math.floor(119L):”,
math.floor(119L)
print “math.floor(math.pi):”,
math.floor(math.pi)

This will produce the following result:

math.floor(-45.17) :-46.0
math.floor(100.12): 100.0
math.floor(100.72): 100.0
math.floor(119L): 119.0
math.floor(math.pi): 3.0

Question - 66 : -
Explain log(x) in Python with example.

Answer - 66 : -

Description : The method log( ) returns natural logarithm of x, for x > 0.
Syntax : Following is the syntax for log() method
import math
math.log(x)
Note: This function is not accessible directly so we need to import math module and then we need to call this function using math static object.
Parameters : x — This is a numeric expression.
Return Value: This method returns natural logarithm of x, for x > 0.
Example: The following example shows the usage of log() method.

# !/usr/bin/python
import math # This will import
math module
print “math.log(100.12): ” ,
math.log(100.12)
print “math.log(100.72): “,
math.log(100.72)
print “math.log(119L):”,
math.log(119L)
print “math.log(math.pi):”,
math.log(math.pi)

This will produce the following result:

math.log(100.12): 4.60636946656
math.log(100.72): 4.61234438974
math.log(119L): 4.77912349311
math.log(math.pi): 1.14472988585

Question - 67 : -
Describe sqrt(x) in Python with example.

Answer - 67 : -

Description : The method sqrt( ) returns the square root of x for x > 0.
Syntax: Following is the syntax for sqrt() method
import math
math.sqrt(x)
Note: This function is not accessible directly so we need to import math module and then we need to call this function using math static object.
Parameters: x —This is a numeric expression.
Return Value : This method returns square root of x for x > 0.
Example: The following example shows the usage of sqrt() method.

# !/usr/bin/python
import math # This will import math module
print “math.sqrt(100): “, math.sqrt(100)
print “math.sqrt(7): “, math.sqrt(7)
print “math.sqrt(math.pi): “, math.sqrt(math.pi)

This will produce the following result:

math.sqrt(100): 10.0
math.sqrt(7): 2.64575131106
math.sqrt(math.pi): 1.77245385091

Question - 68 : -
Describe Random numbers function in Python with example.

Answer - 68 : -

Description : The method random( ) returns a random float r, such that 0 is less than or equal to r and r is less than 1.
Syntax: Following is the syntax for
random() method
random()
Note : This function is not accessible directly so we need to import random module and then we need to call this function using random static object.
Parameters: NA
Return Value : This method returns a random float r, such that 0 is less than or equal to r and r is less than 1.
Example: The following example shows the usage of random() method.

#!/usr/bin/python
import random # First random number
print “random(): “,
random.random() # Second random number
print “random(): “,
random.random()

This will produce the following result:

random(): 0.281954791393
random(): 0.309090465205

Question - 69 : -
Define functions with example.

Answer - 69 : -

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provides better modularity for your application and a high degree of code reusing.As you already know, Python gives you many built-in functions like
print( ) etc. but you can also create your own functions. These functions are called user – defined functions.
Syntax:
def functionname(parameters):
“function_docstring”
function_suite return[expression]
By default, parameters have a positional behavior, and you need to inform them in the same order that they were defined.
Example:
Here is the simplest form of a Python function. This function takes a string as input parameter and prints it on standard screen.
def printme(str):
“This prints a passed string into this function”
print str
return

Question - 70 : -
Define functions calling with example.

Answer - 70 : -

Defining a function only gives it a specific name, specifies the parameters that are to be included in the function, and structures the blocks of code.
Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme( ) function:

# !/usr/bin/py thon
# Function definition
def printme(str):
“This prints a passed string into this function”
print str;
return;
# Now you can call printme function
printme (“I’m first call to user defined function!”);
printme (“Again second call to the same function”);

This will produce the following result:

I’m first call to user defined function!
Again second call to the same function

Free - Previous Years Question Papers
Any questions? Ask us!
×