DEV Community

15 Python tips and tricks every beginner should know !

AIDRI on August 11, 2021

Hy guys! Today I'm going to share with you the best tips and tricks to master in Python! These tips are based on my experience on Codingame, duri...
Collapse
 
deninpaulv profile image
Denin Paul

I see myself coming to this article again and again in the future

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
mccurcio profile image
Matt Curcio

Dear friend,
Your website does not have three links working, as far as I can see.

Thread Thread
 
zastixx profile image
TARUN KUMAR UTTAM

Yeah !!!
Because it is in under-construction....

Hey are you on in instagram follow my id is zastixx

Collapse
 
aidri profile image
AIDRI

Thank you very much !

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

A neat trick with sets

alist = [1, 2, 3, 3, 3, 4, 4, 5]
list(set(alist))
Enter fullscreen mode Exit fullscreen mode

will give you

[1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

There is also the reverse() method which reverses the list in-place, while [::-1] gives a new list in reversed order.

casper = [1,2,3,4]
casper.reverse()
Enter fullscreen mode Exit fullscreen mode

gives you

[5,4,3,2,1]
Enter fullscreen mode Exit fullscreen mode

Happy Coding

Collapse
 
aidri profile image
AIDRI

Yes! For the first trick, it is a very cool property of the set function to get unique values.
For the second trick, I explained in my post that this built-in function is much slower, but it is probably easier to remember, I admit :)

Collapse
 
grizzlysmit profile image
Francis Grizzly Smit • Edited

nice I didn't know some of that, like the a, *b, c = l thing and import this lol very cool

Collapse
 
sanzoghenzo profile image
Andrea Ghensi

Just a quick note (that can count as another trick): you can simplify the my_filter function to the return x==3 or x%2==0

Collapse
 
aidri profile image
AIDRI

It's true that you can use that, but I wanted to keep it pretty clear in my post. But your trick is useful in "small" functions like this one to avoid making the code ugly

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