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.
xs = [10, 20, 30]print(xs[-1])
What does this print?
Loading the Python runtime…
grid = [[]] * 3grid[0].append("x")print(grid)
What does this print?
Loading the Python runtime…
def collect(item, into=[]):into.append(item)return intoprint(collect(1))print(collect(2))
What does this print?
Loading the Python runtime…
fns = [lambda: i for i in range(3)]print([f() for f in fns])
What does this print?
Loading the Python runtime…
print("a-b-c".replace("-", "+"))
What does this print?
Loading the Python runtime…
xs = [3, 1, 2]print(xs.sort())
What does this print?
Loading the Python runtime…
for value in ([], {}, "", 0, 0.0, None, [0]):print(repr(value), bool(value))
What does this print?
Loading the Python runtime…
print(7 / 2)print(7 // 2)print(-7 // 2)
What does this print?
Loading the Python runtime…
print(1 < 2 < 3)print(3 > 2 > 1)print((1 < 2) < 3)
What does this print?
Loading the Python runtime…
a, b = 256, 256c, d = 257, 257print(a is b, a == b)print(c is d, c == d)
What does this print?
Loading the Python runtime…
print(0.1 + 0.2)print(0.1 + 0.2 == 0.3)
What does this print?
Loading the Python runtime…
print(type((1)).__name__)print(type((1,)).__name__)print(len((1,)))
What does this print?
Loading the Python runtime…
d = {"b": 1, "a": 2, "10": 3, "2": 4}print(list(d))
What does this print?
Loading the Python runtime…
original = [[1, 2], [3, 4]]copy = original.copy()copy[0].append(99)print(original)
What does this print?
Loading the Python runtime…
s = "abc"try:s[0] = "z"except TypeError as e:print("TypeError:", e)print(s)
What does this print?
Loading the Python runtime…
class Bag:items = []a, b = Bag(), Bag()a.items.append("x")print(b.items)
What does this print?
Loading the Python runtime…
class A:def __init__(self):self.__secret = 1a = A()print([n for n in vars(a)])print(a._A__secret)
What does this print?
Loading the Python runtime…
try:raise ValueError("boom")except ValueError as err:print(err)print("err" in dir())
What does this print?
Loading the Python runtime…
squares = (x * x for x in range(3))print(list(squares))print(list(squares))
What does this print?
Loading the Python runtime…
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…