Python / Core Python Fundamentals Interview Questions
How do you define and call a function in Python?
Functions are defined with the def keyword, a name, parentheses for parameters, a colon, and an indented body. They are first-class objects — you can assign them to variables, pass them as arguments, and return them from other functions.
def greet(name, greeting='Hello'): """Return a personalised greeting string.""" return f'{greeting}, {name}!' print(greet('Alice')) # Hello, Alice! print(greet('Bob', 'Hi')) # Hi, Bob! print(greet(greeting='Hey', name='Carol')) # keyword args
The string literal immediately after the def line is the docstring — accessible via help() or function.__doc__. Always write docstrings for anything that will be reused.
Parameter types to know for interviews:
- Positional: matched left to right.
- Default:
greeting='Hello'— must come after positional args. - *args: captures any number of extra positional arguments as a tuple.
- **kwargs: captures any number of keyword arguments as a dictionary.
- Keyword-only: parameters after a bare
*must be passed by name.
def summary(*args, separator=', '): return separator.join(str(a) for a in args) print(summary(1, 2, 3)) # 1, 2, 3 print(summary(1, 2, 3, separator='-')) # 1-2-3
A function without an explicit return statement returns None. Returning multiple values looks like separate values but Python actually returns a tuple: return x, y is return (x, y).
More Related questions...