r/PythonLearning • u/karakasoglu • Oct 28 '26
Showcase 5 tiny Python snippets that trip up most beginners (answers hidden, guess first)
Five short snippets. Guess what each prints before you open the answer. Most people get two or three.
1.
=
=
Answer: >![1, 2, 3, 4]. b = a doesn't copy the list, both names point at the same list. Want a copy? b = a.copy() or b = a[:].!<
2.
=
Answer: >![10, 3, 27, 8], unchanged. sorted() returns a new list and the result was thrown away. Use nums.sort() to sort in place, or nums = sorted(nums).!<
3.
* 2
Answer: >!None. The function works out x * 2 but never returns it, and a function without return gives back None. Fix: return x * 2.!<
4.
= 5
= 12
Answer: >!5. Assigning to x inside the function makes a new local x; the outer one is untouched. The clean fix is to return the value (x = change()), not global.!<
5.
Answer: >!2 4. Python rounds exact halves to the nearest even number ("banker's rounding"), so 2.5 goes down and 3.5 goes up. If you need school-style rounding, use the decimal module with ROUND_HALF_UP.!<
I made a small free game out of traps like these: you read a snippet, pick what it prints, and one wrong answer ends the run. Questions get harder and faster as you go, and when you miss, it shows why and the moment in a YouTube tutorial that explains it. No sign-up, plays in the browser:
https://openinbeyond.com/code?utm_source=reddit&utm_content=pythonlearning
What's your score? And which of the five got you?
•
u/Sea-Ad7805 Oct 28 '26
Run these great snippet by clicking Memory Graph Web Debugger and see the program state change step by step.
2
u/atreidesardaukar Oct 28 '26
Last one got me. Stupid bankers rounding.
Wonder why they chose that?
Thanks for the practice, by the way!
2
u/riddims22 Oct 28 '26
awesome
2
u/NoMakeSenseOk Oct 28 '26
Thank you so much, this is great for practice!
2
u/recentAd1666 Oct 28 '26
Nice game. Could be better if you can choose staring level and have slower pace for begginers. It would be nice to be able to review all the questions after failing.
7
u/Nunc-dimittis Oct 28 '26
Only use python every now and then, and didn't even realise there was something like bankers rounding
4
u/DoKeMaSu Oct 29 '26
I learned every single one of those the hard way.
1
u/blavek Oct 28 '26
I tried your game. The timer doesn't really give enough time to evaluate the code, and the more complex it gets, the more true that becomes. Also, at least the explanation for the and operator isn't quite correct. It says that and requires the left and right to be true, but that's not the case. Either side can be true or false and if one or both of the sides is false and evaluates to false. The question was
x = 5
y = 2
print(x > 4 and y < 4)
The explanation should say something like this evaluates to true because both statements are true.
2
u/frnzprf Oct 28 '26
Related to point 1.
I often get making 2D-array wrong.
Maybe using tuples is more appropriate here, because the length shouldn't change.
2
u/MentalStage8006 Oct 29 '26
thanks man
2
u/kevinguy21 Oct 29 '26
Thank you for that.. It is very clean, functional, and looks great plus is Fun.!
1
u/Whiskey_lullaby0 29d ago
Thank you
2
u/[deleted] Oct 28 '26
[removed] — view removed comment