Loading samples...
Loading samples...
py-sample-19
Writing clean, Pythonic code
What is 'Pythonic' code?
Which is preferred: x is None or x == None?
What does 'Explicit is better than implicit' mean?
Which is Pythonic way to check empty list?
What is PEP 8?
Which tool checks code style?
What is EAFP?
Which is preferred for truth check?
What is the naming convention for functions?
Which docstring format is recommended?
Explain the Zen of Python. How to view it?
[3 marks]What is the difference between is and ==? When to use each?
[3 marks]Explain EAFP vs LBYL programming styles.
[3 marks]What are Python naming conventions? Explain all types.
[3 marks]Explain list comprehension vs for loop. Which is more Pythonic?
[3 marks]What are context managers? Explain with and __enter__/__exit__.
[3 marks]Explain the drawbacks of from module import *.
[3 marks]What is type hinting? Why use it despite Python being dynamic?
[3 marks]Explain the use of __all__ in modules.
[3 marks]What tools help with Python code quality? List and explain.
[3 marks]Refactor nested for loops with list comprehensions.
[5 marks]Write a context manager for database transaction using contextlib.
[5 marks]Convert imperative code to functional style using map/filter.
[5 marks]Create a well-documented function following PEP 257 docstring conventions.
[5 marks]Implement a generator-based pipeline for processing large datasets.
[5 marks]Write code demonstrating proper exception handling with specific exceptions.
[5 marks]Find issue: def get_data(): try: with open('file.txt') as f: return f.read() except: pass
[3 marks]Explanation:Should catch specific exceptions. Bare except catches KeyboardInterrupt, SystemExit. Silent failure hides bugs.
Predict output: if []: print('Yes') else: print('No')
[2 marks]Explanation:Empty list is falsy. 'if not lst:' is Pythonic way to check empty.
Find issue: from math import * from numpy import *
[2 marks]Explanation:Both modules have functions with same names. Last import wins. Use explicit imports: import numpy as np.
Predict output: def func(x=[]): x.append(1) return x print(func()) print(func())
[3 marks]Explanation:Mutable default argument persists! Classic gotcha. Use x=None and initialize inside.
Find issue: for i in range(len(items)): do_something(items[i])
[2 marks]Explanation:Direct iteration is cleaner. Only use range(len()) when index needed. Consider enumerate() if need both.
Predict behavior: class MyClass: value = [] def add(self, x): self.value.append(x) a, b = MyClass(), MyClass() a.add(1) print(b.value)
[3 marks]Explanation:Class variable shared by all instances! Both a and b share same list. Use self.value = [] in __init__.
Find issue: if x == None: print('Empty')
[2 marks]Explanation:Identity comparison for None is faster and more correct. None is singleton. Use 'if x is None:'.
Predict output: squares = {x: x**2 for x in range(5)} print(squares.get(3, 0)) print(squares.get(10, 0))
[2 marks]Explanation:Dict comprehension creates {0:0, 1:1, 2:4, 3:9, 4:16}. get(3) returns 9. get(10, 0) returns default 0.