The Zen of Python (PEP 20) is a set of nineteen aphorisms by Tim Peters that serve as guiding principles for Python’s design.
A Thread 🧵👇
#python #codanics #pythonprogramming #DataScience #programming #pythonchilla #pythonkachilla2 #zenofpython #pep20 https://t.co/lRI1eFNNsY
2/19- Explicit is better than implicit
Make sure anyone reading your code understand its functionality instead of obscure syntax. Anyone new to python should also be able to understand it to a greater extent. https://t.co/6Dfth2ZRZG
3/19- Simple is better than complex
You can do a task in both simple and complex ways.
But its much more efficient to do it in a simple way that your colleagues can understand. https://t.co/4gQuweNWQB
4/19- Complex is better than complicated
Sometimes you'd face problems of complex nature in real world. Their solution can be complex if not simple but they shouldn't be complicated for experts. https://t.co/LrxM8cdi2M
5/19- Flat is better than nested
This can relate to many things such as sub-categories within libraries, classes etc. For our example we're simply going to look at if-else statements to clarify it. https://t.co/sCH8H6PqVy
6/19- Sparse is better than dense
Usually some programmers tend to go for one-liner codes making it dense. Best practice is to sparse it by creating multiple functions. https://t.co/4zC2tiPUbW
7/19- Readability counts
As the name suggests, the more readable a code is the better its understood.
Readability = explicit naming + simple solutions + sparse structure https://t.co/MnIXALJZxp
8/19- Special cases aren’t special enough to break the rules
In programming there are multiple ways to solve the same problem. It doesn't mean you always have to stick to your own ways. Try to follow best practices whenever possible https://t.co/wLjvl8CPD0
9/19- Although practicality beats purity
This may seem like a contradiction with the previous statement. But there are times when you don't have enough time to follow all rules in a project and thus you can break a few rules
10/19- Errors should never pass silently
Always raise an error message whenever you're applying try and except.
Don't let the error pass without any notification https://t.co/fytF6jU7SH
11/19- Unless explicitly silenced
As for some cases where we don't want to get bothered over trivial errors.
We silence those errors since practicality beats purity! https://t.co/vwEsDzjxWF
12/19- In the face of ambiguity, refuse the temptation to guess
If you look at a code and try to guess what it does then that's an incorrect approach. Always break it down to smaller pieces and then figure them all out and put it together. https://t.co/tI1qzAyZRy
13/19- There should be one-- and preferably only one --obvious way to do it
There are many ways to solve a problem but as you code repeatedly you go from complex to simpler solutions. Always make sure you know what the optimal way is after experimentation. https://t.co/2yn26hng0q
14/19- Although that way may not be obvious at first unless you're Dutch
Its a reference to the creator of Python "Guido van Rossum" since he's a Dutch. There're many ways for a solution but only the Dutch would know the optimal one, from complex to simpler solutions.
15/19- Now is better than never
If you know you can optimize the code, you've an idea you want to implement, or there's some tweaking you would like to try then now is the time to do it. Its like don't get stuck in a loop of procrastination.
16/19- Although never is often better than "right" now
"You aren't gonna need it" (YAGNI) principle does apply if your project has a time limit. You can skip some optimizations while delivering the project at a deadline.
17/19- If the implementation is hard to explain, it's a bad idea
If you've completed a project and you're unable to explain it to others in simpler terms then it means you either have lack of knowledge or the solution is way too complex.
18/19- If the implementation is easy to explain, it may be a good idea
If you've completed a project and you're able to explain it to others in simpler terms then it means you have knowledge about it even if its a bad code. At least you're on the right track.
19/19- Namespaces are one honking great idea - let’s do more of those!
A namespace is an abstraction used in Python to organize names assigned to objects in a program.
Python using namespaces is an awesome concepts that makes it easier for developers.
You can always get the "Zen of Python" within python by using the command:
import this https://t.co/EHMlT9bWFG
Hope you enjoyed PEP 20. If you liked it then re-tweet it and follow me for more exciting stuff 😊🥳