Resource mentioned in this lesson: Writing Beautiful Pythonic Code With PEP 8 (Video Course)
Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Examining Pythonic Code
00:00 You might have heard the phrase Pythonic code before, but what does that mean? First, consider a quote from Python Enhancement Proposal, or PEP 20, The Zen of Python: Readability counts.
00:12 It starts there. PEP 8 is the official style guide for Python code. Its guidelines include things like: use four spaces per indentation level, limit your lines to a maximum of 79 characters, always choose clear, descriptive names, avoid leaving unnecessary whitespace, and keep your imports organized, among many others.
00:34 I feel you getting overwhelmed, but you don’t need to memorize all of these recommendations. In fact, editors and IDEs can flag style issues as you write code.
00:43 You can also use linters to check your code and report style and quality issues, and even formatters to actually update your code and imports automatically.
00:52 And all of these can be done based on rules derived from PEP 8 or any other Python style guide of your choosing.
01:00 Some common linters are Flake8, Pylint, and pycodestyle, whereas some common formatters include Black, Ruff, and isort. But most importantly, a style guide is about consistency.
01:15 PEP 8 provides guidance, not a prescription. You’ll always want to follow the style already used in a project, or the guide chosen by your team. Prefer readability over rigid rule-following, and use these tools to build good habits over time.
01:30 You can learn more about PEP 8 and Python code styles in Writing Beautiful Pythonic Code With PEP 8.
01:38 And next up, how to build on what you’ve seen so far to unlock even more uses for Python.
Become a Member to join the conversation.