Skip to main content

Loops and Lists

  • Chapter
  • 14k Accesses

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

Abstract

This chapter shows how repetitive tasks in a program can be automated by loops. We also introduce list objects for storing and processing collections of data with a specific order. Loops and lists, together with functions and if-tests from Chapter 3, lay the fundamental programming foundation for the rest of the book.

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

Buying options

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

Learn about institutional subscriptions

Notes

  1. 1.

    For this table we also add (of teaching purposes) a line above and below the table.

  2. 2.

    If you did not get the last point here, just relax and continue reading.

  3. 3.

    In Python, cond1 and cond2 or cond1 or cond2 returns one of the operands and not just True or False values as in most other computer languages. The operands cond1 or cond2 can be expressions or objects. In case of expressions, these are first evaluated to an object before the compound boolean expression is evaluated. For example, (5+1) or -1 evaluates to 6 (the second operand is not evaluated when the first one is True), and (5+1) and -1 evaluates to -1.

  4. 4.

    All objects in Python can in fact be evaluated in a boolean context, and all are True except False, zero numbers, and empty strings, lists, and dictionaries. See Exercise 6.24 for more details.

  5. 5.

    Every object in Python and everything you can do with them is defined by programs made by humans. With the techniques of Chapter 7 you can create your own objects and define (if desired) what it means to add such objects. All this gives enormous power in the hands of programmers. As one example, you can easily define your own list objects if you are not satisfied with Python’s own lists.

  6. 6.

    Any value in [41,45] can be used as second argument (stop value) to range and will ensure that 40 is included in the range of generate numbers.

  7. 7.

    Actually, the data are taken from a web page as explained in Chapter 6.4.3 and easily written out in the list format shown here.

  8. 8.

    Any command you can run in the terminal window can also be run inside IPython if you start the command with an exclamation mark.

  9. 9.

    This nonzero eps value is called machine epsilon or machine zero and is an important parameter to know, especially when certain mathematical techniques are applied to control round-off errors.

References

  1. D. Beazley. Python Essential Reference. SAMS, Indianapolis, 2nd edition, 2001.

    Google Scholar 

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

    Google Scholar 

  3. M. Lutz and D. Ascher. Learning Python. O’Reilly, Sebastopol, 1999.

    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). Loops and Lists. 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_2

Download citation

Publish with us

Policies and ethics