Skip to main content

Part of the book series: Texts in Computational Science and Engineering ((TCSE,volume 6))

  • 14k Accesses

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).

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 59.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    Sometimes the word invoke is used as an alternative to call.

  2. 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. 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. 4.

    Observe the 1.0 numbers: These avoid integer division (i is int and x may be int).

  5. 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. 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

  1. H. P. Langtangen. Python Scripting for Computational Science, volume 3 of Texts in Computational Science and Engineering. Springer, Berlin, 3rd edition, 2009.

    Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints 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

Publish with us

Policies and ethics