MENU

Chapter 10 Functions Solutions

Question - 51 : -
Write a program to illustrate “variablelength arguments”.

Answer - 51 : -

#!/usr/bin/python
# Function definition is here
def printinfo(argl,*vartuple):
“This prints a variable passed arguments”
print “Output is:”
print argl for var in vartuple :
print var return;
# Now you can call printinfo function
printinfo(10); printinfo(70,60,50);

When the above code is executed, it produces following result:

Output is:
10
Output is:
70
60
50

Question - 52 : -
Explain Global vs. Local variables.

Answer - 52 : -

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.
This means that local variables can be accessed only inside the function in which they are declared whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope.

Question - 53 : -
Explain anonymous functions.

Answer - 53 : -

You can use the lambda keyword to create small anonymous functions. These functions are called anonymous because they are not declared in the standard manner by using the def keyword, lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.

Question - 54 : -
Explain Scope of Variables.

Answer - 54 : -

All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python :

(1) Global variables
(2) Local variables

Question - 55 : -
What do you mean by module ?

Answer - 55 : -

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes, and variables. A module can also include runnable code.

Question - 56 : -
What do you mean by globals() and locals () Functions.

Answer - 56 : -

The globals( ) and locals( ) functions can be used to return-the names in the global and local namespaces depending on the location from where they are called.
If locals() is called from within a function, it will return all the names that can be accessed locally from that function.
If globals() is called from within a function, it will return all the names that can be accessed globally from that function .The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function

Question - 57 : -
Explain reload)) Function.

Answer - 57 : -

When the module is imported into a script, the code in the top-level portion of a module is executed only once.Therefore, if you want to re-execute the top -level code in a module, you can use the reload() function.
The reload() function imports a previously imported module again. The syntax of the reload)) function is this :
reload (module_name)

Question - 58 : -
What are docstrings ? How are they useful ?

Answer - 58 : -

A docstring is just a regular python triple- quoted string that is the first thing in a function body or a module or a class. When executing a functionbody the docstring does not do anything like comments, but Python stores it as part of the function documen-tation. This documentation can later be displayed using help)) function. So even though docstrings appear like comments but these are different from comments.

Question - 59 : -
Find the error
def minus (total, decrement)
output = total – decrement
return output

Answer - 59 : -

The function’s header has colon missing at the end.
So add colon (:) at end of header line.
Thus, the correct code is

def minus (total, decrement)
output = total – decrement
return output

Question - 60 : -
Explain Comments in Python.

Answer - 60 : -

Comments in Python :
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the physical line end are part of the comment, and the Python interpreter ignores them.

# !/usr/bin/python
# First comment
print
“Hello, Python!”
;
# second comment
This will produce following result:
Hello, Python!
A comment may be on the same line after a
statement or expression:
name
=
“Madisetti”
# This is again comment
You can comment multiple lines as follows :
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

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