Python Coding Standards
Python Coding Standards Interview with follow-up questions
1. What is PEP 8 in Python?
PEP 8 is the official style guide for Python code — "PEP" stands for Python Enhancement Proposal, and PEP 8 lays out conventions for layout and naming so that code from different authors reads consistently. Highlights interviewers expect you to name:
- 4-space indentation, no tabs; a recommended line length (the classic 79, though most teams set 88–100 in their formatter).
- Naming:
snake_casefor functions and variables,PascalCasefor classes,UPPER_CASEfor constants. - Two blank lines between top-level definitions, imports grouped (stdlib / third-party / local) and one per line.
- Whitespace rules around operators and after commas.
The key 2026 point: nobody applies PEP 8 by hand. It's enforced automatically by Ruff (linter + formatter, which replaced black/flake8/isort) configured in pyproject.toml and run in pre-commit and CI. So in practice "follow PEP 8" means "let the formatter own it."
Follow-up — "is PEP 8 absolute?" No. PEP 8 itself says consistency within a project wins, and "a foolish consistency is the hobgoblin of little minds" — don't break a working module's local style just to satisfy a rule.
Follow-up 1
Why is PEP 8 important in Python programming?
PEP 8 is important in Python programming because it promotes code consistency and readability. By following the guidelines, developers can write code that is easier to understand, maintain, and collaborate on. It also helps in reducing the chances of introducing bugs and makes the code more professional and standardized.
Follow-up 2
Can you give an example of a PEP 8 guideline?
Sure! One example of a PEP 8 guideline is the use of spaces around operators and after commas. For example:
# Good
x = 5 + 2
y = [1, 2, 3]
# Bad
x=5+2
y=[1,2,3]
Follow-up 3
How does following PEP 8 improve code readability?
Following PEP 8 improves code readability by enforcing consistent and clear coding style. It defines guidelines for naming conventions, indentation, line length, comments, and more. By adhering to these guidelines, code becomes more readable and easier to understand for both the original author and other developers who may work on the code in the future.
Follow-up 4
What tools can be used to check if your code is PEP 8 compliant?
There are several tools available to check if your code is PEP 8 compliant. Some popular ones include:
- Pylint: A static code analysis tool that can check for PEP 8 violations.
- Flake8: A command-line tool that combines the functionality of Pylint, PyFlakes, and McCabe to check for PEP 8 compliance.
- PyCharm: An integrated development environment (IDE) that provides built-in PEP 8 checks and suggestions.
These tools can be integrated into your development workflow to automatically check your code for PEP 8 compliance.
2. What are some common Python coding standards?
The standards usually anchored to specific PEPs:
PEP 8 — the style guide: indentation, naming (
snake_case,PascalCase,UPPER_CASE), imports, line length, whitespace.PEP 257 — docstring conventions: how to write
"""..."""summaries and multi-line docstrings for modules, functions, and classes.PEP 484 / PEP 604 — type hints. Modern style uses built-in generics (
list[int],dict[str, int]) andX | Yunions instead oftyping.ListorOptional, checked by mypy or pyright.PEP 20 — the Zen of Python (
import this): guiding principles like "Readability counts" and "There should be one — and preferably only one — obvious way to do it."
In 2026 these are operationalized through tooling rather than memorized: Ruff enforces PEP 8 and import ordering and auto-fixes most violations, mypy/pyright enforce the type hints, uv manages environments, and it all lives in a single pyproject.toml, wired into pre-commit and CI. The expectation in professional code is annotated signatures, docstrings on public APIs, and a formatter that settles style automatically.
Follow-up 1
Why are coding standards important?
Coding standards are important for several reasons:
Readability: Consistent coding standards make code easier to read and understand, especially when working in a team.
Maintainability: Following coding standards makes code more maintainable, as it becomes easier to make changes and debug issues.
Collaboration: Coding standards facilitate collaboration among developers by providing a common set of guidelines to follow.
Code quality: Adhering to coding standards helps improve code quality by promoting best practices and reducing the likelihood of errors and bugs.
Follow-up 2
How do coding standards improve code quality?
Coding standards improve code quality in the following ways:
Consistency: By enforcing consistent coding styles and conventions, coding standards make code easier to read and understand, reducing the likelihood of errors.
Readability: Coding standards prioritize code readability, making it easier for developers to understand and maintain the code.
Maintainability: Following coding standards makes code more maintainable, as it becomes easier to make changes and debug issues.
Bug prevention: Coding standards often include best practices that help prevent common coding mistakes and reduce the likelihood of bugs.
Code reviews: Coding standards provide a common set of guidelines for code reviews, ensuring that code is reviewed consistently and thoroughly.
Follow-up 3
Can you give an example of a Python coding standard?
One example of a Python coding standard is PEP 8, the official style guide for Python code. PEP 8 covers topics such as indentation, naming conventions, and code layout. Here are a few examples of PEP 8 guidelines:
Indentation: Use 4 spaces per indentation level.
Naming conventions: Use lowercase letters and underscores for variable and function names.
Line length: Limit lines to a maximum of 79 characters.
Imports: Import modules on separate lines and avoid using wildcard imports.
Following these guidelines helps ensure consistent and readable Python code.
3. How do you ensure your Python code adheres to coding standards?
In 2026 the honest answer is "I let automated tooling enforce it so it isn't a matter of discipline." Concretely:
Ruff for linting and formatting. A single Rust-based tool that replaced flake8 + black + isort + pyupgrade:
ruff check --fixcatches and fixes lint issues and import order,ruff formatapplies PEP 8 layout. Config lives inpyproject.toml.Static type checking with mypy or pyright/ty, so annotated signatures are actually verified rather than decorative.
pre-commit hooks that run Ruff and the type checker on every commit, so nothing unformatted or untyped ever leaves a developer's machine.
CI gates (GitHub Actions, GitLab CI, etc.) that re-run the same checks plus the test suite on every pull request and block merges on failure — the same commands locally and in CI, no drift.
Code review for the things tools can't judge: naming, design, and whether the change is correct and readable.
The principle: style and typing are mechanically enforced and consistent across the team, freeing reviews to focus on logic and design rather than formatting nits.
Follow-up 1
What tools or practices do you use to maintain coding standards?
To maintain coding standards in Python, I use the following tools and practices:
Editor/IDE Plugins: I use editor or IDE plugins like Pylance, PyCharm, or Visual Studio Code with Python extensions. These plugins provide real-time feedback and suggestions based on coding standards while writing code.
Pre-commit Hooks: I set up pre-commit hooks in my Git repository. These hooks automatically run linting and formatting checks on the code before each commit, ensuring that it adheres to coding standards.
Style Guides: I refer to style guides like Google Python Style Guide or Airbnb Python Style Guide to ensure consistency in coding standards across the team.
Training and Documentation: I actively participate in training sessions and workshops on coding standards. I also contribute to maintaining internal documentation that outlines the coding standards and best practices.
By using these tools and practices, I can consistently maintain coding standards in my Python code.
Follow-up 2
How do you handle a situation where a team member is not following coding standards?
If I encounter a situation where a team member is not following coding standards, I would take the following steps:
Communication: I would have a conversation with the team member to understand their perspective and reasons for not following the coding standards. It could be due to lack of awareness, misunderstanding, or other factors.
Provide Feedback: I would provide constructive feedback to the team member, explaining the importance of coding standards and how it impacts the overall code quality, readability, and maintainability.
Offer Assistance: If the team member is facing challenges in adhering to coding standards, I would offer assistance by providing resources, sharing knowledge, or pairing up for code reviews.
Team Discussion: If the issue persists, I would raise the concern in a team discussion or during a retrospective meeting. This would allow the team to collectively address the issue and find a solution that works for everyone.
Escalation: If the team member continues to disregard coding standards despite the above steps, I would escalate the issue to the team lead or project manager for further action.
By following these steps, I aim to promote a culture of adherence to coding standards within the team and ensure the overall code quality is maintained.
4. What is the significance of indentation in Python?
In Python, indentation is syntax, not decoration. Where C-style languages use braces {} to delimit blocks, Python uses the indentation level itself to define which statements belong to a loop, function, conditional, or class. Change the indentation and you change the program's meaning; get it wrong and you get an IndentationError or TabError before the code even runs.
def classify(n: int) -> str:
if n > 0:
return "positive" # inside the if-block
return "non-positive" # back at function level
Things interviewers like to surface:
- PEP 8 says 4 spaces per level, and you must not mix tabs and spaces — Python 3 rejects inconsistent mixing outright. Editors and Ruff's formatter normalize this for you.
- The colon
:opens a block; the following line(s) must be indented further. - This is why Python has no need for
end/braces and why "the layout is the structure" — readability is enforced by the grammar rather than left to convention.
So indentation simultaneously controls scope/flow of execution and guarantees the visual structure matches the logical structure.
Follow-up 1
How does incorrect indentation affect Python code?
Incorrect indentation in Python code can lead to syntax errors and affect the logic and functionality of the code. Python relies on consistent and correct indentation to determine the structure of the code. If the indentation is incorrect, Python will raise an 'IndentationError' and the code will not run. Additionally, incorrect indentation can change the meaning of the code and cause unexpected behavior. It is important to ensure that the indentation is consistent and follows the recommended style guidelines.
Follow-up 2
What is the standard number of spaces for indentation in Python according to PEP 8?
According to PEP 8, the official style guide for Python code, the standard number of spaces for indentation is 4. It is recommended to use spaces for indentation instead of tabs. This helps to ensure consistent and readable code across different platforms and editors. However, the most important thing is to be consistent with the chosen indentation style throughout the codebase.
5. What are the naming conventions in Python as per PEP 8?
PEP 8's naming conventions:
- Modules / packages: short, all-lowercase, underscores only if they improve readability (
my_module); packages preferably no underscores. - Classes:
PascalCase(a.k.a. CapWords), e.g.HttpClient. - Functions, methods, variables:
snake_case, e.g.parse_config. - Constants:
UPPER_CASE_WITH_UNDERSCORES, defined at module level. - A single trailing underscore avoids clashing with a keyword (
class_,type_). - A single leading underscore (
_internal) signals "non-public / internal" by convention — it's a hint to humans, not enforced. - A double leading underscore (
__name) inside a class triggers name mangling (_ClassName__name) to avoid accidental override in subclasses — this is the real gotcha, and it is not "private" in the C++/Java sense. - Dunder names (
__init__,__name__) are reserved for the language; don't invent your own. - Avoid single-character names except short-lived counters/iterators, and never use
l,O, orIalone (they look like1/0).
Correction to a common myth: Python has no truly "protected" or "private" access modifier. The leading-underscore conventions are signals; only double-underscore name mangling actually changes the attribute name. Ruff (via pep8-naming rules) can enforce these conventions automatically.
Follow-up 1
Why are naming conventions important?
Naming conventions are important in Python (and in programming in general) because they make code more readable and maintainable. By following consistent naming conventions, it becomes easier for developers to understand the purpose and functionality of different elements in the code. It also helps in avoiding naming conflicts and improves code collaboration among team members. Additionally, adhering to naming conventions like PEP 8 ensures that your code looks professional and follows industry best practices.
Follow-up 2
Can you give examples of good and bad variable names according to PEP 8?
Sure! Here are some examples of good and bad variable names according to PEP 8:
Good variable names:
total_countuser_nameis_valid
Bad variable names:
tCunvalid
In the good examples, the variable names are descriptive, using lowercase letters with underscores as separators. They provide clear information about the purpose or content of the variable. On the other hand, the bad examples use abbreviations, single letters, or vague names that do not convey the meaning of the variable. It is always recommended to use meaningful and descriptive names to improve code readability.
Live mock interview
Mock interview: Python Coding Standards
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.