The PEP8 Python style guide

When you write code, you should adhere to the conventions of the programming language you use.

If you learn the right naming and formatting conventions right from the start, it will be easier to read code written by other people, and people w…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

When you write code, you should adhere to the conventions of the programming language you use.

If you learn the right naming and formatting conventions right from the start, it will be easier to read code written by other people, and people will find your code easier to read.

Python defines its conventions in the PEP8 style guide. PEP stands for Python Enhancement Proposals and it’s the place where all Python language enhancements and discussions happen. There are a lot of PEP proposals, all available at https://www.python.org/dev/peps/.

PEP8 is one of the first ones, and one of the most important, too. It defines the formatting and also some rules on how to write Python in a “pythonic” way.

You can read its full content here: https://www.python.org/dev/peps/pep-0008/ but here’s a quick summary of the important points you can start with:

  • Indent using spaces, not tabs
  • Indent using 4 spaces.
  • Python files are encoded in UTF-8
  • Use maximum 80 columns for your code
  • Write each statement on its own line
  • Functions, variable names and file names are lowercase, with underscores between words (snake_case)
  • Class names are capitalized, separate words are written with the capital letter too, (CamelCase)
  • Package names are lowercase and do not have underscores between words
  • Variables that should not change (constants) are written in uppercase
  • Variable names should be meaningful
  • Add useful comments, but avoid obvious comments
  • Add spaces around operators
  • Do not use unnecessary whitespace
  • Add a blank line before a function
  • Add a blank line between methods in a class
  • Inside functions/methods, blank lines can be used to separate related blocks of code to help readability


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-02-23T05:00:00+00:00) The PEP8 Python style guide. Retrieved from https://www.scien.cx/2021/02/23/the-pep8-python-style-guide/

MLA
" » The PEP8 Python style guide." flaviocopes.com | Sciencx - Tuesday February 23, 2021, https://www.scien.cx/2021/02/23/the-pep8-python-style-guide/
HARVARD
flaviocopes.com | Sciencx Tuesday February 23, 2021 » The PEP8 Python style guide., viewed ,<https://www.scien.cx/2021/02/23/the-pep8-python-style-guide/>
VANCOUVER
flaviocopes.com | Sciencx - » The PEP8 Python style guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/23/the-pep8-python-style-guide/
CHICAGO
" » The PEP8 Python style guide." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/02/23/the-pep8-python-style-guide/
IEEE
" » The PEP8 Python style guide." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/02/23/the-pep8-python-style-guide/. [Accessed: ]
rf:citation
» The PEP8 Python style guide | flaviocopes.com | Sciencx | https://www.scien.cx/2021/02/23/the-pep8-python-style-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.