11 Tips And Tricks To Write Better Python Code

preview_player
Показать описание
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.

~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~

📚 Get my FREE NumPy Handbook:

📓 Notebooks available on Patreon:

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

~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

# 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 :)

patloeber
Автор

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

wgphvec
Автор

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

FailedSquare
Автор

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.

evanhagen
Автор

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.

Daniel-umye
Автор

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!

manuelmanolo
Автор

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

jordangl
Автор

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.

schedarr
Автор

I love how you explain with simplicity. Great content.

saurabhjain
Автор

Nr.3 you can also do:

from operator import itemgetter
sorted_data = sorted(data, key=itemgetter('age'))

vitalimueller
Автор

Great video. Please make more of these quick tips for comparisons of "beginner" python code vs experienced developer idioms

TheSuperUser
Автор

I almost don't know any python, but I was able to comprehend 80% of the content. Amazing simple explanation. Thanks.

eminm
Автор

Great collection of useful tips, presented very clearly and concisely. Thanks!!

thebuggser
Автор

Clear tips, like how you explain them, are simple and clear!

plumberski
Автор

Simply wonderful! Subscribed in the first 2 minutes! Python is the greatest modern language, and these tips are gold!

RicardoAmaralAndrade
Автор

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

jth
Автор

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.

Kinos
Автор

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.

etgaming
Автор

Thanks a lot! The first minute already helps a lot.

davidvanleeuwen
Автор

First example: return [max(i, 0) for i in data]

YouAreNotFree