Python / Core Python Fundamentals Interview Questions
What is PEP 8 and which conventions does it define for Python code?
PEP 8 is Python's official style guide, written by Guido van Rossum, Barry Warsaw, and Nick Coghlan. It defines conventions for formatting Python code so that all Python code looks consistent and is easier to read and review.
The most frequently tested conventions:
Indentation: 4 spaces per level. Never tabs. (The standard library itself mandates 4 spaces; mixing tabs and spaces causes TabError in Python 3.)
Line length: Maximum 79 characters for code, 72 for docstrings and comments. Most teams now accept 88 or 99 characters when using the Black formatter.
Naming conventions:
snake_case # variables, functions, module names SCREAMING_SNAKE # module-level constants PascalCase # class names _single_leading # private by convention (not enforced) __double_leading # name-mangled in classes (avoid unless needed) __dunder__ # reserved for Python internals â don't invent new ones
Whitespace rules: one space around binary operators (x = y + z), no space before a colon in a slice (data[1:3]), two blank lines between top-level definitions, one blank line between methods inside a class.
Imports: standard library first, then third-party, then local — each group separated by a blank line. Absolute imports preferred over relative. One import per line.
Tools that enforce PEP 8 automatically: pycodestyle (checks), autopep8 (fixes), flake8 (checks + extra rules), black (opinionated auto-formatter). In interviews, knowing that you use a linter or formatter signals professional habits.
More Related questions...