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

Computer Science Worksheet

The document contains 10 questions about Python functions and code snippets. It asks the reader to predict outputs, write functions to perform certain tasks, and analyze code examples. The questions cover a range of Python topics including functions, lists, dictionaries, strings, and built-in methods.

Uploaded by

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

Computer Science Worksheet

The document contains 10 questions about Python functions and code snippets. It asks the reader to predict outputs, write functions to perform certain tasks, and analyze code examples. The questions cover a range of Python topics including functions, lists, dictionaries, strings, and built-in methods.

Uploaded by

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

WORKSHEET – PYTHON REVISION TOUR I AND II

1. Predict the output of the following code:


S=”LOST”
L=[10,21,33,4]
D={ }
for I in range(len(S)):
if I%2==0:
D[L.pop( )]=S[I]
else:
D[L.pop( )]=I+3
for K,V in D.items( ):
print(K,V,sep=”*”)

2. Write the Python statement for each of the following tasks using BUILT-IN
functions/ methods only:
(i) To insert an element 200 at the third position, in the list L1.
(ii) To check whether a string named, message ends with a full stop /
period or not.
(iii) To delete an element Mumbai : 50 from Dictionary D.
(iv) To display words in a string S in the form of a list

3. Predict the output of the Python code given below:


Text1= “IND-23”
Text2= “ ”
I=0
while I<len(Text1):
if Text1*I+>=“0” and Text1*I+<=“9”:
Val=int(Text1[I])
Val=Val + 1
Text2=Text2 + str(Val)
elif Text1*I+>=“A” and Text1*I+<=“Z”:
Text2=Text2 + (Text1[I+1])
else:
Text2=Text2 + “*”
I+=1
print(Text2)
4. What will be the output of:
i) print("Welcome To My Blog"[2:6] + "Welcome To My Blog"[5:9])

ii) L=["One , Two", "Three", "Four"]


print(len(L)/2*len(L[0]))

iii) S=”MARSHMELLOW”.partition(‘H’)
print(S[::2])

5. Write a Python Program to display alternate characters of a string my_str.


For example, if my_str = "Computer Science"
The output should be Cmue cec

6. What possible output(s) are expected to be displayed on screen at the time of


execution of the program from the following code? Select correct options (can
also choose more than one option) from below.
import random
arr=['10','30','40','50','70','90','100']
L=random.randrange(1,3)
U=random.randrange(3,6)
for i in range(L,U+1):
print(arr[i],"$",end="@")
a) 30$@40$@50$@70$@90 b) 30$@40$@50$@70$@90$@
c) 30$@40$@70$@90$@ d) 40$@50$@

7. Write the output of the following code given below:


>>>a=[10,20,30,40,50,60,70]
>>>a[3:5]=[100,1000]
>>>a[3:5]=[10000]
>>>print(a)

8. Predict an output of the code given below:


S=”pythons2ip”
n=len(S)
m=’ ‘
for i in range(0,n):
if (s*i+ >= ‘a’ and s*i+ <= ‘m’):
m=m+s[i].upper()
elif (s*i+>= ‘n’ and s*i+<= ‘z’):
m=m+s[i-1]
elif (s[i].isupper()):
m=m+s[i].lower()
else:
m=m+’&’
print(m)

9. Find and write the output of the following python code:


d={}
d[100]=100
d['100']=200
d[100.0]=50
sum=0
print(d)
for i in d:
sum+=d[int(i)]
print(sum)

10. Write a Python program to accept a list and remove duplicates if any from the
list.
For example if the list entered is [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]
Then the output should be [10,20,30,50,60,40,80]
WORKSHEET – WORKING WITH FUNCTIONS

1. 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

2. Write a function, lenWords(STRING), that takes a string as an argument and


returns a tuple containing length of each word of a string.
For example, if the string is "Come let us have some fun", the tuple will have
(4, 3, 2, 4, 4, 3)

3. Predict the output of the following code:


def Changer(P,Q=10):
P=P/Q
Q=P%Q
return P
A=200
B=20
A=Changer(A,B)
print(A,B, sep=’$’)
B=Changer(B)
Print(A,B, sep= ‘$’, end= ‘###’)

4. Write a function dispBook(BOOKS) in Python, that takes a dictionary BOOKS as


an argument and displays the names in uppercase of those books whose
name starts with a consonant. For example, Consider the following dictionary
BOOKS = {1:"Python", 2:"Internet Fundamentals ", 3:"Networking ", 4:"Oracle
sets", 5:"Understanding HTML"}
The output should be:
PYTHON
NETWORKING
5. Write a Python Program containing a function FindWord(STRING, SEARCH),
that accepts two arguments : STRING and SEARCH, and prints the count of
occurrence of SEARCH in STRING. Write appropriate statements to call the
function.
For example, if STRING = "Learning history helps to know about history with
interest in history" and SEARCH = 'history', the function should display
The word history occurs 3 times.

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


L = [5,10,15,1]
G=4
def Change(X):
global G
N=len(X)
for i in range(N):
X[i] += G

Change(L)
for i in L:
print(i,end='$')

7. write a function SHOW_LIST(L) that accepts a list L as an argument and return


sum of all elements that are divisible by 3 in the list.

8. Find the output of the code:


def boards2024(a,b):
s= ‘ ’
for x in a:
s=s+x
s=s+“@”
for y in b:
s=s+y
return s
print(“Finally :”,boards2024(“hi”,”hello”))
9. Find the output:
def change(Line):
alpha=str()
digi=str()
for ch in line:
if ch.isalpha():
alpha+=ch.upper()
else:
digi+=ch
print(alpha,digi)
change(“WeLcome123@Done”)

10. Write a function INDEX_LIST(L) where L is the list of elements passed as an


argument to the function . The function returns another list named ‘indexlist’
that stores the indices of all non-zero elements of L.
For example: 3 If L contains [12,4,0,11,0,56]
The index list will have =[0,1,3,5]

You might also like