0% found this document useful (0 votes)
171 views6 pages

Function - Worksheet 1 - 2 Marks

This document is a worksheet for Grade 12 Computer Science focusing on functions in Python. It includes questions on variable scope, code predictions, function definitions, and outputs of various Python code snippets. The exercises aim to test students' understanding of Python programming concepts such as global variables, function parameters, and list manipulations.

Uploaded by

Divyanshi Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views6 pages

Function - Worksheet 1 - 2 Marks

This document is a worksheet for Grade 12 Computer Science focusing on functions in Python. It includes questions on variable scope, code predictions, function definitions, and outputs of various Python code snippets. The exercises aim to test students' understanding of Python programming concepts such as global variables, function parameters, and list manipulations.

Uploaded by

Divyanshi Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Grade: 12 Computer Science

Function - Worksheet - 1
2 Marks

1. 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.
2. 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)
3. Predict the output of the Python code given below:
def Diff(N1,N2):
if N1>N2:
return N1-N2
else:
return N2-N1
NUM= [10,23,14,54,32]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),'#', end=' ')
4. Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as
an argument and displays the names (in uppercase)of the places whose names are
longer than 5 characters. For example, Consider the following dictionary
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}
The output should be: LONDON NEW YORK

Function - Worksheet – 1 - 2 Marks Page 1


5. Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as
an argument and displays the names (in uppercase)of the places whose names are
longer than 5 characters.
For example, Consider the following dictionary
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}
The output should be: LONDON NEW YORK
6. Predict the output of the following code:

7. Find and write the output of the following Python code :


defChangeVal(M,N):
fori 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)
fori in L :
print(i, end='#')
8. Explain the use of positional parameters in a Python function with the help of a
suitable example.
9. Explain the use of a default parameter in a Python function with the help of a suitable
example.
10. Write a Python method/function SWapPair(COLORS) to swap the alternate values of
the content of a list COLORS and display the final values of COLORS.
Note : Assuming that the list has even number of values in it.
For Example :

Function - Worksheet – 1 - 2 Marks Page 2


If the list COLORS contains ["RED","BLACK","WHITE","PINK", "CYAN","BLUE"]
After swap pair operation the content should be displayed as
BLACK RED PINK WHITE BLUE CYAN
11. Write a Python method/function DispFactors(N) to find and display all the factors of
an integer N (parameter).
For Example : If the value of N is 28 The output should be displayed as 1 2 4 7 14 28
12. Predict the output of the Python code given below:
def CALC (P1,P2):
if P1>P2:
return P1-P2
else:
returnP2-P1
N=[20,25,18,64,42]
for CP in range (3,0,-2):
A=N[CP]
B=N[CP-1]
print(CALC(A,B),'@',end='')
13. Predict the output of the Python code given below:
def div5(n):
if n%5==0:
return n*5
else:
return n+5
def output(m=5):
fori in range(0,m):
print(div5(i),'@',end='')
print()
output(7)
output()
14. Write the output of the code given below:
A= 7
defsum(M, N=4):

Function - Worksheet – 1 - 2 Marks Page 3


global A
A=N+M**2
print(A,end='#')
X,Y=10,5
Y=sum(X,Y)
sum(N=9,M=2)
15. Write a generator function generatesq() that displays the square roots of
numbers from 100 to n where n is passed as an argument .
16. Write a program in Python to input a number and display its each digit reversed.
Example :
If the number is 6534
The program should display 4356
17. Write definition of a Method SEARCHNAME(MEMBERS, NAME) to search and
display the serial number of first presence of a NAME from a list of MEMBERS.
For example :
If the list of MEMBERS contain
["ZAHEEN","TOM","CATHERINE","AMIT","HEENA"]
And
The NAME to search is "CATHERINE"
The following should get displayed 3
18. Write definition of a method OddSum(NUMBERS) to add those values in the list
of NUMBERS, which are odd.
19. Write definition of a Method AFIND(CITIES) to display all the city names from a
list of CITIES, which are starting with alphabet A.
For example :
If the list CITIES contains
["AHMEDABAD","CHENNAI","NEW DELHI","AMRITSAR","AGRA"]
The following should get displayed
AHMEDABAD
AMRITSAR
AGRA
20. Write definition of a method MSEARCH(STATES) to display all the state names from
a list of STATES, which are starting with alphabet M.
For example :

Function - Worksheet – 1 - 2 Marks Page 4


If the list STATES contains
["MP","UP","WB","TN","MH","MZ","DL","BH","RJ","HR"]
The following should get displayed :
MP
MH
MZ
21. Write definition of a method EvenSum(NUMBERS) to add those values in the list of
NUMBERS, which are even.
22. What will be the output of the following code?
x=3
defmyfunc():
global x
x+=2
print(x, end=' ')
print(x, end=' ')
myfunc()
print(x, end=' ')
23. Vivek has written a code to input a number and check whether it is even or odd
number. His codeis having errors. Rewrite the correct code and underline the corrections
made.
defcheckNumber(N):
status = N%2
return
#main-code
num=int( input(“ Enter a number to check :))
k=checkNumber(num)
if k = 0:
print(“This is EVEN number”)
else:

Function - Worksheet – 1 - 2 Marks Page 5


24. Write the output of the code given below:

defprintMe(q,r=2):

p=r+q**3

print(p)
#main-code
a=10
b=5
printMe(a,b)
printMe(r=4,q=2)

25. Write the output of the following code:


def change(m, n=10):
global x
x+=m
n+=x
m=n+x
print(m,n,x)
x=20
change(10)
change(20)

Function - Worksheet – 1 - 2 Marks Page 6

You might also like