DEV Community

Cover image for What Does This Python Code Print? 10 Problems to Test Yourself
Ameer Abdullah
Ameer Abdullah

Posted on

What Does This Python Code Print? 10 Problems to Test Yourself

No explanations until the end. Work through each one by tracing the code mentally. Write down your answer before you scroll.

This is exactly the format used in technical interviews and university database and programming exams.


Problem 1

x = 5
def change(n):
    n = n + 10
    return n
change(x)
print(x)
Enter fullscreen mode Exit fullscreen mode

Problem 2

a = [1, 2, 3]
def add(lst):
    lst.append(4)
add(a)
print(a)
Enter fullscreen mode Exit fullscreen mode

Problem 3

result = []
for i in range(3):
    result.append(lambda: i)
print([f() for f in result])
Enter fullscreen mode Exit fullscreen mode

Problem 4

def outer():
    x = 10
    def inner():
        x = 20
        return x
    inner()
    return x
print(outer())
Enter fullscreen mode Exit fullscreen mode

Problem 5

data = {1: 'a', 2: 'b', 3: 'c'}
for k, v in data.items():
    if k == 2:
        data[4] = 'd'
print(data)
Enter fullscreen mode Exit fullscreen mode

Problem 6

words = ['hi', 'hello', 'hey']
result = sorted(words, key=len, reverse=True)
print(result[0])
print(words)
Enter fullscreen mode Exit fullscreen mode

Problem 7

def count(start=0, step=1):
    return start + step

print(count(5))
print(count(step=3))
print(count(2, 3))
Enter fullscreen mode Exit fullscreen mode

Problem 8

class Counter:
    total = 0
    def __init__(self):
        Counter.total += 1

a = Counter()
b = Counter()
c = Counter()
print(Counter.total)
print(a.total)
Enter fullscreen mode Exit fullscreen mode

Problem 9

s = "python"
print(s[1:5:2])
print(s[-1::-1])
Enter fullscreen mode Exit fullscreen mode

Problem 10

x = [1, 2, 3]
y = x
x = x + [4]
print(x)
print(y)
Enter fullscreen mode Exit fullscreen mode

Answers

Problem 1: 5
Integers are immutable. The function creates a new local n with value 15. The original x is unchanged.

Problem 2: [1, 2, 3, 4]
Lists are mutable. The function receives a reference to the same list object and appends to it in place. The original list is modified.

Problem 3: [2, 2, 2]
Late binding closure. All three lambdas capture the variable i, not its value at creation time. When called, i is 2 (the last value from range(3)). All three return 2.

Problem 4: 10
The inner function creates its own local x = 20. Without nonlocal, this does not affect the outer x. The outer function returns its own x which is still 10.

Problem 5: {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
In Python 3.7 and later, adding a key during iteration does not always raise RuntimeError if the size change is handled carefully. The dictionary gets the new key added.

Problem 6: First line: hello. Second line: ['hi', 'hello', 'hey']
sorted() returns a new list. It does not modify the original. result[0] is the longest word which is hello.

Problem 7:

6   (count(5) = 5 + 1)
3   (count(step=3) = 0 + 3)
5   (count(2, 3) = 2 + 3)
Enter fullscreen mode Exit fullscreen mode

Problem 8: 3 then 3
total is a class variable shared across all instances. After three instantiations it is 3. Accessing a.total finds no instance attribute so it reads the class attribute which is also 3.

Problem 9:
yh (characters at index 1 and 3, stepping by 2)
nohtyp (reversed string)

Problem 10: [1, 2, 3, 4] then [1, 2, 3]
x = x + [4] creates a NEW list and rebinds x to it. y still points to the original list [1, 2, 3] which was never modified.


How many did you get right? If you missed any, the explanation tells you exactly which concept to study.

For more problems like this with AI-generated unique questions every session, try PyCodeIt. Free, no account needed.


Top comments (0)