How do I use non-recursive functions, recursive functions, and lambda expressions to perform mathematical operations such as computing the absolute value of a number and the mean of a sequence of numbers?
To remember and understand some discrete mathematics and Python programming concepts, setting the stage for exploring of discrete structures.
How do I use non-recursive functions, recursive functions, and lambda expressions to perform mathematical operations such as computing the absolute value of a number and the mean of a sequence of numbers?
To remember and understand some discrete mathematics and Python programming concepts, setting the stage for exploring of discrete structures.
Intuitively read the functions to grasp their behavior
Key components of the Python functions
Definition of the function
Parameter(s) that serve as the input
Body that performs a computation
Function return value(s) that produce output
Invocation of the function
Collecting the output of the function
Investigate the ways to define and call Python functions!
def abs(n):
if n >= 0:
return n
else:
return -n
The absolute value of a number is its distance from zero
What is the meaning of the operator >=
?
What is the output of print(str(abs(10)))
?
What is the output of print(str(abs(-10)))
?
Are there other ways to implement this function?
def abs(n):
if n >= 0:
return n
return -n
The absolute value of a number is its distance from zero
Does this function compute the same value?
Which implementation of abs
do you prefer?
Which implementation of abs
does pylint
prefer?
There are different ways to implement the same function!
python-functions/
directory explore-mathematical-functions.ipynb
def sqrt(num: int, tol: float):
guess = 1.0
while abs(num - guess*guess) > tol:
guess = guess -
(guess*guess - num)/(2*guess)
return guess
The sqrt
function calculates the square root of num
What is the meaning of num:int
and tol:float
?
What are the benefits of defining this as a function?
How could we test the sqrt
function using Pytest?
python-functions/
directory explore-mathematical-functions.ipynb
def factorial(number: int):
if number == 1:
return 1
return number * factorial(number - 1)
num = 5
print("The factorial of " + str(num) +
" is " + str(factorial(num)))
The recursive factorial
function calls itself!
How does this function ever stop executing? π€
What are the benefits to using recursive functions?
As an equation: n!=nΓnβ1Γnβ2Γβ¦Γ1
What are the parts of a recursive function in Python?
Defined by cases using conditional logic
A function definition that calls itself
A recursive call that makes progress to a base case
A base case that stops the recursive function calls
Repeatedly perform an operation through function calls
What would happen if you input a negative number?
How could you write this function with iteration?
def factorial(number: int):
if number == 1:
return 1
return number * factorial(number - 1)
num = 5
print("The factorial of " + str(num) +
" is " + str(factorial(num)))
Where is the base case?
Where is the recursive case?
How does this function make progress to the base case?
python-functions/
directory explore-mathematical-functions.ipynb
def square(number: int):
print(f"Called square({number})")
print(f" returning {number*number}")
return number * number
def call_twice(f, number: int):
print(f"Calling twice {f} with number {number}")
return f(f(number))
You can pass a function as an argument to a function!
The behavior of higher-order functions in Python:
Step 1: square
is a function that computes and returns x2
Step 2: call_twice
is a function that calls a function f
twice
Step 3: First, call_twice
calls f
with number
Step 4: Then, call_twice
calls f
with f(number)
Step 5: Finally, call_twice
returns result of f(f(number))
Can you predict the output of the call_twice
function? How would you test the call_twice
function? Can you express it differently?
num = 5
result = call_twice(square, num)
print("Calling the square twice with " +
str(num) + " is " + str(result))
num = 5
result = num ** 4
print("Computation of twice square is "
+ str(num) + " is " + str(result))
python-functions/
directory explore-higher-order-functions.ipynb
square = lambda x: x*x
number = 5
result = call_twice(square, number)
print("Calling square lambda twice " +
"with " +
str(number) +
" is " +
str(result))
Functions are values in the Python programming language
square
is an expression that has a function as its value
You can define a "function" without an explicit signature!
What are benefits of square = lambda x: x*x
?
What are drawbacks of square = lambda x: x*x
?
How do decide between function and lambda expression?
Use case: simple-to-implement mathematical function
How do you test a lambda function in a Python program?
Implement n!=nΓnβ1Γnβ2Γβ¦Γ1 as lambda?
python-functions/
directory explore-lambda-expressions.ipynb
def compute_mean(numbers):
s = sum(numbers)
N = len(numbers)
mean = s / N
return mean
numbers = [5,1,7,99,4]
print(str(compute_mean(numbers)))
How do we compute the mean of a list of numbers?
How do we compute summary statistics of a list of numbers?
What type of function? Recursive? Iterative? Lambda?
from typing import List
def compute_mean(numbers: List) -> float:
s = sum(numbers)
N = len(numbers)
mean = s / N
return mean
How is this function different from the previous one?
What are the benefits of adding type hints to parameters?
What is currently left out of this function signature?
mypy
pylance
checker offer feedback in VS Code How do you pick between the different types of functions?
Python functions to perform statistical analysis of data:
Q1: How do you compute the median of a list of numbers?
Q2: How do you compute the mode of a list of numbers?
Q3: How do you compute a frequency table of a list of numbers?
Q4: How do you compute the range of a list of numbers?
Q5: How do you compute the variance and standard deviation?
Can you translate the mathematical descriptions of these summary statistics to Python programs? Can you ensure their correctness with testing?