Builtin Functions on Lists

Section 2.6, Function Basics, on page 30 introduced a few of Python's built-in functions. Some of these, such as len, can be applied to lists as well, as can others that we haven't seen before (see Figure 5.5). Here they are in action working on a list of the half-lives3 of our plutonium isotopes > > > half_lives 87.74, 24110.0, 6537.0, 14.4, 376000.0 > > > len(half_lives) 5 > > > max(half_lives) 376000.0 > > > sum(half_lives) 406749.14000000001 We can use the results of...

First Steps

That's enough theory let's create a database and start doing things with it. As a running example, we will use the predictions for regional Southeastern Africa 743112 Northern Africa 1037463 South America Eastern Europe North America Western Europe Japan Figure 15.2 Estimated world population in 2300 populations in the year 2300 shown in Figure 15.2, which is taken from http www.worldmapper.org. (These values are shown graphically in Figure 15.3, on the next page.) As promised earlier, we start...

Lists and Indices

Figure 5.1, on the next page, taken from http www.acschannelislands. org 2008CountDaily.pdf, shows the number of gray whales counted near the Coal Oil Point Natural Reserve in a two-week period in the spring of 2008. Using what we have seen so far, we would have to create fourteen variables to keep track of these numbers (see Figure 5.2, on the following page). If we wanted to track an entire year's worth of observations, we'd need 366 (just in case it was a leap year). Even worse, if we didn't...

Using Joins to Combine Tables

When designing a database, it often makes sense to divide data between two or more tables. For example, if we are maintaining a database of patient records, we would probably want at least four tables one for the patient's personal information (such as their name date of birth), a second to keep track of their appointments, a third for information about the doctors who are treating them, and a fourth for information about the hospitals those doctors work in (see Figure 15.6, on the following...

Variables and the Assignment Statement

Most handheld calculators3 have one or more memory buttons. These store a value so that it can be used later. In Python, we can do this with a variable, which is just a name that has a value associated with it. Variables' names can use letters, digits, and the underscore symbol. For example, X, species5618, and degrees_celsius are all allowed, but 777 isn't (it would be confused with a number), and neither is no-way (it contains punctuation). You create a new variable simply by giving it a...

What Is a Type

We've now seen two types of numbers, so we ought to explain exactly what we mean by a type. In computing, a type is a set of values, along with a set of operations that can be performed on those values. For example, the type int is the values , -3, -2, -1, 0, 1, 2, 3, , along with the operators +, -, *, , and (and a few others we haven't introduced yet). On the other hand, 84.2 is a member of the set of float values, but it is not in the set of int values. Arithmetic was invented before Python,...

Pixels and Colors

Most people want to do a lot more to pictures than just display them and crop them. If you do a lot of digital photography, you may want to remove the red-eye caused by your camera flash. You might also want to convert pictures to black and white for printing, highlight certain objects, and so on. To do these things, you must work with the individual pixels that make up the image. The media module represents pixels using the RGB color model discussed in the sidebar on page 72. Module media...

Images

Python Programming Madeleine

Now that we have seen the basic features of modules, objects, and methods, let's look at how they can solve real-world problems. For our running example, we will write some programs that display and manipulate pictures and other images. Suppose you have a file called pic207.jpg on your hard drive and want to display it on your screen. You could double-click to open it, but what does that actually do To start to answer that question, type the following into a Python prompt gt gt gt import media...

Defining Your Own Modules

Section 2.1, The Big Picture, on page 17 explained that in order to save code for later use, you can put it in a file with a .py extension. You can then tell Python to run the code in that file, rather than typing commands in at the interactive prompt. What we didn't tell you then is that every Python file can be used as a module. The name of the module is the same as the name of the file, but without the .py extension. For example, the following function is taken from Section 2.6, Function...

Computer Science Exercises

Here are some exercises for you to try on your own 1. Import module math, and use its functions to complete the following exercises a Write a single expression that rounds the value of -4.3 and then takes the absolute value of that result. b Write an expression that takes the ceiling of sine of 34.5. 2. In the following exercises, you will work with Python's calendar module a Visit the Python documentation website at http docs.python. org modindex.html, and look at the documentation on the...

Write A For Loop To Read The Contents Of Alkaline Metals.txt And Store It In A Nested List With Each Element Of The

Here are some exercises for you to try on your own 1. Assign a list that contains the atomic numbers of the six alkaline earth metals beryllium 4 , magnesium 12 , calcium 20 , strontium 38 , barium 56 , and radium 88 to a variable called alkaline_earth_metals. 2. Which index contains Radium's atomic number Write the answer in two ways, one using a positive index and one using a negative index. 3. Which function tells you how many items there are in alkaline_ earth_metals 4. Write code that...