Abstract
This chapter introduces and exemplifies two fundamental and extremely useful concepts in Python programming: user-defined functions and branching of program flow (“if” tests).
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Sometimes the word invoke is used as an alternative to call.
- 2.
The global C can technically be accessed as globals()[’C’], but one should avoid working with local and global variables with the same names at the same time!
- 3.
You might think that range(start, stop, inc) makes the makelist function redundant, but range can only generate integers, while makelist can generate real numbers too – and more, see Exercise 3.38.
- 4.
Observe the 1.0 numbers: These avoid integer division (i is int and x may be int).
- 5.
The size of the terms decreases with increasing n, and the first neglected term is then bigger than all the remaining terms, but not necessarily bigger than their sum. The first neglected term is therefore only an indication of the size of the total error we make.
- 6.
One can also apply if n != None, but the is operator is most common (it tests if n and None are identical objects, not just objects with equal contents).
References
H. P. Langtangen. Python Scripting for Computational Science, volume 3 of Texts in Computational Science and Engineering. Springer, Berlin, 3rd edition, 2009.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2012 Springer-Verlag Berlin Heidelberg
About this chapter
Cite this chapter
Langtangen, H.P. (2012). Functions and Branching. In: A Primer on Scientific Programming with Python. Texts in Computational Science and Engineering, vol 6. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-30293-0_3
Download citation
DOI: https://doi.org/10.1007/978-3-642-30293-0_3
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-642-30292-3
Online ISBN: 978-3-642-30293-0
eBook Packages: Mathematics and StatisticsMathematics and Statistics (R0)