Minor Assignment2 (Functions)
Minor Assignment2 (Functions)
2. Give the range in which value of variable x may lie on execution of the following statements:
import random
x = random.random() + 5
3. Evaluate the following expressions using Python shell. Assume that ASCII coding scheme is used for
character data.
a. abs(-5.4)
b. abs(15)
c. chr(72)
d. round(-24.9)
e. float(57)
f. complex(’1+2j’)
g. divmod(5,2)
h. float(57)
i. pow(9,2)
j. max(97, 88, 60)
k. min(55, 29, 99)
l. max(’a’, ’b’, ’AB’)
1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
6. Study the program segments given below. Give the output produced, if any.
a. def test(a, b):
a=a+b
b=a-b
a=a-b
print(’a =’, a)
print(’b =’, b)
test(5, 8)
b. def func():
pass
a = func()
print(a)
7. Write a function areaTriangle that takes the lengths of three sides: side1, side2, and side3 of the
triangle as the input parameters and returns the area of the triangle as the output. Also, assert that sum
of the length of any two sides is greater than the third side. Write a function main that accepts inputs
from the user interactively and computes the area of the triangle using the function areaTriangle.
9. Study the program segments given below. Give the output produced, if any.
a. def say (message, times=2):
print(message*times)
say (‘Hello’)
say(‘World’,5)
b. def fun(a=2,b=3,c=7):
d= a+b+c
print(d)
print(fun (2))
10. Find the sum of even digits of a four-digit number using function.
Warning: Don’t use control structures.
11. Using a function evaluate the value of the arithmetic expression taken from the user.
Hint: Expression will act as an argument while defining function.
2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
12. What does a function return by default in Python? Define a function that does not return any value,
store the function call in a variable and check the value of that variable.
13. Write a function which takes co-ordinates of three points as input and returns true if points are
collinear otherwise returns false.
14. Write a function named as ‘UpperCase’ which converts the lower case alphabet to uppercase alphabet.
Also, assert that the entered alphabet by user is valid lowercase alphabet. Write a function main that
accepts inputs from the user interactively and converts the lowercase alphabet to uppercase using the
function ‘UpperCase’.
a. -5, 5, -5
b. -5, 5, 5
c. -5, -5, -5
d. None of these.
a. Import Error
b. Value Error
c. Type Error
d. Name Error
18. Create the following scripts importedModule and mainModule in the working directory, execute the
script mainModule and justify the output.
• importedModule.py
def test1():
3
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
def test2():
print(’test2 in imported module’)
test1()
test2()
• mainModule.py
import importModule
print(’hello’)