loading…
CBSE Class 12 · Computer Science (083)
CBSE Class 12 Computer Science Python revision: strings, lists, tuples, dictionaries and loops — predict-the-output and find-the-error questions with answers produced by running the code.
import random
random.seed(7) # fixed seed so the run can be repeated
secret = random.randint(1, 20)
tries = 0
while True:
g = int(input("Guess (1-20): "))
tries += 1
if g < secret:
print("Higher")
elif g > secret:
print("Lower")
else:
print("Correct in", tries, "tries")
break
Output (typed input shown after each prompt)
Guess (1-20): 10 Higher Guess (1-20): 15 Lower Guess (1-20): 12 Lower Guess (1-20): 11 Correct in 4 tries
s = "COMPUTER" print(s[1:6:2], s[-3:], s[::-2])
OPT TER RTPO
Why: Slices are [start:stop:step]; stop is never included; a negative step walks backwards.
L = [10, 20, 30, 40, 50] L[1:3] = [5] print(L, len(L))
[10, 5, 40, 50] 4
Why: Assigning to a slice replaces those two items with one, so the list gets shorter.
T = (1, 2, [3, 4]) T[2].append(5) print(T)
(1, 2, [3, 4, 5])
Why: The tuple cannot change, but the list inside it is mutable.
n = int(input("Enter n: "))
if n % 2 = 0
print("Even")
else:
print("Odd")
Running it stops with SyntaxError.
n = int(input("Enter n: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
PyRun is not affiliated with CBSE. Questions are written in the board's style for practice; they are not past papers.