Function definitions, argument passing, *args/**kwargs, lambdas, decorators, recursion, and variable scope.
Question 1: What is a function?
Ans
A function is a reusable block of code defined with def that can accept arguments and return a result. Functions reduce duplication and separate responsibilities.
Example
python
def add(a, b):
return a + b
print(add(2, 3))
Important Point
A function without an explicit return statement returns None.
Question 2: What are positional arguments?
Ans
Positional arguments are matched to parameters according to their order in the function call.
Global mutable state can make testing and concurrency harder, so use it deliberately.
Question 14: What is nonlocal?
Ans
nonlocal tells a nested function to assign to a variable in an enclosing function scope rather than creating a new local variable.
Example
python
def counter():
n = 0
def inc():
nonlocal n
n += 1
return n
return inc
c = counter()
print(c(), c())
Important Point
`nonlocal` cannot refer to a module-global variable; it needs an enclosing function scope.
Question 15: What is a docstring?
Ans
A docstring is a string literal used to document a module, class, or function. It can be accessed through __doc__ and tooling can use it for documentation.
Example
python
def add(a, b):
"""Return the sum of two numbers."""
return a + b
print(add.__doc__)
Important Point
A comment is not the same as a docstring; documentation tools commonly inspect docstrings.