11 Tips And Tricks To Write Better Python Code
HTML-код
- Опубликовано: 10 июл 2024
- In this video, I show 11 Tips and Tricks to Write Better Python code! I show a lot of best practices that improve your code by making your code much cleaner and more Pythonic.
Find Python jobs: pythonengineer.pallet.com
~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~
✅ Write cleaner code with Sourcery: sourcery.ai/?... *
📚 Get my FREE NumPy Handbook:
www.python-engineer.com/numpy...
📓 Notebooks available on Patreon:
/ patrickloeber
⭐ Join Our Discord : / discord
If you enjoyed this video, please subscribe to the channel!
All Tips:
1) Iterate with enumerate instead or range(len(x))
2) Use list comprehension instead of raw for loops
3) Sort complex iterables with sorted()
4) Store unique values with Sets
5) Save memory with Generators
6) Define default values in Dictionaries with .get() and .setdefault()
7) Count hashable objects with collections.Counter
8) Format strings with f-Strings (Python 3.6+)
9) Concatenate strings with .join()
10) Merge dictionaries with {**d1, **d2} (Python 3.5+)
11) Simplify if-statements with if x in list
List comprehension Tutorial: • List Comprehension in ...
~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
🖥️ Website: www.python-engineer.com
🐦 Twitter - / patloeber
✉️ Newsletter - www.python-engineer.com/newsl...
📸 Instagram - / patloeber
🦾 Discord: / discord
▶️ Subscribe: / @patloeber
~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
🅿 Patreon - / patrickloeber
Music: www.bensound.com/
Python #Tips
----------------------------------------------------------------------------------------------------------
* This is a sponsored or an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
I hope you find these tips helpful! Let me know if you have any other Python tips that improve your code :)
Helpful but too many ads cut...
Can you iterate from say idx 2 to n-4 of a list using enumerate without slicing or any extra lines of code...
@@sudhanshuranjan9 ya membership test is faster in set
remarkable!
Thank you!! Cheers from Chile!
In Python 3.9.0 or greater we can merge dictionaries using `|`:
d1 = {"name": "Alex", "age": 25}
d2 = {"name": "Alex", "city": "New York"}
merged_dict = d1 | d2
I like this as it stays true to Pipe symbol
you rock
And
print(merged_dict == d1 | d2)
will print out true
This syntax is way simpler.
Is there an easy (and fast for large dictionaries) way to merge dictionaries in a way, that it includes every value of both dicts with the same key? for example, if d2 would include "name": "Luca" instead of "Alex", i would like the merged output to be like:
{"name": ["Alex", "Luca"], "age": 25, "city": "New York"}
0:20 Iterate with Enumerate x For Loops with If
1:02 List Comprehension x For Loops
1:51 Sort iterables with sorted()
3:00 Unique values with Sets
3:37 Generators replacement for Lists
4:58 default values for dictionary keys
6:06 Count objects with collections.Counter
7:39 f-Strings > str.format()
8:20 Build up strings with .join()
9:27 merge dictionaries - This feature is updated again in 3.9 using |
10:00 simplify if statements
Thanks for the summary :)
@@patloeber you made this in 11 min, I see what u did there
@@patloeber This is a very nice video for quick reference on these coding best practices. Can you please copy this list of times to the video Description for future reference. That makes the vid hugely helpful for in future.
Your videos are by far the most concise and easiest to assimilate compared to every other YT Python teacher (to me). Thanks for taking the time. Good stuff
Superb content. I am a C++ programmer, but since 2019 have been dabbling with python. Being pythonic is actually what I look for as of now. Thanks.
Great collection of useful tips, presented very clearly and concisely. Thanks!!
I thought this would be something that would go way over my head but, as some that recently started learning python, this was really valuable!
On the last tip it would be much faster to use a set instead of a list. Sets have constant lookup time but lists have O(n) lookup time.
To convert list into set you need to execute O(n) operation.
@@sshishov my point is you shouldn't even create a list in the first place. You should create a set to begin with
Agree, but sometimes lists are needed if you want to keep duplicates or you want to keep items in inserted order.
@@sshishov True, but if you want to check membership, say, n times, than its O(n) vs. O(n**2). It depends on the problem which data structure is better, as your second comment shows. But if you are only worried about runtime, then @Evan Hagen is correct: you basically cannot lose by using a set (I mean even if you have to convert first), because if you run it once, it is the same runtime, but if you do it many times, then set is the better choice.
@@andraspongracz5996 agree 👍
Thanks man! Nice video!!
Nice tips! Thankyou so much!
Thanks for these good tips !
Wonderful. Thank you!
very helpful. thank you!
Thanks a lot! The first minute already helps a lot.
Excellent information,Thanks
I almost don't know any python, but I was able to comprehend 80% of the content. Amazing simple explanation. Thanks.
Finally, how to do strings properly. I love using something like that in c#, and I'm glad it's on other languages like python.
Clear tips, like how you explain them, are simple and clear!
That is absolutely golden video. Extremaly useful tricks that will make your life way much easier. I've already used 10 out of 11 but still it's nice refresher.
Great to hear!
Nice tips. It speedup my code writing. Thanks, man.
really helpful, thanks a lot
I love how you explain with simplicity. Great content.
Thank you! Glad you like it!
Great video, Thanks.
Thanks for the tips, always great to listen to fellow Python devs!
Most excellent video!
Excellent video, very precise and nicely done!
Very useful, thanks ☺
Simply wonderful! Subscribed in the first 2 minutes! Python is the greatest modern language, and these tips are gold!
Thank you so much!
amazing tips, very very valuable. thank you for sharing.
Valuable tips! Thank you very much!
bro this was super helpful. thanks for this.
Awesome tips!
Some interesting tips, I'm just going through a reformat of a new script and this should help tidy and speed some operations.
Really useful tricks! Thanks for sharing.
awesome! thx a lot.
Thank you, this video was very helpful!
what a legend , ty very much man
Wonderful tips. Every single one is pretty useful.
Superb [ ] of tips. Thank you!
Great video. Please make more of these quick tips for comparisons of "beginner" python code vs experienced developer idioms
Thanks :)
one of the best python videos.
Really useful
Thanks, it would help!
THANKS that was useful...
You are Superman:) Thanks for all of sharing.
Thanks man, this was helpful
This is a very good video. Thank you very much , keep up the good work .
Nice tips, I'll save the video for later. Thanks!
Great tips!!
thank you!
such a great content. Thank you
Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.
Thanks for these tips! It's hard finding content outside beginner courses.
Hey, Excellent videos. The style is amazing! and more informative!. I am following you.
Great, thank you!
Very nice and useful tips
Awesome tips, thank you!
Glad it's helpful!
Thank you very much! Keep up with the good work!
Thanks, will do!
Still very relevant content, thanks for having this.
very informative video brother 👍
great lesson sir
Very informative great job
Thanks for share
Love these Python tips
I'm a beginner -ish and knew about half to 2/3rd, but also learned a few good tricks :)
Thanks
Thank you very much!
You're welcome!
Very very good !
Great tips 🙂 thanks
Great content and background music
thank you so much. keep it up you're the best.
thank you!
thanks for the useful sharing!!!
glad you like it!
I am so glad I found this channel.
Very useful tips and tricks!
Thank You
glad it's helpful :)
very helpful
Very helpful in refactoring my brain to be more pythonic!
This was very informative, thank you!
Excellent, thank you
Glad you like it!
Dude this is great, thanks!
happy to hear this :)
I subscribed thanks!
thanks for subscribing!
excellent!
Great video
Nr.3 you can also do:
from operator import itemgetter
sorted_data = sorted(data, key=itemgetter('age'))
Yes thanks for the tip :)
i like this
Very cool. Learn from you a lot
Glad to help
The idea of list comprehensions was new to me, but I was curious if there was an option for dictionary comprehensions and, sure enough, there is! Was able to clean up a lot of my dictionary for loops. Thanks!
and what you think about it ?ruclips.net/video/xdmijinrk-w/видео.html
Generators tip was quite a nice trick to know! So easy to be confounded with list generator.
yep it's very handy sometimes :)
Thank you.
perfect video, good job Python Engineer
Thanks so much!
Thank you, Subscribed to the channel
Thanks!
great video!
thank you!
This is great!! 😊
thanks :)
brilliant tips
thanks!
Thank you. Mind actually blown.
Thanks Sir
Great suggestions.
Glad you like it
Thank you
Another great video. Thanks for the amazing content.
Glad you like it :)
awesome and very helpful video
Glad it was helpful!
You did a good job, thanks
thanks!
Great one 👍
Thanks ✌️
If you aren't speeding up your videos during your scripting then you are a REALLY FAST typer, like holy crap. IDK how you can type those lists in under a second, that is crazy to me.
dude, I've been doing a programming course 12 weeks, I feel like f-strings are something we should have been taught immediately, why am I only learning it through you
I am your fan now , thx a ton mate for all these tips.
Awesome, thank you!
Please try adding videos on Scarpping, ML & analytics . 🙂
This is one of the best python related videos I have seen.
thanks a lot!