Functions

Learn how to define functions in Python and how to use them.

Introduction to functions

We spent a lot of time earlier working with mathematical functions. We thought of these as machines that can take input, do work, and produce the result. These functions can be used again and again.

Many computer languages, Python included, make it easy to create reusable computer instructions. Like mathematical functions, these reusable snippets of code stand on their own if we define them sufficiently. Plus, they allow us to write shorter, more elegant code. Why do we want shorter code? Because invoking a function by its name many times is better than writing out the entire function code many times.

Get hands-on with 1200+ tech skills courses.