Python #3 Recursive, Lambda Functions
Unit 3: Functions
Write reusable programs by hiding redundancy and complexity, passing arguments, returning results, and exploring recursion and lambda expressions.
Choose a topic ๐
Read the notes, trace the example, and try each mini task before moving on.
Hiding Redundancy
Reuse a function instead of repeating the same code.
Read notes ↗Hiding Complexity
Give a meaningful name to a multi-step operation.
Read notes ↗Parameters
Parameters are names in a function definition.
Read notes ↗Arguments
Arguments are the values passed to a function call.
Read notes ↗Return Values
Use return to send a result back to the caller.
Read notes ↗Formal vs Actual Arguments
Distinguish parameter names from supplied values.
Read notes ↗Named Arguments
Pass arguments using parameter names.
Read notes ↗Recursive Functions
A recursive function calls itself.
Read notes ↗Lambda Functions
Create a small anonymous function with one expression.
Read notes ↗Hiding Redundancy
Reuse a function instead of repeating the same code.
๐ Understand the idea
A function groups statements under a name. When the same task appears in several places, define it once and call it whenever needed. This reduces duplication and makes updates easier.
Remember
Keep one source of truth: if the logic changes, update the function in one place.
Worked example
def show_welcome():
print("Welcome to Python!")
show_welcome()
show_welcome()๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
Why use a function when the same statements are needed several times?
Hiding Complexity
Give a meaningful name to a multi-step operation.
๐ Understand the idea
A function can hide implementation details behind a simple name. A caller needs to know what the function does and what inputs it expects, not every internal step.
Remember
Choose descriptive names and keep each function focused on one responsibility.
Worked example
def rectangle_area(length, width):
return length * width
print(rectangle_area(5, 3))๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
How does a function help hide complexity?
Parameters
Parameters are names in a function definition.
๐ Understand the idea
Parameters act as local names that receive values when a function is called. They make a function reusable with different inputs.
Remember
Parameters appear in the function definition, inside the parentheses.
Worked example
def greet(name):
print("Hello", name)
greet("Asha")๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
In def greet(name), what is name?
Arguments
Arguments are the values passed to a function call.
๐ Understand the idea
When calling a function, supply arguments for its parameters. The values can be literals, variables, or expressions.
Remember
Remember: parameter = definition; argument = call.
Worked example
def square(number):
return number * number
result = square(6)
print(result)๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
In square(6), what is 6?
Return Values
Use return to send a result back to the caller.
๐ Understand the idea
A return statement ends the current function call and provides a result. The caller can store that result or use it in an expression. A function without an explicit return returns None.
Remember
print() displays a value; return gives a value back to the caller.
Worked example
def add(a, b):
return a + b
total = add(8, 4)
print(total)๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
What does return do?
Formal vs Actual Arguments
Distinguish parameter names from supplied values.
๐ Understand the idea
Formal parameters are the names written in a function definition. Actual arguments are the values or expressions supplied in a function call.
Remember
Use the exact terminology requested by your course: formal parameter in definition; actual argument in call.
Worked example
def multiply(x, y): # x and y are formal parameters
return x * y
print(multiply(3, 7)) # 3 and 7 are actual arguments๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
Which are the formal parameters in multiply(x, y)?
Named Arguments
Pass arguments using parameter names.
๐ Understand the idea
Keyword (named) arguments identify which parameter receives each value, making calls easier to read. In Python, positional arguments must come before keyword arguments in a call.
Remember
Example: describe(item="book", quantity=2). Names must match the function parameters.
Worked example
def describe(item, quantity):
print(quantity, item)
describe(item="notebook", quantity=2)๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
Why can named arguments improve readability?
Recursive Functions
A recursive function calls itself.
๐ Understand the idea
Recursion can express problems that break naturally into smaller versions of the same problem. Every correct recursive solution needs a base case to stop further calls.
Remember
Without a reachable base case, recursion may continue until Python raises RecursionError.
Worked example
def factorial(n):
if n <= 1: # base case
return 1
return n * factorial(n - 1)
print(factorial(5))๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
What is the purpose of a base case?
Lambda Functions
Create a small anonymous function with one expression.
๐ Understand the idea
A lambda expression creates a function object, often for a short operation used briefly as an argument. A lambda body is a single expression, not a block of statements.
Remember
Use def for named, multi-step functions; use lambda for concise expressions.
Worked example
double = lambda n: n * 2
print(double(9))๐ง Exam quick notes
- Define a function using
def, followed by a name and parentheses. - The function body must be consistently indented.
- Call the function by writing its name followed by parentheses.
- Test typical inputs and boundary cases.
✍️ Mini task
How many expressions can a lambda body contain?
Practical Python Lab ๐งช
Minimum three practicals are required; four runnable exercises are provided. Edit the code and run it directly in your browser.
Practical 1 · Calculator Functions
Define and call separate functions for addition, subtraction, multiplication, and safe division.
Click “Run Python” to execute this example.
The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.
Practical 2 · Student Marks and Grade
Use parameters, a return value, and if/elif logic to classify a mark.
Click “Run Python” to execute this example.
The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.
Practical 3 · Recursive Factorial
Calculate factorial using a base case and a recursive call.
Click “Run Python” to execute this example.
The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.
Practical 4 · Lambda and Sorting
Use a lambda expression as a key to sort records by score.
Click “Run Python” to execute this example.
The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.
Unit 3 quiz ๐ง
Answer all 12 questions, then check your score and read the explanations.
Celebrate each step ๐
Tick each topic after reading the notes and attempting its mini task.
๐ฏ Learning outcomes
By the end of Unit III, you should be able to create reusable functions, distinguish parameters from arguments, return values, use named arguments, explain recursion, and write simple lambda expressions.
References & learning resources ๐
- Python Tutorial — Defining Functions (official Python docs) ↗
- Python language reference — Function definitions ↗
- Python expressions — Lambda expressions ↗
Comments
Post a Comment