Module 2: Lesson 3


Python Functions

This lesson introduces the concept of a function. Much like the equivalent in mathematics, a function takes input and generates output. By encapsulating algorithmic details in a function, you promote code reuse, increase efficiency by leveraging previous work, and reduce the likelihood for errors to occur.

Objectives

By the end of this lesson, you will be be able to

  • create a function,
  • process input inside a function, and
  • return a new value from a function.

You will also learn how to create unnamed functions, known as lambda functions, and how to provide default values for functions.

Time Estimate

Approximately 2 hours.

Activities

Video: Watch the Python functions video, which will demonstrate how to write and use Python functions.

Notebook: Read and complete the practice exercises in the Python functions notebook.


© 2017: Robert J. Brunner at the University of Illinois.

This notebook is released under the Creative Commons license CC BY-NC-SA 4.0. Any reproduction, adaptation, distribution, dissemination or making available of this notebook for commercial use is not allowed unless authorized in writing by the copyright holder.