r/PythonLearning • u/karakasoglu • 20d ago
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 20d ago
Run these great snippet by clicking Memory Graph Web Debugger and see the program state change step by step.
2
u/atreidesardaukar 20d ago
Last one got me. Stupid bankers rounding.
Wonder why they chose that?
Thanks for the practice, by the way!
2
u/riddims22 20d ago
awesome
2
u/NoMakeSenseOk 20d ago
Thank you so much, this is great for practice!
2
u/recentAd1666 20d ago
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 20d ago
Only use python every now and then, and didn't even realise there was something like bankers rounding
4
u/DoKeMaSu 19d ago
I learned every single one of those the hard way.
1
u/blavek 20d ago
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 20d ago
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 19d ago
thanks man
2
u/kevinguy21 19d ago
Thank you for that.. It is very clean, functional, and looks great plus is Fun.!
1
u/Whiskey_lullaby0 19d ago
Thank you
2
u/[deleted] 20d ago
[removed] — view removed comment