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 argsThe 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-3A 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).
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
