20 snippets

Footgun simulator

Where a correct JavaScript instinct produces the wrong answer. Predict the output, then the snippet is handed to CPython in your browser and the interpreter settles it.

0/0 correct
x
1/20Indexing past the start
Sequences
xs = [10, 20, 30]
print(xs[-1])

What does this print?

Loading the Python runtime…

2/20Multiplying a nested list
Sequences
grid = [[]] * 3
grid[0].append("x")
print(grid)

What does this print?

Loading the Python runtime…

3/20The default argument that remembers
Objects
def collect(item, into=[]):
into.append(item)
return into
print(collect(1))
print(collect(2))

What does this print?

Loading the Python runtime…

4/20Closures in a loop
Closures
fns = [lambda: i for i in range(3)]
print([f() for f in fns])

What does this print?

Loading the Python runtime…

5/20How much does replace replace?
Strings
print("a-b-c".replace("-", "+"))

What does this print?

Loading the Python runtime…

6/20Sorting in place
Sequences
xs = [3, 1, 2]
print(xs.sort())

What does this print?

Loading the Python runtime…

7/20Empty containers are falsy
Semantics
for value in ([], {}, "", 0, 0.0, None, [0]):
print(repr(value), bool(value))

What does this print?

Loading the Python runtime…

8/20Dividing two integers
Numbers
print(7 / 2)
print(7 // 2)
print(-7 // 2)

What does this print?

Loading the Python runtime…

9/20Comparisons in a row
Semantics
print(1 < 2 < 3)
print(3 > 2 > 1)
print((1 < 2) < 3)

What does this print?

Loading the Python runtime…

10/20is versus ==
Semantics
a, b = 256, 256
c, d = 257, 257
print(a is b, a == b)
print(c is d, c == d)

What does this print?

Loading the Python runtime…

11/20The classic
Numbers
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)

What does this print?

Loading the Python runtime…

12/20A tuple with one element
Sequences
print(type((1)).__name__)
print(type((1,)).__name__)
print(len((1,)))

What does this print?

Loading the Python runtime…

13/20Dictionary ordering
Objects
d = {"b": 1, "a": 2, "10": 3, "2": 4}
print(list(d))

What does this print?

Loading the Python runtime…

14/20Copying a nested list
Sequences
original = [[1, 2], [3, 4]]
copy = original.copy()
copy[0].append(99)
print(original)

What does this print?

Loading the Python runtime…

15/20Mutating a string
Strings
s = "abc"
try:
s[0] = "z"
except TypeError as e:
print("TypeError:", e)
print(s)

What does this print?

Loading the Python runtime…

16/20A mutable class attribute
Objects
class Bag:
items = []
a, b = Bag(), Bag()
a.items.append("x")
print(b.items)

What does this print?

Loading the Python runtime…

17/20Double-underscore attributes
Objects
class A:
def __init__(self):
self.__secret = 1
a = A()
print([n for n in vars(a)])
print(a._A__secret)

What does this print?

Loading the Python runtime…

18/20The exception variable afterwards
Semantics
try:
raise ValueError("boom")
except ValueError as err:
print(err)
print("err" in dir())

What does this print?

Loading the Python runtime…

19/20Iterating twice
Sequences
squares = (x * x for x in range(3))
print(list(squares))
print(list(squares))

What does this print?

Loading the Python runtime…

20/20Identity on strings
Semantics
a = "hello world"
b = "hello world"
c = "".join(["hello", " ", "world"])
print(a is b, a == b)
print(a is c, a == c)

What does this print?

Loading the Python runtime…