Chapter 10 Functions Solutions
Question - 31 : - What is the utility of built-in function help () ?
Answer - 31 : -
Python’s built in function help ( ) is very useful. When it is provided with a program-name or a module-name or a function-name as an argument, it displays the documentation of the argument as help. It also displays the docstrings within its passed-argument’s definition. For example :
help (math)
will display the documentation related to module math.
Question - 32 : - What is the utility of Python standard library’s math module and random module ?
Answer - 32 : -
Math module is used for math related functions that work with all number types except for complex numbers, radom module is used for different random number generator functions.
Question - 33 : - What is raw_input?
Answer - 33 : -
It is a function which takes a string (e.g., a question) as argument, writes the string to the terminal window, halts the program and lets the user write in text on the keyboard, and then the text is returned to the calling code as a string object.
Question - 34 : - What value will be return by log(x[, base])?
Answer - 34 : -
With one argument, return the natural logarithm of x (to base e).
With two arguments, return the logarithm of x to the given base, calculated as log(x) log(base)
Question - 35 : - Define pow(x, y) function in Python ?
Answer - 35 : -
Return x raised to the power y.. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.
Question - 36 : - Determine what is printed by the following program listinput.py,
list1 = eval(raw_input(‘Give a list:’))
list2 = eval(raw_input(‘Give another list:’))
print list1 + list2
When the program is running as follows:
Unix/DOS > python listinput.py
Give a list: ‘[1,2]’
Give another list: “[3,4]”
Answer - 36 : -
[1,2] [3,4]
Question - 37 : - What will happen when you run the following program:
import sys
try:
t = sys.arg[1]
a = sys.arg[2]
except:
print ‘You must provide TWO
command-line arguments!!!’
sys.exit(1)
s = 0.5*a*t**2
print s
Answer - 37 : -
The program will always respond with, “You must provide TWO command-line arguments!!!”, regardless of what you write on the command line
Question - 38 : - How can you make a module ‘helloworld’ out of these two functions?
def hello():
print ‘Hello,’,
def world(): print ‘World!’
Answer - 38 : -
Put the functions in a file with name helloworld.
py
Question - 39 : - Write a program to read the user name using raw_input() and display back on the screen using print()
Answer - 39 : -
#!/usr/bin/python
name=raw_input(‘Enter your name :’)
print (“Hi %s, Let us be friends!” % name);
Question - 40 : - What does the len() function do?
Answer - 40 : -
It gets the length of the string that you pass to it then returns it as a number. Play with it.