This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

< Chapter 3 Summary and Problems | Contents | 4.1 If-Else Statements >

Chapter 4. Branching Statements


When writing functions, it is very common to want certain parts of the function body to be executed only under certain conditions. For example, if the input argument is odd, you may want the function to perform one operation on it, and another if the input argument is even. This effect can be achieved in Python using branching statements (i.e., the execution of the function branches under certain conditions), which are the topic of this chapter.

By the end of this chapter, you should be able to program branching statements into your functions and scripts, which should substantially increase the scope of tasks for which you will be able to make functions.