🎩 Python Naming Conventions
Prove your DevSecOps expertise! Answer 20 questions to earn your rank.
Green = Correct, Red = Incorrect.
What is the recommended style for variable names in Python?
💡 Explanation
Snake Case (snake_case) is the Python standard.
According to PEP 8, variables and function names should be lowercase with underscores to separate words.
When should you strictly use PascalCase?
💡 Explanation
For Class Names (e.g., BankAccount).
This helps developers instantly recognize that BankAccount is a blueprint (Class), whereas bank_account is an instance.
What is PEP 8?
💡 Explanation
The “Bible” of Python Styling.
PEP 8 (Python Enhancement Proposal 8) defines the standard rules for formatting Python code.
How should you name a constant value like PI?
💡 Explanation
UPPER_CASE (e.g., PI, MAX_RETRIES).
Capitalizing every letter signals: “This value is fixed. Do not change it.”
Which tool automatically checks your code for style violations?
💡 Explanation
A Linter (like pylint or flake8).
In DevSecOps pipelines, linters reject code that doesn’t follow PEP 8 before it reaches production.
Why avoid Hungarian Notation (e.g., strName)?
💡 Explanation
Because types can change.
Since Python is dynamic, strName could technically be assigned a number later, making the name misleading.
Which variable name is preferred?
💡 Explanation
days_since_login
Style is important, but meaning is critical. d is vague. The full name explains exactly what the data represents.
When is it acceptable to use CamelCase (e.g., setUp)?
💡 Explanation
When using legacy libraries.
Libraries like unittest were ported from Java, so they use CamelCase. You must match their style when using them.
Refactor calculate_Salary to Python style:
💡 Explanation
calculate_salary (Snake Case).
Functions should always use lowercase letters with underscores.
When are single-letter vars (i, x) okay?
💡 Explanation
In short loops or math.
Using i for loop counters or x, y for coordinates is standard. For anything else, be descriptive.
What does a single underscore prefix mean (e.g., _data)?
💡 Explanation
“Internal Use Only” (Convention).
Python doesn’t have true private variables. A single underscore is a polite note to other developers: “Please don’t touch this from outside the class.”
What happens with double underscores (e.g., __password)?
💡 Explanation
Name Mangling triggers.
Python internally renames __password to _ClassName__password to prevent subclasses from accidentally overwriting it. It is the closest thing to “Private” in Python.
Why is list = [1, 2, 3] a bad variable name?
💡 Explanation
It shadows a built-in function.
If you name your variable list, you overwrite Python’s actual list() function. Later, if you try to convert a tuple to a list using list((1,2)), your code will crash.
What is the convention for Module names (file names)?
💡 Explanation
Short, all-lowercase names.
Modules should have short, all-lowercase names (e.g., numpy, requests). Underscores can be used if it improves readability, but short is better.
Which single letter variable should be avoided due to font issues?
💡 Explanation
Lowercase L (l), Uppercase O (O).
In many fonts, l looks exactly like the number 1, and O looks like 0. Using them as variable names confuses other developers.
Which assignment follows PEP 8 whitespace rules?
💡 Explanation
x = 1 (One space on each side).
PEP 8 recommends surrounding assignment operators with a single space to let the code “breathe.”
How should you name a custom Exception class?
💡 Explanation
Suffix with ‘Error’ (PascalCase).
Exceptions are Classes, so they use PascalCase. The convention is to end the name with “Error” (e.g., ValueError, KeyError, MyCustomError).
What is the correct style for Function Arguments?
💡 Explanation
Snake Case (my_arg).
Function arguments follow the same rule as variables: lowercase with underscores.
According to strict PEP 8, what is the max line length?
💡 Explanation
79 Characters.
While modern teams often use 100 or 120, the strict PEP 8 standard is 79 characters. This dates back to when terminals were only 80 columns wide.
Which import style is preferred?
💡 Explanation
Explicit Aliases (e.g., np for numpy).
Avoid import * because it pollutes the namespace (variables might clash). Using standard aliases like pd for pandas or np for numpy is best practice.