Python Programmer Do\'s and Dont\'s — Questions & Answers

Test your knowledge of best practices and common mistakes in Python programming with this beginner-friendly quiz. Improve your Python coding skills by learning the do's and don'ts every programmer should know.

This quiz contains 10 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.

  1. Question 1: Using Mutable Default Arguments

    When defining a Python function that takes a list as an optional argument, which method should you avoid to prevent unexpected behavior?

    • Not providing a default value at all
    • Using a mutable list like [] as the default value
    • Setting the default value to None and creating a new list inside the function
    • Using an immutable tuple as the default value
    Show correct answer

    Correct answer: Using a mutable list like [] as the default value

    Explanation: Using a mutable list like [] as the default value leads to shared state across function calls, causing subtle bugs. Setting the default to None and creating a new list each time avoids this problem. Not providing a default value means the argument is required, which is acceptable. Using an immutable tuple is also safe because it cannot be altered.

  2. Question 2: String Concatenation Efficiency

    When repeatedly appending strings inside a loop, what is the recommended approach for efficiency and clarity?

    • Appending strings to a list and joining after the loop
    • Directly concatenating strings with + in each loop iteration
    • Building a long string with repeated multiply operations
    • Using string interpolation inside the loop every time
    Show correct answer

    Correct answer: Appending strings to a list and joining after the loop

    Explanation: Appending strings to a list and joining them after the loop is efficient because it avoids creating many intermediate strings. Direct concatenation with + is slow for large numbers of strings. String interpolation inside the loop is also inefficient. Multiplying strings is only useful for repeating, not for concatenation.

  3. Question 3: Variable Naming Conventions

    Which variable name best follows Python's recommended naming conventions for function names and variables?

    • calculate_sum
    • calculate-sum
    • CalculateSum
    • calculateSum
    Show correct answer

    Correct answer: calculate_sum

    Explanation: Python recommends using lowercase letters with underscores (snake_case) for variable and function names, so 'calculate_sum' is correct. 'CalculateSum' is a naming style typically used for classes. 'calculateSum' uses camelCase, which is not standard in Python. 'calculate-sum' is invalid due to the hyphen, which is not allowed in identifiers.

  4. Question 4: Importing Modules Correctly

    What is the preferred way to import the math module for general use throughout your Python script?

    • import math
    • import Math
    • from math import *
    • from math import math
    Show correct answer

    Correct answer: import math

    Explanation: Using 'import math' is preferred because it keeps the namespace clear and avoids potential conflicts. 'from math import *' can overwrite other names and is discouraged. 'import Math' is incorrect because Python is case-sensitive. 'from math import math' does not work, as there is no submodule named 'math' in the math module.

  5. Question 5: Comparing Values Properly

    Which is the correct and safe way to compare two variables in Python for equality?

    • Using the => operator
    • Using the = operator
    • Using the === operator
    • Using the == operator
    Show correct answer

    Correct answer: Using the == operator

    Explanation: The '==' operator correctly compares values for equality in Python. The '=' operator is for assignment, not comparison, so it's incorrect. '===' is not valid syntax in Python, despite similarities in other languages. '=>' is also not a Python comparison operator, making only '==' correct for value equality.

  6. Question 6: Avoiding Shadowing Built-Ins

    Why should you avoid naming your own variables 'list', 'dict', or 'str' in Python code?

    • Because these names are built-in types and can cause confusion or bugs
    • Because Python does not allow these variable names
    • Because they are reserved strictly for system use only
    • Because these names make the code run slower
    Show correct answer

    Correct answer: Because these names are built-in types and can cause confusion or bugs

    Explanation: Using names like 'list', 'dict', or 'str' for variables replaces the built-in types in the current scope, leading to errors or unexpected behavior. Python technically allows these as variable names, but it's considered bad practice. There is no performance penalty for using these names, but clarity and bug prevention are the main concerns. These names are not system-reserved, but they have important built-in meanings.

  7. Question 7: Using Exception Handling Wisely

    When should you use a try-except block while writing Python code?

    • To hide bugs that you do not want to debug
    • Only when working with files
    • When handling code that might raise an exception you can handle meaningfully
    • For all parts of the code, even if no exceptions are expected
    Show correct answer

    Correct answer: When handling code that might raise an exception you can handle meaningfully

    Explanation: Use try-except blocks when you anticipate specific errors and have a way to handle them calmly, such as catching file not found errors. Wrapping all code in try-except hides bugs and makes troubleshooting difficult. While file operations often use exceptions, try-except is not limited to files. Using it to mask bugs is discouraged because it can lead to silent, hard-to-find errors.

  8. Question 8: Indentation Importance

    Why must you use consistent indentation in Python for blocks of code such as loops and functions?

    • Because otherwise comments will not work
    • To make code execution faster
    • Because it only helps with readability and has no effect on code execution
    • Because Python uses indentation to define code blocks, not braces
    Show correct answer

    Correct answer: Because Python uses indentation to define code blocks, not braces

    Explanation: Python uses indentation to indicate blocks of code, unlike some languages that use braces. Inconsistent indentation leads to syntax errors. Although indentation also improves readability, in Python, it directly affects how the program runs. Indentation does not affect comments or code execution speed; it is required for syntax reasons.

  9. Question 9: Comparing with None

    What is the recommended way to check if a variable x is set to None in Python?

    • Using x == None
    • Using x is None
    • Using x = None
    • Using x === None
    Show correct answer

    Correct answer: Using x is None

    Explanation: The 'is' operator is recommended for checking against None because it checks for identity, not just value. 'x == None' may work but is less precise, especially if the __eq__ method is overridden. 'x === None' is not valid syntax in Python. 'x = None' assigns None rather than checking, so it's not correct for comparison.

  10. Question 10: Avoiding Unused Imports

    Why should you remove unused module imports from your Python scripts?

    • Because Python throws a syntax error if an import is unused
    • Because using any import statement significantly slows down execution
    • Because unused imports clutter the code and can reduce readability
    • Because it disables other imports in the file
    Show correct answer

    Correct answer: Because unused imports clutter the code and can reduce readability

    Explanation: Unused imports do not cause syntax errors, but they make the code harder to read and maintain. Having many unnecessary imports can be confusing for others reading your code. Python does not slow down significantly due to unused imports except in very large projects. Unused imports do not affect or disable other import statements.