11 Tips And Tricks To Write Better Python Code

601,602
0
Published 2020-07-05
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/?utm_source=youtube&utm_campaign=pytho… *

📚 Get my FREE NumPy Handbook:
www.python-engineer.com/numpybook

📓 Notebooks available on Patreon:
www.patreon.com/patrickloeber

⭐ Join Our Discord : discord.gg/FHMg9tKFSN

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 Python - A Pyth...  

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

🖥️ Website: www.python-engineer.com/
🐦 Twitter - twitter.com/patloeber
✉️ Newsletter - www.python-engineer.com/newsletter
📸 Instagram - www.instagram.com/patloeber
🦾 Discord: discord.gg/FHMg9tKFSN
▶️ Subscribe:    / @patloeber  

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

🅿 Patreon - www.patreon.com/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 sup

All Comments (21)
  • @patloeber
    I hope you find these tips helpful! Let me know if you have any other Python tips that improve your code :)
  • @user-wg2ph7ve8c
    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
  • @FailedSquare
    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
  • @evanhagen7084
    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.
  • @jordangl1
    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
  • @Daniel-um9ye
    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.
  • @thebuggser2752
    Great collection of useful tips, presented very clearly and concisely. Thanks!!
  • @eminm6383
    I almost don't know any python, but I was able to comprehend 80% of the content. Amazing simple explanation. 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!
  • @schedarr
    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.
  • @Kinos141
    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.
  • Simply wonderful! Subscribed in the first 2 minutes! Python is the greatest modern language, and these tips are gold!
  • Nr.3 you can also do: from operator import itemgetter sorted_data = sorted(data, key=itemgetter('age'))
  • @ekkyarmandi
    Nice tips. It speedup my code writing. Thanks, man.