| Goals: |
|
An algorithm is an finite list of commands that take input and produce desired output in finitely many steps. The simulation examples given earlier all illustrate algorithms. The goal of this chapter is to develop further the analysis and construction of algorithms, and to do so using precision in how the algorithms are defined. Defining algorithms precisely can be done in a wide variety of ways, but the vehicle we will use is called Python. Python is widely-used high-level programming language that will provide a common way for us to communicate computational algorithms.
Note: The purpose of this chapter is not to train you to be a Python programmer, but instead to engage the mind in algorithmic thinking. Some parts of courses are constructed around building skills, while some parts are about a journey. This part is journey. Do know though that what is illustrated in this chapter can form powerful and highly marketable skills. If you like this stuff, you might consider taking some computer science courses. They’re great fun and add power to résumés.
Because installations of Python vary from year to year, we assume in this text that your instructor has shown you how to start Python, whether you are in a traditional classroom or online course.
Open Python. Depending on the setup, you will have one or two windows. Select the window labelled as Python Shell. It will look like Figure 5.1.
At the >>> prompt, type in some arithmetic expression, such as and then hit Enter. When you hit Enter, Python will attempt to process whatever you’ve typed. Figure 5.2 contains the result.
Addition, subtraction, multiplication, and parentheses work in Python as in Excel. Try computing and in the Shell. You should see Figure 5.3.
Division and exponentiation, however, are both different in Python. If and are integers, x/y will compute the quotient of divided by 1616Henceforth, Python commands will be displayed using this font. For example, try 9/4 in the Shell, and you’ll get 2. To get a floating-point approximation to one of the two needs to be a floating-point number.1717You can think of a floating-point number as a number with a decimal in it. The simplest option is to type 9/4.0, and you’ll get 2.25. Figure 5.4 demonstrates.
To compute in Python, type x**y. Figure 5.5 demonstrates Be careful, 28 does something different (note the output).
Two examples:
We you start Python, a minimal number of commands are automatically loaded. For example, you will recall the exponential function of base As with Excel, this function is called using exp(). But, before calling the function, we need to load it. Try executing exp(1) in Python and you will get an unsightly error. There is a large number of libraries of commands for Python, and we will rely on the one named numpy. This library contains many definitions of functions for working with numbers, including the familiar functions on a scientific calculator. The following command will load the library:
| from numpy import * |
Now try exp(1) again and you’ll see an approximation to the number as in Figure 5.6.
A note: Recall that logarithms are the inverse functions to exponential functions. The natural logarithm function is the inverse function of exp. The most common mathematics notations for the natural logarithm function are and The numpy library uses log. To see this, execute log(exp(4)).
Some examples using commands from numpy:
Another way we can use Python like a standard calculator is with storing numbers. But, with Python we can do so more elegantly, and with greater power. We can assign values to variables in Python, and then compute with them symbolically. To assign a value, we use the equal sign, =. For example, executing x = 4 will store the value of 4 into x. (We’ll discuss naming things as we develop examples.) Then executing x**2 - x will yield the correct value, as shown in Figure 5.7.
Answer the following as True or False.
The module numpy is needed to use the command exp.
If we wanted to square the value of 2, we would type 22.
If we typed 2/2 in Python, the value of 0 would be produced.
Addition and subtraction symbols are exactly the same as that in Excel.
The command for square root in Python is sqrt.
Store and and . Compute the following.