0% found this document useful (0 votes)
118 views7 pages

Revision Tour

The document contains sample Python code and questions related to Python programming. The code samples include loops, conditionals, functions, lists, dictionaries, strings and more. The questions test knowledge of Python concepts like data types, operators, indexing, slicing, sorting algorithms and more. The questions range from filling in missing code, predicting output, identifying infinite loops, writing programs to perform tasks like checking number type or finding day of week, to multiple choice questions testing core Python concepts.

Uploaded by

Nancy Singh
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)
118 views7 pages

Revision Tour

The document contains sample Python code and questions related to Python programming. The code samples include loops, conditionals, functions, lists, dictionaries, strings and more. The questions test knowledge of Python concepts like data types, operators, indexing, slicing, sorting algorithms and more. The questions range from filling in missing code, predicting output, identifying infinite loops, writing programs to perform tasks like checking number type or finding day of week, to multiple choice questions testing core Python concepts.

Uploaded by

Nancy Singh
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/ 7

1.

Fillinthe mising lines of eode in the follewing tode Thecode 1etlsin a hnmit anunt
prices and prints the larget price that is lessthan the limit You tan 6Un thatalprias
are positive humbers. Wlhen a price 0 is eitered the progiatiterBals and prlits the iae
is less than the limit.
vgest phes
#Read the limit
limit float (input ("Inter the 1imit"))
max price 0
#Read the next price
next price =float (input ("Enter a price or 9 to stop")

while next price


cwrite your code here
#Read the next price
write your code herey
if max price > 0
write your code here
else
<write your code here
2. Predict the outputs of the following
programs:
(a) COunt 0
for p in range(1,10)
while count 10: print (p)
print(Hello')
Count + 1 (8) for z in range(-500, 500, 190):

(b) X= 10 print (z)


y=0 (h) X 19
while x > y: y=5
print (x, y) for 1 in range (x - y * 2):

XX 1 print(", 1)
y y+1
() C
(c) keepgoing= True for x in range(10):
X= 100 for y in range
while keepgoing:
(5):
C4 1
print(x) print(c)
X=X 10
ifx < 50:
) X=[1,2,3]
COunter = 0
keepgoing = False
while counter < len (x):
(d) X= 45
print(x[counter] *)
while x < 50: for y in x
print(x) print(y *'% ')

() forx in [1,2, 3,4,5] : COunter + 1

print(x) (k) for x in 'lamp':


print (str.upper (x)
Chopter PrTHON REVISION TOUR
(n) =apple, pear, peach, grapefruit
( ='one
y=x.split (, ')
for z in y:
counter
if z <':
on e counter& len(x)
print(x[counter1, y[couter]) print (str. lower(z))
Counter += 1 else:
print(str.upper(z))
(m) ="apple, pear, peach
y-X.split(", ")
for z iny:
print(z)
3 Find and write the output of the following Python code ICBSE D 2015)

for
Name in ['Jayes, 'Ramya', Taruna, 'Suraj']
print (Name)
i fName[ej == 'T':
break
else:
print ('Finished!)
print ("Got it!')
4. How many times will the following for loop execute and what's the output ?

09 for 1 1n range(-1,7, -2) :


Gi) for i in range (1, 3,1):
for j in range (3): for j in
print (1, 3)
range (i+1):
print ( )
5. 1s the loop in the code below infinite ? How do you know (for sure) before you run it ?
3

n=5
whilen <10:
n-1
n=2#n-m
print (n, m)

ypeCProgramming Practice/Knowledge bosed Questions


a
program to print one of the words negative, zero, or
t h a n Zero, positive, acoording to whether
varia
zero, or greater than zero, respectively
pOgrarn that returns True if the input number is an
3 Write even number, False otherwise
a
Python
rogram that calculates and prints the number of seconds in a year.
o n program
arDer 1s divisible
that accepts two integers fron the user and
prints message a

5. Writea by second number or if it 1s not


of the ve a tasks the userthe day number in a year in the range 2 to 365 and asks the first day
the
dav
day numbernday or Monday or Tuesday etc. Then the program should display
that has been
input
y ad for updation, the key must exist in the dictionary,
Sortin of an array means ranging the array elements in specified order
a
n bubble sort, the adjoining values are compared and exchanged if they are not in
proper ordas
repeated until the entire array is sorted. order. Thie pe
n msertion sort, each successive element is picked & inserted at an appropriate position intheoreie
usly sorted tne

bjective Type Questi0ns


OTQs
Multiple Choice Questions
1. The numbered position of a letter in a string is called

(a) position (b) integer position () index (d) location


2. The operator tells if an element is present in a sequence or not.

(a) exists (b) in (c) into (d) inside


3. The keys of a dictionary must be of types
(a) integer (b) mutable (c) immutable (d) any of these
4. Following set of commands is executed in shell, what will be the output
>>>str= "hello"

>>>str[:2]
(a) he (b) lo (c) olleh (d) hello
5. What data type is the object below ?
L =
[1, 23, 'hello', 1]
(a) list (b) dictionary (c) array (d) tuple
6. What data type is the object below?
L= 1, 23, 'hello', 1
(a) list (b) dictionary (c) array (d) tuple
7. To store values in terms of key and value, what core data type does Python provide ?
(a) list (b) tuple (c) class (d) dictionary
8. What is the value of the following expression ?
3+3.00, 3**3.0
(a) (6.0, 27.0) (6) (6.0, 9.00) () (6, 27)
(d) [6.0, 27.0] (e) [6, 27]
9. List AL is defined as follows:
AL [1, 2, 3, 4, 51
Which of the following statements removes the middle element 3 from it so that the ist qua
[1, 2, 4, 5]?
(a) del a[2 (6)a[2:3][ C) a[2:2]= []
(4) al2] - [
(e)a.remove(3)
Thopt 2 THON EPSO OUR
79
Ths is a strg
This is a strin
3 ovu haswe the taillowing
qude segment
Sturimgl= "my
Strmg2="wOr
prait St1rimgl Stirimg2)
What is the quruit oi thiS COde ?
work (mywork )my
12 o tEve the
iliomE COde segmemit
SUreng="my
Strimgz="wCrk
rimtStrimgl +String2.upper(0))
What is the autgutt of his cnde ?
MY WNork (c)my
WORK d) My Work
13. Wich lime of de podaces am emor?
()1-2 ()one+"2" (d)1+2
4What is the outgut ot his code ?

(c)34 ()24
5wWhich line of code will ause an emor ?

mum= 5, 4, 3, [2], 1]
primt(mum @])
3. primt(mum[3][@])
prämt(mum 5])
(a)Line 3 )Line 2 (c)Line 4 (4)Line 1
h. Which is the omect tom af decdaraion ot dictionary ?

()Day= (Miomday, 2Tuesday, 3 wedimesday


b)Day= (1Monday, 2/Tuesday, 3/wedmesday
)Day-= [Manda, 2Tuesday, 3t'wednesday
() Day= {(Tmandey, 2tuesdiay, 3'wedmesday
17. ldentify the valid derlaratiom ot L
L= [1, 23, h,6]
)ist ) dictiomary (c) array (d) tuple
Fill in the Blonks
tmgS in Pyihom stiore heir individual letters in Memory n location
2 Operator wihem used wih twp stinE5, ives a concatenated sting

Operator rihem uted orith a stnng and an integer gives an error


80 COMPUTER SCIENCE WITH Prru
Part of a stnng Contanng some ous characters from the string is called
The operator whem used with a list/string and an integer, replicates the list/st
ist stming,
are not mutable while lists are.

Using TUTCion, you can make a true copy of a list.

The uncion iS Used to remove an item from a list/dictionary.

The statement can remove an individual item or a slice from a list.

The unction removes all the elements of a list/dicfionary


neaing a tupie from a set ofvalues is called
1Crezting individual values from a tuple's elements is called
3The method retums all the keys in a dictionary.
The uncion returms all values from
Key: value pair of a dictionary.
The umcion returms all
the Key value pairs as (key, value) sequences.
Tue False Guestios
LDo both dhe foilowing represent the same ist".
a 'c']

A ist
may contain any
type of objects except another
list.
here is mo coniceptual mit to the
size of a list.
Al eiements in a ist
must be of the same type.
Agwen object may appear na ist
The keys of a
more than once.
dictionary must be of immutable types.
You can
comibine
numeric value and a
a

he string by using the +


dear) removes all the elements of a Symbol.
Lhe max)and min) when used dictionary but does not delete the
pe with empty
tuples, can work if elements of dictiona
the tuple are all or u

