Think You Know Python? These 20 Riddles Will Make You Think Again!
Welcome, Python enthusiasts! Today, I’m going to challenge your understanding of Python with 10 riddles. Each riddle is a small Python code snippet. Your task is to figure out what each snippet will print when executed.
And here’s a little secret: The answers to all these riddles will be revealed in the first comment below this article. But don’t peek until you’ve given each riddle your best shot!
Also, don’t forget to share your experience in the comments section below. How many riddles did you solve correctly? Which ones stumped you? Did you learn something new? Your feedback and insights are valuable to me and other readers.
Ready to test your skills? Let’s dive in!
Riddle 1: List mutability
x = [1, 2, 3]
y = x
y.append(4)
print(x)
What will this print?
Riddle 2: Default mutable arguments
def func(a, b=[]):
b.append(a)
return b
print(func(1))
print(func(2))
print(func(3))
What will each of these print calls output?
Riddle 3: Multiple assignment
a = 1
b = 2
a, b = b, a
print(a, b)
What will this print?
Riddle 4: Boolean arithmetic
print(True + True + True)
What will this print?
Riddle 5: String indexing and slicing
print("hello"[-1] + "world"[1])
What will this print?
Riddle 6: F-strings and formatting
x = 10
y = 50
print(f"{x}% of {y} is {x/100*y}")
What will this print?
Riddle 7: Operator precedence
print(2 * 3 ** 3 * 2)
What will this print?
Riddle 8: Function returns and string concatenation
def greet(name):
return f"Hello, {name}!"
print(greet("Alice") + " " + greet("Bob"))
What will this print?
Riddle 9: Range function
print(list(range(5, 0, -1)))
What will this print?
Riddle 10: String formatting with .format()
print("{2}, {1}, {0}".format('a', 'b', 'c'))
What will this print?
Riddle 11: Set operations
print(len(set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])))
What will this print?
Riddle 12: Dictionary copying
x = {'a': 1, 'b': 2}
y = dict(x)
y['c'] = 3
print(x)
What will this print?
Riddle 13: List comprehensions and character manipulation
print("".join([chr(ord(c) + 1) for c in "hello"]))
What will this print?
Riddle 14: Type coercion in multiplication
def multiply(a, b):
return a * b
print(multiply('5', 3))
What will this print?
Riddle 15: Functional programming (map and sum)
print(sum(map(int, "123")))
What will this print?
Riddle 16: Boolean conversion
print(bool("False"))
What will this print?
Riddle 17: List slicing
x = [1, 2, 3]
print(x[::-1])
What will this print?
Riddle 18: Rounding behavior
print(round(5.5), round(6.5))
What will this print?
Riddle 19: Itertools module
import itertools
print(list(itertools.permutations([1, 2, 3], 2)))
What will this print?
Riddle 20: The eval function
print(eval('2 + 3 * 4'))
What will this print?
That’s all for our Python riddles! How many did you get right? Remember, the best way to improve your Python skills is through practice and curiosity. Keep coding and exploring!
Engage with Me!
Did you enjoy this Python riddle challenge? I’d love to hear from you! Please take a moment to clap for this article if you found it helpful or entertaining. Your support motivates me to create more content like this.