Assignment Functions in Python
Assignment Functions in Python
Assignment Functions in Python
Q1. Observe the following Python code very carefully and rewrite it after removing all syntactical errors with each (2)
correction underlined.
def execmain():
x = input("Enter a number:")
if (abs(x)= x):
print("You entered a positive number")
else:
x=*-1
print("Number made positive : ",x)
execmain()
show("PasS@2019")
1 mark for correct approach and 1 mark for correct output
The new string is:, pASs@2019
Q3. Rewrite the following code after removing all errors (2)
def Tot(Number) #Method to find Total
Sum=0
for C in Range (1, Number+1):
Sum+=C
RETURN Sum
print Tot[3] #Function Call
(1/2 mark for each correction) or 1 mark for identifying all the errors without suggesting correction
def Tot(Number) : #Method to find Total #Error 1
Sum=0
for C in range (1, Number+1): #Error 2
Sum+=C
return Sum #Error 3
print(Tot(3)) #Function Call #Error 4
(1/2 mark for each correction) or 1 mark for identifying all the errors without suggesting correction
Q4. Write definition of a method/function DoubletheOdd(Nums) to add and display twice of odd values from the (3)
list of Nums.
For example :
If the Nums contains [25,24,35,20,32,41]
The function should display
Twice of Odd Sum: 202
l=[25,24,35,20,32,41]
s=0
for i in l:
if i%2!=0:
s+=i*2
print(s)
Q5. What is wrong with the following function definition even with correct code? (1)
def addEm(x,y,z):
return x+y+z
print(“the answer is :”, x+y+z)
Indentation of Print statement.
1 mark for correct answer
ASSIGNMENT WORKSHEET 3
Q1. Write definition of a method/function AddOddEven(VALUES) to display sum of odd and even values separately from the
list of VALUES.
For example : If the VALUES contain [15, 26, 37, 10, 22, 13] The function should display Even Sum: 58 Odd Sum: 65
Q2. 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')
Q3. Write a Python function to determine how many times a given letter occurs in a string.
Q4. Write a Python function to binary search a list
Q5. Show output of following:
def Show(STR, KEY):
x=0
L=len(STR)?
while X < (L//2):
if X%2 is not 1:
print( STR[X] * KEY )
else:
print( STR|X] * (KEY+1) )
x+=1
KEY+=2
show(”PYTHON”,1) #calling function show()
CBSE BASED PROBLEMS WORKSHEET 4
Q1 Consider the following function headers. Identify the correct statement: (1)
1) def correct(a=1,b=2,c): 2) def correct(a=1,b,c=3):
3) def correct(a=1,b=2,c=3): 4) def correct(a=1,b,c):
Ans: - 3) def correct(a=1,b=2,c=3)
Q2 Write output of following code (2)
def Alter(x,y=20):
x=x*y
y=x%y
print (x,'*',y)
return (x)
a=200
b=30
a=Alter(a,b)
print (a,'$',b)
b=Alter(b)
print (a,'$', b)
a=Alter(a)
print (a,'$',b)
output is
(6000, '*', 0)
(6000, '$', 30)
(600, '*', 0)
(6000, '$', 600)
(120000, '*', 0)
(120000, '$', 600)
Q3. Find and write the output of the following python code: (2)
def Change(p,q=30):
p=p+q
q=p-q
return (p)
R=150
S=100
R=Change(R, S)
print (R, "#",S)
S=Change (S)
print (R, "#", S)
R=Change (R)
print (R, "#", S)
output is :-(250, '#', 100)
(250, '#', 130)
(280, '#', 130)
Q4. Go through the following python codes carefully and write the output of the code. (3)
a = 10
b = 20
def changer(lst):
global a
a+=10
b = 30
lst.append(a)
lst = [1,2,3]
print(a,b)
changer(lst)
print(a,lst,b)
changer(lst)
print(a,lst,b)
output is :- 10 20
20 [1, 2, 3, 20] 20
30 [1, 2, 3, 20, 30] 20
Q5 Write a python function showlarge() that accepts a string as parameter and prints the words (3)
whose length is more than 4 characters.
Eg: if the given string is “My life is for serving my Country”
The output should be serving Country
def showlarge(s):
l = s.split()
for x in l:
if len(x)>4:
print(x)
s=" My life is for serving my Country "
showlarge(s)
ASSESMENT WORKSHEET 5
Q1. Write a function rowsum() that accepts a 2D list as parameter and prints the sum of each row.
Eg: 1 2 3
456
789
The output should be:
6
15
24
Q2. Find and write the output of the following python code:
a=10
def call():
global a
a=15
b=20
print(a)
call()
Q3. What do you understand by local and global scope of variables? How can you access
a global variable inside the function, if function has a variable with same name.
Q4. Find error in following code:-
def DEFA_ARG(n1=1, n2=2,n3):
n1=n1+n2
n2+=n3
print (nl ,n2,n3)
Check()
Check(2, 1)
Check(3,4,5)
Q5. Kritika was asked to accept a list of even numbers but she did not put
the relevant condition while accepting the list of numbers. You are
required to write a user defined function oddtoeven(L) that accepts the
List L as an argument and convert all the odd numbers into even by
multiplying them by 2 .