Python Basics

Read short Python snippets and predict what they print

Python Basics is a Bertopia flashcard pack of 73 cards: read short Python snippets and predict what they print. It covers values and types; variables, input and print; strings; lists; dictionaries; conditions; loops; functions.

Sample cards

In Python 3, what does this print?
print(5 / 2)
Show answer

Answer

2.5
Plain / always gives a float in Python 3, even when both numbers are whole. 5 / 2 is 2.5.
second_place, _place2, place_2nd, 2nd_place: name the one that is an invalid Python variable name.
Show answer

Answer

2nd_place
Names may use letters, digits and underscores, but cannot start with a digit.
What does this print?
word = 'Python'
print(word[0])
Show answer

Answer

P
Indexing starts at 0, so word[0] is the first character.
What does this print?
squares = [1, 4, 9, 16, 25]
print(squares[-1])
Show answer

Answer

25
-1 picks the last item of a list, just like with strings.
What does this print?
tel = {'jack': 4098, 'sape': 4139}
print(tel['sape'])
Show answer

Answer

4139
A dictionary maps keys to values. d['sape'] gives the value stored under that key.
What does this print?
x = 1
if x < 0:
    print('Negative')
elif x == 0:
    print('Zero')
elif x == 1:
    print('Single')
else:
    print('More')
Show answer

Answer

Single
Python checks each condition in order and runs only the first block that is true, then skips the rest.

Sources

4 sources

Get this pack when we launch