Zen of Python: Difference between revisions

Content deleted Content added
Added section on being Pythonic
Being Pythonic: Add official definition
Line 57:
 
== Being Pythonic ==
One of the principles, "There should be one-- and preferably only one --obvious way to do it", can be described as the "Pythonic" way.<ref name=":0" /> The official definition of "Pythonic" is:<ref>{{Cite web |title=Glossary |url=https://docs.python.org/3/glossary.html |access-date=2024-02-07 |website=Python Documentation |language=en}}</ref><blockquote>An idea or piece of code which closely follows the most common idioms of the Python language, rather than implementing code using concepts common to other languages. For example, a common idiom in Python is to loop over all elements of an iterable using a <code>for</code> statement.</blockquote> Many other languages don’t have this type of construct, so people unfamiliar with Python sometimes use a numerical counter instead:
<syntaxhighlight lang="python">
for i in range(len(food)):
print(food[i])
</syntaxhighlight>
As opposed to the cleaner, Pythonic method:
<syntaxhighlight lang="python">
for piece in food:
print(piece)
</syntaxhighlight>
</blockquote>
 
== In practice ==