

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: Functions As Values
incomplete
2: Anonymous Functions
incomplete
3: First-Class and Higher-Order Functions
incomplete
4: Map
incomplete
5: Filter
incomplete
6: Reduce
incomplete
7: Map, Filter, and Reduce Review
incomplete
8: Zip
incomplete
9: Higher-Order Functions Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
In Python, functions are just values, like strings, integers, or objects. For example, we can assign an existing function to a variable:
from collections.abc import Callable
def add(x: int, y: int) -> int:
return x + y
# assign the function to a new variable
# called `addition`. It behaves the same
# as the original `add` function
addition: Callable[[int, int], int] = add
print(addition(2, 5))
# 7
Callable is the type hint for a function. Callable[[int, int], int] means a function that takes two ints as arguments and returns an int.
With the popularity of generative AI (like ChatGPT), we need to be able to convert files into pure text to be injected into prompts.
Complete the file_to_prompt function. It should take a file dictionary and a to_string function as inputs and return a formatted string. to_string converts a dictionary into a string.
an example string
should become:
```
an example string
```
Including the newlines!
Notice the two newlines in the example above! You don't need a trailing newline, but you do need one after the first set of backticks, and another before the second set of backticks. You can achieve this by using the newline \n escape character. Here's an example:
print("I wish the ring had never come to me.\nI wish none of this had happened.")
becomes:
I wish the ring had never come to me.
I wish none of this had happened.