Chapter-3 Exercise Questions
1.Write a function that takes amount-in-dollars and dollar-to-rupee conversion price; it then returns amount
converted to rupees. Create the function in both void and non-void forms.
Code-1
def DollarToRupee(dollars, exchangeRate):
print(dollars * exchangeRate)
amt=float(input('Enter amount in dollar'))
rate=float(input('Enter Exchange rate'))
DollarToRupee(amt,rate)
Code-1
def DollarToRupee(dollars, exchangeRate):
return dollars * exchangeRate
amt=float(input('Enter amount in dollar'))
rate=float(input('Enter Exchange rate'))
rupee=DollarToRupee(amt,rate)
print(rupee)
2.Write a function to calculate volume of a box with appropriate default values for its parameters. Your
function should have the following input parameters :
(a) length of box ; (b) width of box ; (c) height of box.
Test it by writing complete program to invoke it.
Code
def volume(length = 1, breadth =1, height = 1):
return length * breadth * height
l=float(input(‘Enter length of a box:’))
b=float(input(‘Enter breadth of a box:’))
h=float(input(‘Enter height of a box:’))
v=volume(l,b,h)
print(‘volume of box=‘,v)
3.Write a program to have following functions :
(i) a function that takes a number as argument and calculates cube for it. The function does not return a value.
If there is no value passed to the function in function call, the function should calculate cube of 2.
(ii) a function that takes two char arguments and returns True if both the arguments are equal otherwise False.
Test both these functions by giving appropriate function call statements.
Code
def cube(x = 2):
print(x ** 3)
def compare(a, b):
return a == b
cube(5)
cube()
print (compare( 'a', 'a' ))
print (compare( ‘z' , 'x' ))
4.Write a function that receives two numbers and generates a random number from that range. Using this
function, the main program should be able to print three numbers randomly
Code
import random
def randomInRange(a, b):
return random.randrange(a, b)
for i in range(3):
print(randomInRange(1, 10))
5.Write a function that receives two strings arguments and checks whether they are same-length strings
(returns True in this case otherwise false)
Code
def comparestr(str1,str2):
l1= len(str1)
l2=len(str2)
if l1==l2:
return True
else:
return False
s1=input("Enter first string:")
s2=input("Enter second string:")
comparestr(s1,s2)
r=comparestr(s1,s2)
if r==True:
print("Same length")
else:
print("Different length")
6.Write a function namely nthRoot( ) that receives two parameters x and n and returns nth root of x i.e., x 1/n .
The default value of n is 2.
Code
def nthRoot(x, n = 2):
return x ** (1 / n)
print(nthRoot(x,3))
print(nthRoot(x))
7.Write a function that takes two numbers and returns the number that has minimum one's digit.
[For example, if numbers passed are 491 and 278, then the function will return 491 because it has got
minimum one's digit out of two given numbers (491's 1 is < 278's 8)].
Code
def smallUnitPlace(x, y):
unitX = x % 10
unitY = y % 10
if unitX < unitY:
return x
else:
return y
a=int(input(‘Enter first no:’))
b=int(input(‘Enter second no:’))
min=smallUnitPlace(a,b):
print(min)
8.Write a program that generates a series using a function which takes first and last / values of the series and then
generates four terms that are equidistant e.g., if two numbers passed are 1 and 7 then function returns 1 3 5 7.
Code
def completeSeries(a, 1):
d = (1 - a) / 3
return (a, a + d, a + 2*d, a + 3*d, )
print(completeSeries(1, 7))
9.Which names are local and which are global in the following code fragment?
invaders = "Big names"
pos 200
level = 1
def play( ) :
max_level = level + 10
print(len (invaders) ) == 0)
return max_level
res = play()
print(res)
Ans.
Global names invaders, pos, level, res
Local names max level
Built in len
10.What is wrong with the following function definition ?
def addEm(x, y, z):
return x + y + z
print("the answer is“, x + y + z)
Ans. The statement with print( ) is unreachable in above code as it follows the return statement. As soon as
the return statement gets executed, the control returns from the function and no other statement gets
executed.
11.Consider the code below and answer the questions that follow :
def multiply(number1, number2):
answer = numberl * number2
return (answer)
print(number1, 'times ' , number2, '=', answer )
output = multiply(5,5)
(i) When the code above is executed, what gets printed ?
(ii) What is variable output equal to after the code is executed ?
Ans.
(i) Nothing gets printed (as print() is after the return statement)
(ii) 25
12. Find the errors in code given below :
(a) def minus (total, decrement)
output = total - decrement
print (output)
return (output)
Ans. Syntax error. Colon ( : ) missing in the end of function header.
(b)define check()
N = input ( 'Enter N: ' = 3
answer = 1 + i ** 4 / N
Return answer
Ans.Syntax error, keyword to define a function is def ( not define). Also, colon ( : ) missing in the end of
function header.Return is not a valid statement. It should be return
c)def alpha (n, string = 'xyz', k = 10) :
return beta (string)
return n
def beta (string)
Return string == str(n)
print(alpha ("Valentine' s Day"):)
print(beta (string = 'true' ))
print(alpha(n = 5, "Good-bye"):)
Ans.Second return statement is unreachable.
In function beta( )'s definition, Colon ( : ) missing in the end of function header.
In main part, the colons at the end of first and third print( ) statements is invalid (not enclosed in quotes)
In third print( ) statement, in the function call of alpha( ), positional argument follows keyword argument,
which is a syntax error.
13.What is the output of following code fragments ?
(a)def increment(n):
n.append([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)
(b)def increment(n):
n.append([49])
return n[0], n[1], n[2], n[3]
L = [23, 35, 47]
m1, m2, m3, m4 = increment(L)
print(L)
print(ml, m2, m3, m4)
print(L[3] == m4)
Ans.
(a) [1, 2, 3,[4]] [1,2, 3, [4]]
(b) [23, 35,47,[49]]
23 35 47 [49]
True
14.Find and write the output of the following python code :
def fun(s):
k = len(s)
m =" "
for i in range(0,k):
if (s[ i] isupper( )):
m = m+s[i].lower( )
elif s[i].isalpha( ):
m = m+s[i].upper( )
else:
m = m+' bb'
print(m)
fun('school2@com)
output
SCHOOLbbbbCOM
15.Predict the output of the following code fragment ?
def func(message, num = 1):
print(message * num)
func( 'Python' )
func( 'Easy' , 3)
output
Python
EasyEasyEasy
16.Is return statement optional ? Compare and comment on the following two return statements :
return
return val
Ans.The return statement is optional ONLY WHEN the function is void or we can say that when the function
does not return a value. A function that returns a value, must have at least one return statement.
From given two return statements, statement return is not returning any value, rather it returns the control to
caller along with empty value None. And the statement return val is returning the control to caller along with
the value contained in variable val.