A list of
cdharacters is sinmilar to
11

For amy a string type.


index ,
12 A dicticnary cam sm]+ s[nc] will give you original
contain keys of string s.
3OTE Answers any valid Python types.
for OTQs are
qgiven at the end of
the book.
Sslved Praklems
a t e t kty
three e n t r e s , to assign to
non
19. Create dictionary named D with
a do if you try
? What does Python
a nonexistent key (DI'd')
?
(e.g., D[ "d']='spam') sorting techniques.
What is sorting? Name some popular
20.
sort techniques.
and Insertion
Discuss Bubble sort
21.
Based Questions
B: Application
Type code fragments ?
be the output produced by following
1. What will
"hello" +\ | ( ) x "hello world"
| (b) x =

(a) y str (123)


"to Python" +\ print (x[:2], x[:-2], x[-2:
X="hello" * 3
"world"
print (x[6], x[2:4])
print (x, y) print (x[2:-3], x[-4:-2])
for char in x:
X"hello"+ "world"
y char
y len(x) "

print (y, x) print(y,':', end = ")


that adds up the lengths of all
the words in a list and then prints
2 Write a short Python code segment
average (mean) length.
3. Predict output of the following code
the snippet ?
a [1,2,3,4, 5]
print (a[3:0:-1])
4. Predict the output of the following code snippet?
(a) arr [1, 2, 3, 4, 5, 6]
for i in range (1, 6):
arr[i 1] =arr[i]
for i in range (@, 6):
print (arr[i], end = " ")

(6) Numbers =[9, 18, 27, 36]


for Num in NumbersS
for N in range(1, Num%8):
print (N, "#", end =" ")

print() [CBSE D 2016


5. Find the errors. State reasons.

(a) t (1, "a", 9.2) (d) t 'hello


t[e] =6 t[e] = "H

(6) t [1, "a", 9.2]


(e) for Name in [Amar, Shveta, Paragl
t[e] 6
IF Name [e] 'S =

(c)t [1, "a", 9.2] print (Name)


t[4] 6 ICBSE D 20

6. Assuming words is a valid list of words, the


program below tries to print the list in
have an error ? If so, why ? (Hint. There are two reverse. Do
problems with the code.)
for i in range (len(words), 0,
print(words[i], end = )
-1):
Chopter 2
PYTHON REVISION TOURI1 91
What would be the output of followins code if ntplPHello", Nita'", "Hows ife ?) ?

(a, b, c, d) =
ntpl
print("a is:", a)
print("b is:", b)
print("c is:", c)
print ("d is : ", d)
ntpl= (a, b, c, d)
print(ntpl[0][e]+ntpl[1][1], ntpl[1])
following code ?
8. What will be the output of the
tuple_a 'a', 'b'
tuple_b=('a', 'b')
print (tuple_a tuple_b) ==

(b) 1 (c) False (d) True


(a) 0
code snippet ?
9. What will be the output of the following
"Addr": "NJ", "Country" "USA"}
rec {"Name": "Python", "Age": "20",
idl id(rec)
del rec
rec {"Name" "Python", "Age": "20", "Addr'" : "NJ", "Country": "USA"}
id2 id(rec)
print (idl == id2)

(a) True (b) False (c) 1 (a) Exception

code snippet ?
10. What will be the output of the following
my_dict = {}
my_dict[ (1,2,4) ] = 8

my_dict[ (4,2,1)] = 10

my_dict[(1,2)] = 12
sum 0
for k in my_dict:
sum += my_dict [k]
print (s um)
print (my_dict)
11. Write a method in python to display the elements of list thrice if it is a number and display the element
terminated with # if it is not a number. ICBSE D 2015]
For example, if the content of list is as follows
List =
['41', 'DROND , GIRIRAJ', '13, ZARA']
The output should be
414141
DROND#
GIRIRAJ#
131313
ZARA#

You might also like