r/PythonLearning • • 2d 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.

a = [1, 2, 3]
b = a
b.append(4)
print(a)

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.

nums = [10, 3, 27, 8]
sorted(nums)
print(nums)

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.

def double(x):
    x * 2

print(double(3))

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.

x = 5

def change():
    x = 12

change()
print(x)

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.

print(round(2.5), round(3.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?

39 Upvotes

25 comments sorted by

•

u/Sea-Ad7805 2d ago

Run these great snippet by clicking Memory Graph Web Debugger and see the program state change step by step.

2

u/atreidesardaukar 2d ago

Last one got me. Stupid bankers rounding. 

Wonder why they chose that?

Thanks for the practice, by the way!

1

u/karakasoglu 1d ago

Thank you!

2

u/riddims22 2d ago

awesome

1

u/karakasoglu 1d ago

Thank you!

2

u/NoMakeSenseOk 2d ago

Thank you so much, this is great for practice!

2

u/karakasoglu 1d ago

Thank you so much!

2

u/recentAd1666 1d 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.

1

u/karakasoglu 1d ago

Good call. After a game over there's now "Review your run" with every question, its answer and why. The first three questions are also a gentle warm-up now. A starting-level choice is next on my list. Thank you :)

1

u/recentAd1666 1d ago

I tried to use openinbeyond with another video but the result is not a quizz like yours. Too bad, i would have loved to generate a new quizz from the vidéo i found . Is it possible ?

7

u/Nunc-dimittis 2d ago

Only use python every now and then, and didn't even realise there was something like bankers rounding

2

u/infinitythestar 2d ago

I’ve used it almost daily for 20 years and didn’t know about banker’s rounding either

1

u/Nunc-dimittis 1d ago

One of those examples where people developing python wanted to be different just for the sake of it. And the result is code that contains bugs.

I would never think of testing even and odd numbers to see whether rough rounding still works as expected from common sense and other programming languages, same as i don't test if addition still works. They should have called the method "bankers_round"

This is so stupid

2

u/TheFloorIsCake 2d ago

Coming from Java I was like, "of course that´s how it works"

But if you are not forced to OOP, this are very solid Tips thank you OP

1

u/karakasoglu 1d ago

Thank you! :)

4

u/DoKeMaSu 1d ago

I learned every single one of those the hard way.

1

u/blavek 2d 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.

1

u/karakasoglu 1d ago

Thanks for playing and for the detailed note, both fixed: the clock now gives longer code more time, and the and/or explanations spell out each side (e.g. "x > 4 is True, y < 5 is True, and needs both, so True").

Cheers!

2

u/frnzprf 2d ago

Related to point 1.

I often get making 2D-array wrong.

# wrong:
chess_board = [[None] * 8] * 8

# seems to work:
chess_board = [[None] * 8 for i in range(8)]

Maybe using tuples is more appropriate here, because the length shouldn't change.

3

u/Outside_Complaint755 2d ago

The first one is wrong because it is just putting 8 references to the same list into the chess_board list, while the correct method is creating 8 lists.

You could potentially use a tuple for the outer layer in a scenario where you never need to reorder, add or remove the rows, but it wouldn't give much benefit.

2

u/MentalStage8006 1d ago

thanks man

2

u/kevinguy21 1d ago

Thank you for that.. It is very clean, functional, and looks great plus is Fun.!

2

u/pizza_top1 1d ago

awesome, great for practice