0% found this document useful (0 votes)
7 views

functions

The document contains a series of Python programming exercises and their corresponding answers. It includes function definitions, code snippets, and output evaluations. Each exercise tests various programming concepts such as loops, conditionals, and string manipulations.

Uploaded by

dharaneshvnr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

functions

The document contains a series of Python programming exercises and their corresponding answers. It includes function definitions, code snippets, and output evaluations. Each exercise tests various programming concepts such as loops, conditionals, and string manipulations.

Uploaded by

dharaneshvnr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

WORKSHEET

1. def fun2(name,age):
print(age,name)
func2(25,”Ramesh”)

2. def fun3(a,b,c):
return a+1,b+2,c+3 #if more than 1 values are returned than it will be as
tuple
t=fun3(10,20,30)
print(t)

3. def fun2(list1):
for x in list1:
print(x.upper(),end="#")
fun2(["Rajesh","Kumar"])

4. def fun2(num1,num2):
for x in range(num1,num2):
if x%4==0:
print(x,end="")
fun2(10,20)

5. def prog(email):
for x in email.split("."):
if x.isalpha():
print("alphabet")
elif x.isdigit():
print("digit")
elif x.isupper():
print("upper")
else:
print("all the best")
prog("rajesh.123.yahoo")

6. def check(x,y):
if x != y:
return x+5
else:
return y+10
print(check(10,5))
7.

8.
9.
def my_func(var1=100, var2=200):
var1+=10
var2 = var2 - 10
return var1+var2
print(my_func(50),my_func())

10. Evaluate the following:


16 - (4 + 2) * 5 + 2**3 * 4

11. What will be the output of the following code?


value = 50
def display(N):
global value
value = 25
if N%7==0:
value = value + N
else:
value = value - N

print(value, end="#")
display(20)
print(value)

12. What will be the output of the following code?


import random
List=["Delhi","Mumbai","Chennai","Kolkata"]
for y in range(4):
x = random.randint(1,3)
print(List[x],end="#")

a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # Mumbai

13. What is the output of the following code snippet?


def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0:
M[i]//=5
if M[i]%3 == 0:
M[i]//=3
L = [25,8,75,12]
ChangeVal(L,4)
for i in L:
print(i,end="#")

14. Find the output of the following code:


Name="PythoN3.1"
R=""
for x in range(len(Name)):
if Name[x].isupper():
R=R+Name[x].lower()
elif Name[x].islower():
R=R+Name[x].upper()
elif Name[x].isdigit():
R=R+Name[x-1]
else:
R=R+"#"
print(R)
15. What will be the output of the following code?
x = 3
def myfunc():
global x
x+=2
print(x, end=' ')
print(x, end=' ')
myfunc()
print(x, end=' ')

16. What possible output(s) are expected to be displayed on screen at the time of execution of the
program from the following code?
Also specify the minimum values that can be assigned to each of the variables BEGIN and LAST.
import random
VALUES = [10, 20, 30, 40, 50, 60, 70, 80]
BEGIN = random.randint (1, 3)
LAST = random.randint(2, 4)
for I in range (BEGIN, LAST+1):
print (VALUES[I], end = "-")

(i) 30-40-50- (ii) 10-20-30-40-


(iii) 30-40-50-60- (iv) 30-40-50-60-70-

17. Find the output of the following:


def makenew(mystr):
newstr = " "
count = 0
for i in mystr:
if count%2 !=0:
newstr = newstr+str(count)
else:
if i.islower():
newstr = newstr+i.upper()
else:
newstr = newstr+i
count +=1
newstr = newstr+mystr[:1]
print("The new string is :", newstr)
makenew("sTUdeNT")
18. Write a function in python to count the number lines in a text file ‘Country.txt’ which is starting with
an alphabet ‘W’ or ‘H’. If the file contents are as follows:

Whose woods these are I think I know.


His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
The output of the function should be:
W or w : 1
H or h : 2

ANSWERS

1. Ans :- Ramesh, 25

2. (11, 22, 33)

3. RAJESH#KUMAR#

4. 1216
5. alphabet

digit

alphabet

6. 15

7. I and IV

8. True

True

True

9. 250 300

10. 18

11. 50#5

12. b. Mumbai#Chennai#Kolkata#Mumbai#

13. 5#8#5#4#

14. pYTHOnN#.

15. 3 5 5

16. OUTPUT – (i) 30-40-50-


Minimum value of BEGIN: 1
Minimum value of LAST: 2

17. The new string is : Ss1sUs3sEs5sTs


18.

You might also like