Loading

Quipoin Menu

Learn • Practice • Grow

python / Functions
interview

Q1. How do you define a function in Python?
Use the def keyword, followed by function name, parameters in parentheses, and a colon. The body is indented. Example:
def greet(name): return f"Hello {name}"
Functions can have default arguments, variable-length arguments (*args, **kwargs).

Q2. What are default arguments?
Default arguments have a default value if not provided by the caller. They are evaluated at function definition time. Example:
def power(base, exp=2): return base ** exp
Then power(3) returns 9, power(3,3) returns 27.

Q3. What are *args and **kwargs?
*args allows passing a variable number of positional arguments, collected as a tuple. **kwargs allows passing a variable number of keyword arguments, collected as a dictionary. Example:
def func(*args, **kwargs): print(args) # tuple print(kwargs) # dict

Q4. What is the difference between return and print in a function?
return sends a value back to the caller and exits the function. print displays output to the console but does not return any value. Functions without return statement return None. Use return when you need to use the result later.

Q5. What are docstrings and how do you use them?
Docstrings are string literals as the first statement in a function, class, or module, used to document its purpose. They are accessible via help() or __doc__. Example:
def add(a, b): """Return the sum of a and b.""" return a + b