PEP 8 Coding Standards
46 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

A variable's name should be ______ enough to convey its purpose.

descriptive

In Python, functions are typically named using ______ case.

lowercase

A variable that stores a person's age should be named something like ______.

student_age

When naming a class, use ______ case to separate words.

<p>camel</p> Signup and view all the answers

Constants in Python are conventionally named in ______ case.

<p>uppercase</p> Signup and view all the answers

Avoid using single-letter variable names like 'i', 'O', or 'I' as they can be mistaken for ______.

<p>numbers</p> Signup and view all the answers

A function that calculates the factorial should be named something like ______.

<p>find_factorial</p> Signup and view all the answers

Python encourages the use of ______ spaces to improve code readability.

<p>vertical</p> Signup and view all the answers

Avoid extra whitespace ______ parentheses, brackets, or braces.

<p>inside</p> Signup and view all the answers

Place whitespace ______ a comma, semicolon, or colon.

<p>before</p> Signup and view all the answers

Don't put whitespace ______ the open parenthesis of a function call.

<p>before</p> Signup and view all the answers

Avoid whitespace ______ the open bracket of an index or slice.

<p>before</p> Signup and view all the answers

Don't put whitespace ______ a trailing comma and a closing parenthesis.

<p>between</p> Signup and view all the answers

Avoid using whitespace to ______ assignment operators.

<p>align</p> Signup and view all the answers

Instead of comparing boolean values to True or False with the ______ operator, use a direct boolean evaluation in the "if" statement.

<p>equivalence</p> Signup and view all the answers

In "if" statements, utilize the fact that ______ sequences are falsy.

<p>empty</p> Signup and view all the answers

Docstrings should be surrounded by three ______ quotes on either side.

<p>double</p> Signup and view all the answers

The ______ that ends a multiline docstring should be on a line by itself.

<p>quote</p> Signup and view all the answers

Docstrings should be written for all public ______, functions, classes, and methods.

<p>modules</p> Signup and view all the answers

Surround ______ operators with a single space on either side.

<p>binary</p> Signup and view all the answers

When there's more than one operator in a statement, it is better to only add whitespace around the operators with the ______ priority.

<p>lowest</p> Signup and view all the answers

In slices, the ______ acts as a binary operator.

<p>colon</p> Signup and view all the answers

Adding whitespace around the ______ sign when assigning a default value to a function argument is not recommended.

<p>equals</p> Signup and view all the answers

The ______ of Python states that "Sparse is better than dense."

<p>Zen</p> Signup and view all the answers

Flake8 checks code against coding style known as ______.

<p>PEP8</p> Signup and view all the answers

To install Flake8, you can use the command 'python -m pip install ______'.

<p>flake8</p> Signup and view all the answers

The command 'black' is an example of an ______ that formats code to comply with PEP 8.

<p>autoformatter</p> Signup and view all the answers

If Flake8 finds errors in a file, it will list all the ______ in that file.

<p>errors</p> Signup and view all the answers

To run Black on a file, you would use the command 'black ______'.

<p>code.py</p> Signup and view all the answers

Limit the line length of comments and docstrings to ______ characters.

<p>72</p> Signup and view all the answers

Block comments should be indented to the same level as the ______ they describe.

<p>code</p> Signup and view all the answers

Each line of a block comment should start with a # followed by a ______.

<p>single space</p> Signup and view all the answers

Inline comments should be separated by two or more spaces from the ______.

<p>statement</p> Signup and view all the answers

Documentation strings, or ______, are used to explain a specific block of code.

<p>docstrings</p> Signup and view all the answers

Inline comments should be written on the same line as the ______ they refer to.

<p>statement</p> Signup and view all the answers

Docstrings are enclosed in double or single ______ marks.

<p>quotation</p> Signup and view all the answers

PEP 8 provides rules for writing block comments and ______ comments.

<p>inline</p> Signup and view all the answers

The code uses a ______ indent when aligning a block with the opening delimiter.

<p>hanging</p> Signup and view all the answers

In PEP 8, if a line exceeds a certain length, it is recommended to use ______ during line continuation.

<p>indentation</p> Signup and view all the answers

To improve readability, add a ______ after the condition in 'if' statements.

<p>comment</p> Signup and view all the answers

When writing large expressions, it is important to keep them ______.

<p>vertically aligned</p> Signup and view all the answers

PEP 8 suggests two options for the position of the ______ in line continuations.

<p>closing brace</p> Signup and view all the answers

The first option for closing brace placement is to line it up with the first non-______ character of the previous line.

<p>whitespace</p> Signup and view all the answers

Adding ______ indentation can help distinguish line continuations from code blocks.

<p>double</p> Signup and view all the answers

Documenting code is crucial for ______ understanding and collaboration.

<p>programmer</p> Signup and view all the answers

Signup and view all the answers

Flashcards

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 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

Functions and methods should use lowercase words with underscores to separate words. Example: 'find_sum()' or 'validate()'.

Variable Naming

Variables use lowercase letters, words, or combinations with underscores separating words. Example: 'x', 'length', 'student_age'.

Signup and view all the flashcards

Class Naming

Classes use CamelCase, where the first letter of each word is capitalized. Example: 'Student', 'UserAuthentication'.

Signup and view all the flashcards

Constant Naming

Constants use uppercase letters, words, or combinations with underscores separating words. Example: 'PI', 'USER_TYPE', 'THETA'.

Signup and view all the flashcards

Module Naming

Modules use short, lowercase words or combinations separated by underscores. Example: 'module1.py', 'student_module.py', 'module.py'.

Signup and view all the flashcards

Package Naming

Packages use short, lowercase words or combinations without underscores. Example: 'UserUtilities', 'student'.

Signup and view all the flashcards

PEP 8

