Basic Python: Practice Questions
For each snippet, write the output. If it prints nothing, write “No Output”.
Question 1
x = 2 // 3
y = x * 4
z = y + 5
print("z=", z)Answer
z= 5
Question 2
print(2 * 2 ** 2 + 3 * 4 % 2)Answer
8
Question 3
print(type(8))
print(type(8 / 2))
print(8 == 8.0)Answer
<class 'int'>
<class 'float'>
True
Question 4
word = 'go'
message = word * 2 + '!'
print(message)
print(len(message))Answer
gogo!
5
Question 5
cost = 7
total = cost + 3
cost += 2
print(cost, total)Answer
9 10
Question 6
x = 4%5
y = 4//5
if x == 1 or y == 1:
result = 0
else:
result = 1
print('result=', result)Answer
result= 1
Question 7
if '':
result = 0
else:
result = 1
print('result=', result)Answer
result= 1
Question 8
name = ''
label = name or 'guest'
print(label)
print(bool(label) and not bool(name))Answer
guest
True
Question 9
values = [4, 8, 3, 9]
print(values[0] + values[-2])Answer
7
Question 10
values = [4, 8, 3, 9, 2]
selected = values[1:4]
print(len(selected), selected[-1])Answer
3 9
Question 11
values = [4, 8, 3, 9, 2]
print(values[4:1:-1])
print(values[4:1])Answer
[2, 9, 3]
[]
Question 12
values = [4, 8, 3, 9, 2]
print(sum(values[:3]))
print(sum(values[9:]))Answer
15
0
Question 13
values = [4, 8, 3, 9, 2]
selected = values[-3:]
print(max(selected) - min(selected))Answer
7
Question 14
rows = [[1, 3, 5], [2, 4], [6, 0, 8]]
print(rows[1][0] + rows[0][-1])
print(len(rows[2]))Answer
7
3
Question 15
order = {'item': 'tea', 'count': 2}
order['count'] += 1
print(order['item'], order['count'])Answer
tea 3
Question 16
colors = ['red', 'blue']
record = {'color': 'blue'}
print('blue' in colors)
print('blue' in record)
print('color' in record)Answer
True
False
True
Question 17
def difference(first, second):
return first - second
print(difference(9, 4))
print(difference(4, 9))Answer
5
-5
Question 18
def increase(value):
value += 2
return value
value = 5
result = increase(value)
print(value, result)Answer
5 7
Question 19
x = 10
def foo(x):
if x % 2:
return 1
x -= 1
return x
x += foo(4)
x += foo(5)
x += foo(6)
print("x=", x)Answer
x= 19
Question 20
def announce(value):
print(value + 1)
result = announce(4)
print(result)Answer
5
None
Question 21
i = 0
total = 0
while i < 5:
total = total + i
i += 1
print('total=', total)Answer
total= 10
Question 22
i = 34567
total = 0
while i > 0:
total += 1
i //= 10
print('total=', total)Answer
total= 5
Question 23
total = 0
for i in range(3, 5):
total = total + 1
print('total=', total)Answer
total= 2
Question 24
total = 0
for i in range(10, 0, -2):
total -= i
print("total=", total)Answer
total= -30
Question 25
total = 0
for i in range(0, 10, 3):
if i%2 == 0 or i<5:
total += i
print("total=", total)Answer
total= 9
Question 26
xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
total = 0
for i in range(3, 8, 2):
total += xs[i]
print('total=', total)Answer
total= 33
Question 27
xs = [1, 3, 5]
total = 0
for x in xs:
total += x
print('total=', total)Answer
total= 9
Question 28
xs = [1, 3, 5]
total = 0
for x in xs:
for i in range(3):
total += x*i
print('total=', total)Answer
total= 27
Question 29
values = [3, 8, 2, 5]
selected = []
for value in values:
if value > 3:
selected.append(value)
print(selected)Answer
[8, 5]
Question 30
values = [7, 2, 5]
ranked = sorted(values)
print(values[0], ranked[0])
result = values.sort()
print(values)
print(result)Answer
7 2
[2, 5, 7]
None
Question 31
rows = [[2, 7], [], [4, 1, 8]]
selected = []
for row in rows:
for value in row:
if value % 2 == 0:
selected.append(value)
print(selected)Answer
[2, 4, 8]
Question 32
def has_discount(age):
return age < 12
print(has_discount(11) == True)
print(has_discount(12) == True)
print(has_discount(13) == False)Answer
True
False
True