Podcast
Questions and Answers
A variable's name should be ______ enough to convey its purpose.
A variable's name should be ______ enough to convey its purpose.
descriptive
In Python, functions are typically named using ______ case.
In Python, functions are typically named using ______ case.
lowercase
A variable that stores a person's age should be named something like ______.
A variable that stores a person's age should be named something like ______.
student_age
When naming a class, use ______ case to separate words.
When naming a class, use ______ case to separate words.
Constants in Python are conventionally named in ______ case.
Constants in Python are conventionally named in ______ case.
Avoid using single-letter variable names like 'i', 'O', or 'I' as they can be mistaken for ______.
Avoid using single-letter variable names like 'i', 'O', or 'I' as they can be mistaken for ______.
A function that calculates the factorial should be named something like ______.
A function that calculates the factorial should be named something like ______.
Python encourages the use of ______ spaces to improve code readability.
Python encourages the use of ______ spaces to improve code readability.
Avoid extra whitespace ______ parentheses, brackets, or braces.
Avoid extra whitespace ______ parentheses, brackets, or braces.
Place whitespace ______ a comma, semicolon, or colon.
Place whitespace ______ a comma, semicolon, or colon.
Don't put whitespace ______ the open parenthesis of a function call.
Don't put whitespace ______ the open parenthesis of a function call.
Avoid whitespace ______ the open bracket of an index or slice.
Avoid whitespace ______ the open bracket of an index or slice.
Don't put whitespace ______ a trailing comma and a closing parenthesis.
Don't put whitespace ______ a trailing comma and a closing parenthesis.
Avoid using whitespace to ______ assignment operators.
Avoid using whitespace to ______ assignment operators.
Instead of comparing boolean values to True or False with the ______ operator, use a direct boolean evaluation in the "if" statement.
Instead of comparing boolean values to True or False with the ______ operator, use a direct boolean evaluation in the "if" statement.
In "if" statements, utilize the fact that ______ sequences are falsy.
In "if" statements, utilize the fact that ______ sequences are falsy.
Docstrings should be surrounded by three ______ quotes on either side.
Docstrings should be surrounded by three ______ quotes on either side.
The ______ that ends a multiline docstring should be on a line by itself.
The ______ that ends a multiline docstring should be on a line by itself.
Docstrings should be written for all public ______, functions, classes, and methods.
Docstrings should be written for all public ______, functions, classes, and methods.
Surround ______ operators with a single space on either side.
Surround ______ operators with a single space on either side.
When there's more than one operator in a statement, it is better to only add whitespace around the operators with the ______ priority.
When there's more than one operator in a statement, it is better to only add whitespace around the operators with the ______ priority.
In slices, the ______ acts as a binary operator.
In slices, the ______ acts as a binary operator.
Adding whitespace around the ______ sign when assigning a default value to a function argument is not recommended.
Adding whitespace around the ______ sign when assigning a default value to a function argument is not recommended.
The ______ of Python states that "Sparse is better than dense."
The ______ of Python states that "Sparse is better than dense."
Flake8 checks code against coding style known as ______.
Flake8 checks code against coding style known as ______.
To install Flake8, you can use the command 'python -m pip install ______'.
To install Flake8, you can use the command 'python
The command 'black' is an example of an ______ that formats code to comply with PEP 8.
The command 'black' is an example of an ______ that formats code to comply with PEP 8.
If Flake8 finds errors in a file, it will list all the ______ in that file.
If Flake8 finds errors in a file, it will list all the ______ in that file.
To run Black on a file, you would use the command 'black ______'.
To run Black on a file, you would use the command 'black ______'.
Limit the line length of comments and docstrings to ______ characters.
Limit the line length of comments and docstrings to ______ characters.
Block comments should be indented to the same level as the ______ they describe.
Block comments should be indented to the same level as the ______ they describe.
Each line of a block comment should start with a # followed by a ______.
Each line of a block comment should start with a # followed by a ______.
Inline comments should be separated by two or more spaces from the ______.
Inline comments should be separated by two or more spaces from the ______.
Documentation strings, or ______, are used to explain a specific block of code.
Documentation strings, or ______, are used to explain a specific block of code.
Inline comments should be written on the same line as the ______ they refer to.
Inline comments should be written on the same line as the ______ they refer to.
Docstrings are enclosed in double or single ______ marks.
Docstrings are enclosed in double or single ______ marks.
PEP 8 provides rules for writing block comments and ______ comments.
PEP 8 provides rules for writing block comments and ______ comments.
The code uses a ______ indent when aligning a block with the opening delimiter.
The code uses a ______ indent when aligning a block with the opening delimiter.
In PEP 8, if a line exceeds a certain length, it is recommended to use ______ during line continuation.
In PEP 8, if a line exceeds a certain length, it is recommended to use ______ during line continuation.
To improve readability, add a ______ after the condition in 'if' statements.
To improve readability, add a ______ after the condition in 'if' statements.
When writing large expressions, it is important to keep them ______.
When writing large expressions, it is important to keep them ______.
PEP 8 suggests two options for the position of the ______ in line continuations.
PEP 8 suggests two options for the position of the ______ in line continuations.
The first option for closing brace placement is to line it up with the first non-______ character of the previous line.
The first option for closing brace placement is to line it up with the first non-______ character of the previous line.
Adding ______ indentation can help distinguish line continuations from code blocks.
Adding ______ indentation can help distinguish line continuations from code blocks.
Documenting code is crucial for ______ understanding and collaboration.
Documenting code is crucial for ______ understanding and collaboration.
Flashcards
Descriptive Naming
Descriptive Naming
Use descriptive names for variables, functions, classes, etc. to make your code understandable. Choose names that clearly represent what the object represents.
Avoid Confusing Names
Avoid Confusing Names
Avoid using inappropriate names like 'i', 'O', or 'I' as single letters. They can be mistaken for '1' and '0', leading to debugging complications.
Function/Method Naming
Function/Method Naming
Functions and methods should use lowercase words with underscores to separate words. Example: 'find_sum()' or 'validate()'.
Variable Naming
Variable Naming
Signup and view all the flashcards
Class Naming
Class Naming
Signup and view all the flashcards
Constant Naming
Constant Naming
Signup and view all the flashcards
Module Naming
Module Naming
Signup and view all the flashcards
Package Naming
Package Naming
Signup and view all the flashcards
PEP 8
PEP 8
Signup and view all the flashcards
Block Indentation
Block Indentation
Signup and view all the flashcards
Alignment with Opening Delimiter
Alignment with Opening Delimiter
Signup and view all the flashcards
Hanging Indent
Hanging Indent
Signup and view all the flashcards
Indentation in If Statements
Indentation in If Statements
Signup and view all the flashcards
Comments
Comments
Signup and view all the flashcards
Double Indentation
Double Indentation
Signup and view all the flashcards
Placement of Closing Brace
Placement of Closing Brace
Signup and view all the flashcards
Block Comment
Block Comment
Signup and view all the flashcards
Inline Comment
Inline Comment
Signup and view all the flashcards
Comment Sentence Structure
Comment Sentence Structure
Signup and view all the flashcards
Docstring
Docstring
Signup and view all the flashcards
Comment Conciseness
Comment Conciseness
Signup and view all the flashcards
Keeping Comments Up-to-Date
Keeping Comments Up-to-Date
Signup and view all the flashcards
Comment Line Length
Comment Line Length
Signup and view all the flashcards
Block Comment Formatting
Block Comment Formatting
Signup and view all the flashcards
Flake8
Flake8
Signup and view all the flashcards
python -m pip install flake8
python
Signup and view all the flashcards
flake8 path/to/code/code.py
flake8 path/to/code/code.py
Signup and view all the flashcards
Autoformatter
Autoformatter
Signup and view all the flashcards
Black
Black
Signup and view all the flashcards
Whitespace in Python
Whitespace in Python
Signup and view all the flashcards
Whitespace Around Binary Operators
Whitespace Around Binary Operators
Signup and view all the flashcards
Whitespace with Multiple Operators
Whitespace with Multiple Operators
Signup and view all the flashcards
Whitespace in Slices
Whitespace in Slices
Signup and view all the flashcards
Default Value Assignment
Default Value Assignment
Signup and view all the flashcards
When to Avoid Whitespace
When to Avoid Whitespace
Signup and view all the flashcards
Conditional Execution with if
Statements
Conditional Execution with if
Statements
Signup and view all the flashcards
Inappropriate Whitespace: Inside Brackets
Inappropriate Whitespace: Inside Brackets
Signup and view all the flashcards
Inappropriate Whitespace: Before Comma, Semicolon, or Colon
Inappropriate Whitespace: Before Comma, Semicolon, or Colon
Signup and view all the flashcards
Inappropriate Whitespace: Function Calls
Inappropriate Whitespace: Function Calls
Signup and view all the flashcards
Inappropriate Whitespace: Index or Slice
Inappropriate Whitespace: Index or Slice
Signup and view all the flashcards
Inappropriate Whitespace: Trailing Comma
Inappropriate Whitespace: Trailing Comma
Signup and view all the flashcards
Inappropriate Whitespace: Alignment
Inappropriate Whitespace: Alignment
Signup and view all the flashcards
Boolean Comparison
Boolean Comparison
Signup and view all the flashcards
Empty Sequences
Empty Sequences
Signup and view all the flashcards
Study Notes
PEP 8 Coding Standards
- PEP 8 is a document providing guidelines and best practices for writing Python code.
- It was created in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan.
- The primary goal of PEP 8 is to improve code readability and consistency.
- It outlines coding conventions for the standard Python library.
Learning Outcomes
- Students will be able to write Python code following PEP 8 standards.
- They will understand the rationale behind PEP 8 guidelines.
- Students will set up their development environments for writing Python code.
- They will understand Python code compliant with PEP 8.
Introduction
- Code readability is crucial since code is read much more often than it's written
- Code readability directly reflects professionalism in programming
- Learning and applying PEP 8 enhances writing clear and understandable code that is maintainable.
- Remembering code after a while is challenging for beginners.
Why PEP 8?
- PEP 8 guides improve the readability of Python codes.
- Proper naming conventions improve code comprehension.
- Consistent use of whitespace improves the logical flow and structure of code.
- Well-documented code makes collaboration and maintenance easier.
- Following PEP 8 creates more structured and maintainable codebases for better developer efficiency and experience.
Naming Conventions
- Functions and methods use lowercase words with underscores separating them (e.g.,
find_sum()
). - Variables use lowercase single words or words separated by underscores (e.g.,
student_age
). - Classes use CamelCase (e.g.,
Student
). - Constants use uppercase words separated by underscores (e.g.,
USER_TYPE
). - Modules use short, lowercase words separated by underscores (e.g.,
module1.py
). - Packages use short, lowercase words without underscores (e.g.,
userutilities
).
Code Layout
- Use four spaces for indentation.
- Spaces are generally preferred over tabs.
- Use blank lines to improve readability. This is especially important at function and method boundaries, and for top-level code elements.
- Line length should be limited to 79 characters.
- Long lines can be broken using parentheses, brackets, and braces.
- When breaking long statements, align the continuation line with the opening delimiter, or use a 'hanging indent'.
Documentation and Comments
- Comments and documentation strings (docstrings) are important for explaining code functionality and logic.
- Docstrings (multiline strings beginning and ending with triple-quotes) should be used for functions, classes, methods, and modules.
- Line length should be limited, with complete sentences and capitalised first letters.
Inline and Block Comments
- Use inline comments sparingly.
- Use white space to separate inline comments from code.
- Write inline comments on the same line as the statement being commented.
- Use block comments to document sections of code that need more detailed explanation.
- Start block comments with a '#' char followed by a single space.
Whitespace Around Binary Operators
- Use a single space around binary operators: =, +=, -=, etc.
- Use a single space around comparison operators: ==, !=, <, >, >=, <=.
- Use a single space around logical operators: and, or, is or is not, in.
- Do not use spaces when the symbol "=" sets a default value for a function argument.
When to Use PEP 8
- General recommendation to increase code consistency and readability
- Best to follow standards when working with other developers on a project.
- Use PEP 8 to ensure code remains compatible with other current Python libraries
- Avoid discrepancies in style, formatting, and conventions.
- Avoid unnecessary errors associated with inconsistent code.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz covers the PEP 8 coding standards, which provide essential guidelines for writing clear and consistent Python code. Students will learn the importance of code readability, the rationale behind the guidelines, and how to set up their development environments accordingly. By the end, participants will be better equipped to write maintainable Python code that adheres to best practices.