A style guide that helps programmers ensure consistency and readability in their Python code.

Signup and view all the flashcards

Block Indentation

A method of aligning code where each line in a block is indented to the same level, creating a uniform visual structure.

Signup and view all the flashcards

Alignment with Opening Delimiter

A method of formatting multi-line code where the beginning of the line aligns with the opening delimiter, ensuring consistency.

Signup and view all the flashcards

Hanging Indent

A way of formatting code where lines are indented by four spaces, creating a visual hierarchy for nested structures.

Signup and view all the flashcards

Indentation in If Statements

A type of indent commonly used in multi-line if statements where the condition is written on multiple lines and the code block is indented as necessary.

Signup and view all the flashcards

Comments

Comments that help clarify a code block or explain complex logic within a program. They are often used to make code easier to understand and maintain.

Signup and view all the flashcards

Double Indentation

A method of indenting lines that use two levels of indentation, distinguishing them from regular indentation.

Signup and view all the flashcards

Placement of Closing Brace

Placement of the closing brace in multi-line code blocks, where it can either be aligned with the first character of the line starting the construct or with the first non-whitespace character of the previous line.

Signup and view all the flashcards

Block Comment

A multiline comment that documents a section of code, used to explain several lines of code or a single action.

Signup and view all the flashcards

Inline Comment

A single-line comment used to explain a single statement within a code block.

Signup and view all the flashcards

Comment Sentence Structure

Use complete sentences when writing comments, starting with a capital letter.

Signup and view all the flashcards

Docstring

A multiline string used to document functions, classes, methods, or modules.

Signup and view all the flashcards

Comment Conciseness

Comments should be concise and focused, avoiding unnecessary details or rambling explanations.

Signup and view all the flashcards

Keeping Comments Up-to-Date

Always update comments when you modify the code to ensure they remain accurate and helpful.

Signup and view all the flashcards

Comment Line Length

The maximum length of a comment line is 72 characters.

Signup and view all the flashcards

Block Comment Formatting

Start each line of a block comment with a # followed by a single space.

Signup and view all the flashcards

Flake8

A tool that checks your code for style errors (PEP 8) and potential programming problems like unused imports or undefined variables.

Signup and view all the flashcards

python -m pip install flake8

The command used to install the Flake8 tool. It uses pip, the Python package installer.

Signup and view all the flashcards

flake8 path/to/code/code.py

The command to run Flake8 on a specific file, like 'code.py', to find style and programming errors.

Signup and view all the flashcards

Autoformatter

A tool that automatically reformats your code to follow PEP 8 style guidelines, making your code more consistent and readable.

Signup and view all the flashcards

Black

A popular autoformatter tool that formats Python code to adhere to PEP 8 rules.

Signup and view all the flashcards

Whitespace in Python

Whitespace, like spaces and tabs, is important for readability. It helps to separate operators (like =, +, ==, etc.) and improve code clarity.

Signup and view all the flashcards

Whitespace Around Binary Operators

When using binary operators (like =, +, ==, etc.) in expressions, add a single space on either side.

Signup and view all the flashcards

Whitespace with Multiple Operators

When using multiple operators in a single statement, add whitespace only to operators with the lowest priority (generally those performing addition and subtraction). This maintains readability without creating confusion.

Signup and view all the flashcards

Whitespace in Slices

When working with slices in Python, the colon (:) acts as a binary operator, and should be surrounded by a single space, similar to other binary operators.

Signup and view all the flashcards

Default Value Assignment

Avoid adding extra space around the '=' operator when assigning a default value to a function argument.

Signup and view all the flashcards

When to Avoid Whitespace

While whitespace is important in general, it is best to avoid it in certain situations, as it can actually make the code harder to read. This includes avoiding spaces around the colon in slices, and using spacing consistently for clarity.

Signup and view all the flashcards

Conditional Execution with if Statements

The if statement allows code to execute conditionally based on the true value of a condition. Logical operators can be combined within the condition.

Signup and view all the flashcards

Inappropriate Whitespace: Inside Brackets

Immediately inside parentheses, brackets, or braces. Example: my_list = [1, 2, 3] (recommended), NOT: my_list = [ 1, 2, 3, ]

Signup and view all the flashcards

Inappropriate Whitespace: Before Comma, Semicolon, or Colon

Before a comma, semicolon, or colon. Example: print(x, y) (recommended), NOT: print(x, y)

Signup and view all the flashcards

Inappropriate Whitespace: Function Calls

Before the open parenthesis that starts the argument list of a function call. Example: find_factorial(5) (recommended), NOT: find_factorial (5)

Signup and view all the flashcards

Inappropriate Whitespace: Index or Slice

Before the open bracket that starts an index or slice. Example: my_list[3] (recommended), NOT: my_list [3]

Signup and view all the flashcards

Inappropriate Whitespace: Trailing Comma

Between a trailing comma and a closing parenthesis. Example: tuple = (1, ) (recommended), NOT: tuple = (1, )

Signup and view all the flashcards

Inappropriate Whitespace: Alignment

Used for aligning assignment operators, which is not recommended. Example: var1 = 5 (recommended), NOT: var1 = 5

Signup and view all the flashcards

Boolean Comparison

Don't compare boolean values directly to True or False using the equivalence operator (==). Use the boolean value directly. Example: if my_bool: return '10 is bigger than 5' (recommended), NOT: if my_bool == True: return '10 is bigger than 5'

Signup and view all the flashcards

Empty Sequences

Empty sequences are falsy. So, instead of checking the length of an empty sequence, you can check for the sequence directly. Example: if not my_list: print('List is empty!') (recommended), NOT: if not len(my_list): print('List is empty!')

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.

Quiz Team

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.

More Like This

Use Quizgecko on...
Browser
Browser