Python Book
🇺🇦 Stand with Ukraine🎓Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page

Was this helpful?

Edit on Git
  1. 6. Code Styling

Zen of Python

Also know as PEP 20. (PEP is proposal for enhancement)

🪄 Code:

import this

📟 Output:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

## Zen of Python (summary)

  • Readability is very important

    • 10 reads, 1 write

  • Simple is better than complex

    • Easier to maintain and upgrade

  • Motivations:

    • Beautiful code

    • "Pythonic way"

Update/clean advises

General advises

  • Use docstrings in modules and functions, add README.md to your project

  • Don't use inline comments too much - clear code is better than comment.

  • Split your code into small functions. Ideal length - less than 10 lines.

  • Variables should be lower-case with underscores all_lines and not AllLines or allLines.

  • Variables should be named properly with descriptive names( a, abc or function_1 are not good).

  • Modules ( .py files) should be max 500 lines of code.

  • Each module should have a clear purpose and name (e.g. data_processing.py, db_export.py ).

  • Use if __name__ == '__main__': code blocks if this module will be imported.

  • Use list comprehensions when it is suitable (not too long lines)

  • Use for loops and don't forget about continue and break/else clauses.

  • When iterating - don't use for i in range(len(list_)), use enumerate() instead

Previous6. Code StylingNextLint

Last updated 2 years ago

Was this helpful?