To save content items to your account,
please confirm that you agree to abide by our usage policies.
If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account.
Find out more about saving content to .
To save content items to your Kindle, first ensure no-reply@cambridge.org
is added to your Approved Personal Document E-mail List under your Personal Document Settings
on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part
of your Kindle email address below.
Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations.
‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi.
‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Pencil and paper are two of the greatest tools ever invented for studying science and engineering problems. But, there comes a point when the amount of data and complexity of the calculations require us to use more powerful approaches. The calculator is a scientist or engineer’s basic workhorse for making those calculations. While Python has a wide range of applications, from graphing to modeling to data analysis, the simplest (and sometimes most useful) way is to use Python as a calculator.
In Section 22.4.2, we mentioned just-in-time compilation tools such as cython and numba that enable us to write parts of our Python code in another language or convert our Python code into an optimized non-Python version. The goal with using cython and numba is to enable our Python code to run faster. There are times, however, when we have a collection of legacy Fortran or C++ routines – battle-tested and reliable – that we want to use “as-is” from inside Python. The code files already exist external to Python, so we want to compile and wrap them up and expose them to Python to use.
So far, we have seen how to use Python to access and/or store information in two different kinds of file formats: text files (Chapter 9) and image files (Chapter 13). The former hold numbers, letters, punctuation characters, and some special characters. The latter hold the image information and are encoded in formats such as the Portable Network Graphics (PNG) and Joint Photographic Experts Group (JPEG) formats.
We have looked at calculations involving diagnostic analysis, calculations of various quantities at a given moment in time based on other values at that time. In this chapter, we consider a case of prognostic modeling, where we examine how a quantity changes with time, given an equation that relates that quantity to time (mathematically, where 𝑦 = 𝑓(𝑡)). In a prognostic model, the quantity we are modeling at some future time is somehow dependent on values at the present time.
As mentioned in the Preface, we have saved this chapter on how to write programs (except for a brief look in Section 6.2.7) until now because we wanted to focus our teaching of how to program on “doing” rather than on “talking about doing.” So, our problems so far have been generally on the short side. As we move into longer and more complex programs, we are in a place to consider more detailed advice about how to write programs. In this chapter, we discuss three topics regarding how to write programs: the process of writing programs, the importance of testing, and the importance of style conventions.
The Preface and To the Student sections describe how and why we structured the book and resources the way we did and how to make the most out of them. In the present chapter, we set the stage for the study of programming as an endeavor and Python as a language. We also describe what software needs to be installed in order to make use of the rest of the book.
After making calculations, a second major task computers can help scientists and engineers with is making plots of data and calculations. In this chapter, we examine how to make a basic line and scatter plot. In Chapter 5, we discuss how to customize a basic line and scatter plot to make it more useful and readable.
The topics of searching (finding certain values in a collection of values) and sorting (putting values into an order) are not new to us. As we have examined various scientific and engineering computing use cases, we have encountered both topics. For searching, we have seen the use of if statements, boolean expressions, etc. For sorting, we have seen the use of the sorted function.
In Section 4.2.4 we introduced code comments and Jupyter markdown, and in Section 6.2.6, we introduced docstrings. Although these constructs and practices are vital for the programs we write, there is more to documentation. Documentation is often unsung, but it plays a vital role in making our programs understandable and usable by ourselves and others. Python has a number of tools to streamline the process of creating documentation. In this chapter, we discuss principles of documenting, a more detailed general convention for docstrings than was described in Section 6.2.6, and the Sphinx documentation-generation program.
One way of thinking of what a programming language does is that it provides various structures to store data and information (e.g., variables, lists, arrays, object attributes) and various structures to execute tasks (e.g., assignment, branching, looping, functions, object methods). A program, then, is some combination of these structures that accomplish the calculation, visualization, or other task the user is interested in conducting.
In the , we saw how to make a basic line and scatter plot. As useful as the default values are in Matplotlib, we often want to make our plots look different. In this chapter, we describe some of the ways of doing so. We also introduce more features of Python that enable us to do this customization.
Line and scatter plots, which we described in Chapters 4 and 5, are the bread and butter of scientific visualization. There are, however, many other kinds of graphs used to display and analyze scientific and engineering information. In this chapter, we examine two such kinds of visualization: contour plots and animations of two-dimensional plots and images. Contour plots are a common way of presenting data whose values change based on the location in two-dimensional space.