

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: What Is Functional Programming?
incomplete
2: Why Python?
incomplete
3: Immutability
incomplete
4: Declarative Programming
incomplete
5: It's Math
incomplete
6: Classes vs. Functions
incomplete
7: Debugging FP
incomplete
8: Functional vs. OOP
incomplete
9: Statements vs. Expressions
incomplete
10: Ternary Expressions
incomplete
11: Functions Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Functional programming tends to be popular among developers with a strong mathematical background. After all, a math equation isn't procedural – it's declarative. Take the following equation:
avg = Σx/N
To put this calculation in plain English:
Σ is just the Greek letter Sigma, and it represents "the sum of a collection."x is the collection of numbers we're averaging.N is the number of elements in the collection.avg is equal to the sum of all the numbers in collection x divided by the number of elements in collection x.So, the equation really just says that avg is the average of all the numbers in collection x. This math equation is a declarative way of writing "calculate the average of a list of numbers." Here's some imperative Python code that does the same thing:
def get_average(nums: list[int]) -> float:
total = 0
for num in nums:
total += num
return total / len(nums)
However, with functional programming, we would write code that's a bit more declarative:
def get_average(nums: list[int]) -> float:
return sum(nums) / len(nums)
Here we're not keeping track of state (the total variable in the first example is "stateful"). We're simply composing functions together to get the result we want.
In the world of document conversion, we sometimes need to handle fonts and font sizes.
Complete the get_median_font_size function. Given a list of numbers representing font sizes, return the median of the list.
For example:
[1, 2, 3] => 2
[10, 8, 7, 5] => 7
None.Here are some helpful docs:
To be a good little functional programmer, your code for this lesson should not: