Functions, Modules and Errors
A function is a small machine: you put something in, and a result comes out. In AI work, you write functions to build prompts, count tokens and clean up model answers. In this lesson you will write your own functions and borrow ready-made ones. You will also learn to read an error message without fear.
New words
- function
- A named recipe of code that you can use again and again.
- parameter
- A name in the function definition that waits for a value.
- argument
- The real value you give to a function when you call it.
- return value
- The answer that a function hands back to the code that called it.
- module
- A file of ready-made Python code that you borrow with import.
- exception
- The Python word for an error that you can catch and handle.
- traceback
- The report Python prints when an error stops your program.
What you will learn
- Why do you need functions?
- Write a function: def, parameters and return
- Default and keyword arguments
- Docstrings and scope
- Modules: borrow ready-made code
- Errors, tracebacks and try/except
- Raise your own errors
- Try it: the function machine
- Common mistakes
- Explain it to a friend
- Key takeaways
10 quiz questions at the end.
The full lesson, with interactive demos and the quiz, opens after you sign in with Google. It is free.