def calculate_discount(price, discount_percent):
"""
Calculate the final price after applying discount.
Args:
price: Original price
discount_percent: Discount percentage
Returns:
Final price after discount
"""
discount_amount = price * (discount_percent / 100)
final_price = price - discount_amount
return final_price
result = calculate_discount(1000, 20)
print(result)Predict the behavior: What will Hari's code output, and is it following best practices for readability?
800, and it follows best practices
200, and it violates PEP 8 conventions
1000, and the calculation is incorrect
The function will raise a TypeError
Show answer & explanationAnswer
Correct answer
800, and it follows best practices
Explanation
The function calculates: 1000 * (20/100) = 200 discount, so 1000 - 200 = 800. The code follows best practices with proper docstring, spacing, and naming conventions.
Written by ExamHoot EditorialPublished · Updated
All 40 Best Practices and Code Style questions
- 1.What is the primary purpose of following consistent naming conventions in Python code?
- 2.What does PEP 8 recommend regarding the maximum line length in Python code?
- 3.What is the correct way to name a constant in Python according to best practices?
- 4.Why should you avoid using single-letter variable names in Python code?
- 5.What is the recommended approach for organizing imports in Python according to PEP 8?
- 6.What does the DRY principle stand for in software development?
- 7.What is the purpose of using docstrings in Python functions?
- 8.What is considered a best practice when writing conditional statements in Python?
- 9.How should you handle exceptions in Python according to best practices?
- 10.What is the recommended maximum number of arguments a function should have?
- 11.What is the purpose of type hints in Python code?
- 12.What does the KISS principle recommend in software development?
- 13.What is the recommended practice for handling mutable default arguments in Python functions?
- 14.What is the purpose of code reviews in software development?
- 15.What is considered a best practice when naming boolean variables in Python?
- 16.What is the primary purpose of following PEP 8 style guidelines in Python development?
- 17.Predict the output:
- 18.Which naming convention should be used for function names according to Python best practices?
- 19.Identify the bug:
- 20.What is the recommended maximum line length in Python code according to PEP 8 standards?
- 21.Predict the behavior:
- 22.Which of the following best demonstrates proper code organization and readability in Python best practices?
- 23.Find the error:
- 24.What is the purpose of using docstrings in Python functions according to code style best practices?
- 25.Predict the output:
- 26.What is the recommended approach for handling imports in a Python file following PEP 8 best practices?
- 27.Identify the style violation:
- 28.Predict the output:
- 29.What is the correct way to write a docstring for a function according to Python best practices?
- 30.Find the bug:
- 31.Predict the behavior:
- 32.Which of the following code snippets best demonstrates proper error handling and code style in Python?
- 33.Identify the violation:
- 34.Predict the output:
- 35.What are the key principles for writing maintainable and readable Python code according to best practices?
- 36.What is the primary purpose of following PEP 8 style guidelines in Python development?
- 37.Predict the output and identify the style issue in the following code snippet written by Hari
- 38.What should Prasath do to improve code readability when dealing with long function parameter lists?
- 39.Predict what issue Deepak will encounter when reviewing this code for style compliance
- 40.What is the correct approach for organizing imports and what does Ravi need to fix in this code structure?
