We've covered a lot about functions - creating them, using parameters, returning values, defaults, and scope. Now let's put it all together by building useful functions from scratch!
Engagement Message
Ready to become a function-building expert?
Let's design a function step-by-step. We want to calculate the final grade for a student. What information do we need? A list of scores and maybe different weights for assignments.
Engagement Message
What would you name this function?
Here's our plan: take scores and weights, calculate weighted average, then return a letter grade. Notice how we're breaking this complex task into smaller, manageable steps within one function.
Engagement Message
Can you see the logical flow?
Before we implement our function, let's learn about a helpful Python tool. The sum()
function is built into Python (just like print()
) and adds up all numbers in a list:
Engagement Message
You see how pre-defined functions like sum()
make our code much cleaner?
Another useful tool is , which pairs up items from two lists. It's named after a zipper. This tool is perfect when you need to work with corresponding elements:
