Learning Guides
Functions in Python: Complete Guide with Examples
Quick answer: Learn Python functions including definition, parameters, default arguments, return values, scope, and lambda functions, with practical examples.
What is a Function in Python?
A function is a reusable block of code that performs a specific task. Instead of repeating the same logic, you define it once and call it whenever needed.
def greet(name):
return f"Hello, {name}!"
print(greet("Fireblaze")) # Hello, Fireblaze!
Parameters and Arguments
def add_numbers(a, b):
return a + b
print(add_numbers(5, 3)) # 8
Default arguments
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Priya")) # Hello, Priya!
print(greet("Priya", "Good morning")) # Good morning, Priya!
Keyword arguments
def describe(name, age):
return f"{name} is {age} years old"
print(describe(age=25, name="Aman")) # order does not matter with keywords
Return Values
A function without an explicit return statement returns None. A function can also return multiple values as a tuple.
def min_max(numbers):
return min(numbers), max(numbers)
lowest, highest = min_max([4, 9, 1, 7])
Variable Scope
A variable defined inside a function is local to that function and is not visible outside it.
def set_value():
x = 10 # local variable
return x
set_value()
print(x) # NameError: x is not defined outside the function
*args and **kwargs
Use *args to accept any number of positional arguments and **kwargs to accept any number of keyword arguments.
def total(*numbers):
return sum(numbers)
total(1, 2, 3, 4) # 10
def print_info(**details):
for key, value in details.items():
print(key, value)
print_info(name="Fireblaze", city="Nagpur")
Lambda Functions
A lambda is a small, unnamed function written on a single line, typically used for short operations passed to another function.
square = lambda x: x ** 2
print(square(5)) # 25
numbers = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, numbers)) # [2, 4, 6, 8]
Common Interview Questions
What is the difference between a parameter and an argument?
A parameter is the name listed in the function definition. An argument is the actual value passed in when the function is called.
Can a Python function return multiple values?
Yes, by returning them as a tuple, which can then be unpacked into separate variables by the caller.
When would you use a lambda instead of a regular function?
For short, throwaway operations, especially when passing a simple function to another function such as map, filter or sorted's key argument.
FAQ
Frequently Asked Questions
What is a function in Python?
A function is a reusable, named block of code that performs a specific task, defined once with def and called wherever needed.
What is the difference between a parameter and an argument?
A parameter is the name listed in the function's definition. An argument is the actual value supplied when the function is called.
What does a Python function return if there is no return statement?
It returns None by default.
What is a lambda function used for?
A lambda is a small, unnamed function for short operations, commonly passed directly into functions like map, filter or sorted.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin