Related courses
See All CoursesIntermediate
Databases in Python
"Databases in Python" is a hands-on course designed to teach you the fundamentals of working with databases using Python, focusing on the sqlite3 library and SQLAlchemy. You’ll learn how to store, modify, and retrieve data, build efficient queries, and configure databases for your projects. The course covers both SQL basics and the ORM (Object-Relational Mapping) approach, which allows you to interact with databases through Python objects. This course is perfect for beginners looking to deepen their skills in data management, application development, and information handling.
Intermediate
Flask Intensive Course: Web Development with Python
You will face 100% hands-on practice and complete the project by the end of the course. This course is perfect for those who have never worked with Flask before. You will acquire the expertise to effectively utilize Flask for your project development needs. You will embark on a journey to create your initial application, mastering the fundamentals, and progressively enhancing your project to unlock its full potential. I will guide you step-by-step during the course.
PEP 8
Fundamental Style Rules for Clean and Readable Python Code
PEP 8 (Python Enhancement Proposal 8) is a document that establishes standards for coding style in the Python programming language. In this article, we will explore key aspects of PEP 8, its core recommendations, and how adhering to these rules contributes to the creation of clean, readable, and consistent code.
Core Tenets of PEP 8
1. Variable and Function Naming:
- Use lowercase letters for names, separated by underscores (snake_case).
- Limit the length of names to 79 characters.
2. Indentation and Whitespace:
- Use spaces for indentation, not tabs.
- Use 4 spaces for each level of indentation.
3. Maximum Line Length:
- Limit the length of a line to 79 characters to ensure readability.
- For detailed documentation, the length can be extended to 72 characters.
4. Imports:
- Use separate lines for each import.
- Avoid using "wildcard" imports.
Run Code from Your Browser - No Installation Required
Сomments and Documentation
5. Comments:
- Use comments to explain complex sections of code.
- Avoid obvious comments; code should be self-explanatory.
6. Docstring Documentation:
- When adding documentation to functions and classes, use docstrings.
- Adhere to the reStructuredText standard for formatting documentation.
Other Important Recommendations
7. Logical Line Length:
- Limit the length of logical lines of code to 79 characters.
8. Exceptions:
- Use exceptions for real exceptional behavior, not for managing program flow.
Final Thoughts
PEP 8 defines rules that facilitate consistent code formatting within the Python community. Adhering to these standards not only improves the readability of your code but also contributes to the creation of a consistent and professional software overall. Strict adherence to PEP 8 is the key to producing clean, readable, and collaborative Python code.
Start Learning Coding today and boost your Career Potential
FAQs
Q: What specific recommendations does PEP 8 provide regarding the formatting of variable and function names in Python?
A: According to PEP 8, variable and function names should be written in lowercase and separated by underscores (snake_case). The length of names should not exceed 79 characters.
Q: What are the requirements from PEP 8 regarding indentation and whitespace in Python code?
A: PEP 8 recommends using spaces for indentation instead of tabs. It suggests using 4 spaces for each level of indentation.
Q: What does PEP 8 recommend regarding the maximum line length in Python code?
A: PEP 8 recommends limiting the length of a line to 79 characters to ensure readability. For detailed documentation, the maximum length can be extended to 72 characters.
Q: What rules does PEP 8 have for imports in Python?
A: PEP 8 recommends using separate lines for each import statement and avoiding the use of "wildcard" imports, as it can lead to namespace pollution.
Q: What does PEP 8 suggest regarding comments in Python code?
A: PEP 8 suggests using comments to explain complex sections of code. However, it emphasizes avoiding obvious comments, as the code should be self-explanatory.
Q: What are the core principles of PEP 8 regarding docstring documentation in Python?
A: PEP 8 recommends using docstrings for documenting functions and classes. The documentation should adhere to the reStructuredText standard for proper formatting.
Related courses
See All CoursesIntermediate
Databases in Python
"Databases in Python" is a hands-on course designed to teach you the fundamentals of working with databases using Python, focusing on the sqlite3 library and SQLAlchemy. You’ll learn how to store, modify, and retrieve data, build efficient queries, and configure databases for your projects. The course covers both SQL basics and the ORM (Object-Relational Mapping) approach, which allows you to interact with databases through Python objects. This course is perfect for beginners looking to deepen their skills in data management, application development, and information handling.
Intermediate
Flask Intensive Course: Web Development with Python
You will face 100% hands-on practice and complete the project by the end of the course. This course is perfect for those who have never worked with Flask before. You will acquire the expertise to effectively utilize Flask for your project development needs. You will embark on a journey to create your initial application, mastering the fundamentals, and progressively enhancing your project to unlock its full potential. I will guide you step-by-step during the course.
The SOLID Principles in Software Development
The SOLID Principles Overview
by Anastasiia Tsurkan
Backend Developer
Nov, 2023・8 min read
Match-case Operators in Python
Match-case Operators vs if-elif-else statements
by Oleh Lohvyn
Backend Developer
Dec, 2023・6 min read
30 Python Project Ideas for Beginners
Python Project Ideas
by Anastasiia Tsurkan
Backend Developer
Sep, 2024・14 min read
Content of this article