Chapter 10 Functions Solutions
Question - 41 : - How do you make a higher order function in Python ?
Answer - 41 : -
● A high order function accepts one or more functions as input and returns a new function. Sometimes it is required to use function as data.
● To make high order function, one need the import functools module.
● The functools.partial() function is used often for high order function.
Question - 42 : - Give an example of “choice()”
Answer - 42 : -
The following example shows the usage of choice( ) method.
# !/usr/bin/py thon
import random
print “choice([l, 2, 3, 5, 9]): “,
random.choice([l,2,3,5,9])
print “choice(‘A String*): “,
random.choice(‘A String’)
This will produce the following result choice
([l, 2, 3,5,9]): 2
choice(‘A String’) :n
Question - 43 : - Give an example of “random()”
Answer - 43 : -
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 results
random(): 0.281954791393
random(): 0.309090465205
Question - 44 : - Write a program to swap two numbers.
Answer - 44 : -
a = 5
b = 9
def swap(c,d):
return d,c
swap(a,b)
Question - 45 : - What are the rules to define a function in Python?
Answer - 45 : -
You can define functions to provide the required functionality. Here are simple rules to define a function in Python:
(1) Function blocks begin with the keyword def followed by the function name and parentheses (( ))
(2) Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
(3) The first statement of a function can be an optional statement-the documentation string of the function or docstring.
(4) The code block within every function starts with a colon (:) and is indented.
(5) The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
Question - 46 : - Give an example for defining a function.
Answer - 46 : -
def functionname(parameters):
“function_docstring” function_suite
return[expression]
Question - 47 : - Write a program to illustrate function call.
Answer - 47 : -
#!/usr/bin/python # Function definition is here
def printme(str): “This prints a passed string into this function”
print tr;
return; # Now you can call printme function printme(“Mohd saif naqvi”);
printme(“Chairman of Waltons Technology”)
It produces following result:
Mohd saif naqvi
Chairman of Waltons Technology
Question - 48 : - Write a program to illustrate “Pass by refe-rence vs value”
Answer - 48 : -
#!/usr/bin/python # Function definition is here
def changeme(mylist): “This changes a passed list into this function”
mylist.append([l,2,3,4]);
print “Values inside the function: “, mylist
return # Now you can call changeme
function mylist=[10,20,30];
changeme(mylist);
print “Values outside the function: “, mylist
It produce following result:
Values inside the function: [10, 20,30, [1,2,3,4]]
Values outside the function: [10,20, 30, [1,2,3,4]]
Question - 49 : - Write a program to illustrate “Keyword arguments”.
Answer - 49 : -
#!/usr/bin/python
# Function definition is here
def printme(str):”Thisprints a passed string into this function”
print str;
return;
# Now you can call printme function
printme(str=”My string”);
When the above code is executed, it produces following result:
My string
Question - 50 : - Write a program to illustrate “default argument”.
Answer - 50 : -
#!/usr/bin/python
# Function definition is here
def printinfo(name,age=35):
“This prints a passed info into this function”
print “Name: “,
name; print “Age “,
age return;
# Now you can call printinfo function
printinfo(age=50,name=”miki”);
printinfo(name=”miki”);
When the above code is executed, it produces following result:
Name: miki
Age 50
Name:miki
Age 35