DEV Community

Discussion on: 15 Python tips and tricks every beginner should know !

Collapse
aidri profile image
AIDRI Author

Very nice description of your comment ;)

By the way, can you tell me where my mistakes are? When you say it's irrelevant and outdated, I want to point out that this post is for beginners and that these are tricks I see regularly on CodinGame.

You say that my post is misleading. As far as I know, the title corresponds perfectly to what I do: I present 15 techniques that beginners should know, not to use in all cases, but that can be interesting in some competitions, or in projects. Also, if you were expecting to see tricks that nobody knows, you won't find them here, since it is for beginners.

Since your comment is very poorly constructed and is very nasty, I'll report it to dev.to!

Have a nice day, Adrien

Collapse
veky profile image
Veky • Edited on

Though terse, my comment was justified. I have no problem explaining, but you must understand that I encounter heaps of SIWOTIes (urbandictionary.com/define.php?ter...) and many times people never read my comments. So I just post something terse to provoke a reaction, to see if there is someone who wants to learn on the other side. You replied, so here goes.

\1. Yes, range is a sequence, but it is not a list. Your "output" (which is misleadingly labeled as >>> as if it's a real output of Python, though it's not) is incorrect. range was a list in Python 2, so that's why I said your 'lesson' is outdated (Py2 was killed 20 months ago, and was on life support for more than 5 years).

But there is a bigger problem. What exactly are you trying to teach here? That instead of a for loop, range, list literals, and loop.append method, people should use... range? It makes no sense. Anyone capable of understanding the first code snippet must already know about range. Since it's used in the first snippet also.

\2. Concatenating two dictionaries can be a useful operation to group information... and that's why Python provides an operator for that. a|b is what you need. .update is in most cases not what people need, since it is "concatenation in place" (it modifies a instead of constructing a new value, as other concatenations do). In fact the fact that .update is subtly wrong (and {**a, **b} is even more subtly wrong) has lead to the introduction of a dedicated operator for that operation.

\3. I'm not even sure what are you trying to say here. "creating (in one line) a dictionary where the key depends on x and where the value also depends on x" -- what's x? Do you simply mean a dict comprehension? Why not say so then? Also, I've never seen dicts written with keys and values in parentheses... what's with that? There's really no ambiguity -- x + (2: x**2) + 1 is meaningless anyway.

Also, if keys go from 2 till 6, why not say so directly? for key in range(2, 6).

\4. First, who gave you an idea that reversed function is slower than slicing ::-1? Besides, it's an apples-to-oranges comparison, since these two serve very different purposes. reversed is meant for iterating over the list in the opposite order, without any changing of the list elements' positions, while slicing with ::-1 is meant for making a new list with the same elements as the first one, but in the opposite order.

A bigger problem: not mentioning at all the idiomatic way to do what you wanted. If you want to reverse my_list in place (though it's very rarely needed), my_list.reverse() is what you should write.

\5. You haven't really tried to execute that, right? :-] You have a syntax error there. * on the RHS of assignment has nothing to do with unpacking, it is for re-packing a tuple (or any other iterable) inside something else.

Besides, a, b, c, d = my_tuple is perfectly fine (except the names are horrible, but you know that).

\6. Seriously? 9 lines just to filter 3 and even numbers from a list? Ok, here are some lessons for you. First, my_filter(x) can just be return x==3 or not x%2.

But really, the whole thing is much more easily written (and read) as [x for x in my_list if x==3 or not x%2]. It's funny you explain dict comprehension in 3, but don't use much easier list comprehension in 6.

\7. Generators are very useful, but really, I think noone would call them "return multiple values from a function". My first association was "return one_value, another_value". Look at geeksforgeeks.org/g-fact-41-multip... -- there are 5 things mentioned (2 is what I had in mind) but none of them have anything to do with generators.

\8. Instead of showing how to print ZoPy on the screen, it would be much more useful if you read it and thought about it for a while. ;-) Or at least, inspect.getsource and show how the this module was carefully written to violate every single line in ZoPy. :-D

\9. Those set operations are useful, yes. But they are not the only ones (where's -?), and they aren't even the most useful ones. Beginners usually forget about comparisons, writing horribilities such as s&t == t instead of t <= s.

\10. "It's not the most useful trick, but it's always useful" -- do you read what you write?? Anyway, yes, branching expression is useful, but if your condition is literally True, it is probably the worst example you could find. "Why didn't they just stop writing after i=1?!"

\11. Wat? First, recursion limit has nothing to do with number of recursions, but with depth of any single recursive call. Second, why showing how to change the recursion limit by "changing" it to its default value?

And most importantly, "to avoid having timeouts"? So what, you'd rather have a RecursionError with a 500 frames of traceback, than a correct result at a later time? Ah, kids these days... :-) If you really want to limit the time it takes to execute your function, futures or threads seems like a much more direct (and precise) way to do it that limiting the depth of recursion.

\12. "8 times faster" seems like a very precise number. Putting aside the fact that again you're comparing apples to oranges (print does many things besides calling $file.write), I wouldn't be surprised that looking up the attributes (stdout to sys, then write to that) costs quite a bit of time. Have you measured it, or just read it somewhere? Not everything you read on the internet is correct, you know. ;-)

\13. A death by a thousand cuts. It has nothing to do with middle (as you say later, it can be any position, or even no position at all), underscore is also a variable same as others, and nothing's being "put" anywhere when you assign it a name in Python.

\14. Nice... but again, you could have a more useful example. A billion I'd much rather write as 10**9 than this. But 299_792_458 is much better, for example. ;-)

\15. Yes, but is it any different from item #3? Or did you just write it in order to be able to say you have 15 items? In fact, since it mentions keys and values, maybe a more instructive way would be dict(zip(my_dic.values(), my_dic.keys())). ;-)

You've probably learned something from this. If DEV bans me, many people will be deprived of that opportunity in the future. Just something to think about. Being offended is not necessarily a bad thing. ;-)

Thread Thread
aidri profile image
AIDRI Author

First of all, I would like to point out that you are very pretentious and that you are looking for problems that don't exist just to prove to yourself that you are smarter than others, which is actually not the case :)
A piece of advice, come down to earth.
Just because you get stuck on an ex maths test or something like that doesn't mean you have to yell at me. If you need help I can help you if you want :)

1/ It's a mistake on my side, I'm on an IPad so I didn't execute all of these scripts.
2/ The update method is used by several versions of Python while the | operator was only introduced in Python 3.9
3/ I don't want to comment, you're looking for problems where there are none, it's not because there is a shortcut for this case that it's the same everywhere.
4/ You keep looking for problems where there are none. I compare two methods that give the same result, I don't look at how they get there.
5,6,7,8,9,10/ There are no problems, you just want to shout :)
11/ I wasn't clear in my wording, but I will rephrase it as you told me.
12/ I didn't test it by myself, but it's a site to prepare IOI that says it.
13,14,15.1/ Again, there are no problems.
15.2/ It's another method indeed, it's even the one I use, but I wanted to keep it simple in my post which is aimed at beginners.

PS: I will delete our discussion in 24 hours so that I leave you the time to read, but it does not disturb the others (I modified the problem of the range and the unpack)

Thread Thread
veky profile image
Veky

So, you can just delete what I've written? It's nice to know that. I'll have to be more careful in the future. :-)

Thread Thread
aidri profile image
AIDRI Author

I mean, your comment is only constructive on two points, it's more criticism so that you try to prove something to yourself the rest of the time

Thread Thread
veky profile image
Veky

I understand that it's how you view it. We can discuss it too, but it probably won't be productive -- you're too closed-minded for that.

What was news to me is that you can just delete what I've written. Are you a moderator, or anyone here has absolute power over comments on their posts?

Thread Thread
aidri profile image
AIDRI Author

:) maybe I’m a mod

Some comments have been hidden by the post's author - find out more