Algo . . . What?
Before we start programming in earnest, I’ll try to give you an idea of what computer programming is. Simply put, it’s telling a computer what to do. Computers can do a lot of things, but they aren’t very good at thinking for themselves. They really need to be spoon-fed the details. You need to feed the computer an algorithm in some language it understands. Algorithm is just a fancy word for a procedure or recipe—a detailed description of how to do something. Consider the following:SPAM with SPAM, SPAM, Eggs, and SPAM: First, take some SPAM.
Then add some SPAM, SPAM, and eggs.
If a particularly spicy SPAM is desired, add some SPAM.
Cook until done -- Check every 10 minutes.
Not the fanciest of recipes, but its structure can be quite illuminating. It consists of a series of instructions to be followed in order. Some of the instructions may be done directly (“take some SPAM”), while some require some deliberation (“If a particularly spicy SPAM is desired”), and others must be repeated several times (“Check every 10 minutes.”)
Recipes and algorithms consist of ingredients (objects, things) and instructions (statements). In this example, SPAM and eggs are the ingredients, while the instructions consist of adding SPAM, cooking for a given length of time, and so on. Let’s start with some reasonably simple Python ingredients and see what you can do with them.
Numbers and Expressions
The interactive Python interpreter can be used as a powerful calculator. Try the following:>>> 2 + 2
This should give you the answer 4. That wasn’t too hard. Well, what about this:>>> 53672 + 235253
288925
Still not impressed? Admittedly, this is pretty standard stuff. (I’ll assume that you’ve used a calculator enough to know the difference between 1 + 2 * 3 and (1 + 2) * 3.) All the usual arithmetic operators work as expected. Division produces decimal numbers, called floats (or floating-point numbers).>>> 1 / 2
0.5
>>> 1 / 1
1.0
If you’d rather discard the fractional part and do integer division, you can use a double slash.>>> 1 // 2
0
>>> 1 // 1
1
>>> 5.0 // 2.4
2.0
In older versions of Python, ordinary division on integers used to work like this double slash. If you’re using Python 2.x, you can get proper division by adding the following statement to the beginning of your program (writing full programs is described later) or simply executing it in the interactive interpreter:
>>> from __future__ import division
■ Note In case it’s not entirely clear, the future in the instruction is surrounded by two underscores on both sides: _ _future_ _.Another alternative, if you’re running an old Python from the command line, is to supply the commandline switch -Qnew. There is a more thorough explanation of the __future__ stuff in the section “Back to the __future__” later in this chapter. Now you’ve seen the basic arithmetic operators (addition, subtraction, multiplication, and division), but I’ve left out a close relative of integer division.
>>> 1 % 2
1
This is the remainder (modulus) operator. x % y gives the remainder of x divided by y. In other words, it’s the part that’s left over when you use integer division. That is, x % y is the same as x - ((x // y) * y).
>>> 10 // 3
3
>>> 10 % 3
1
>>> 9 // 3
3
>>> 9 % 3
0
>>> 2.75 % 0.5
0.25
Here 10 // 3 is 3 because the result is rounded down. But 3 × 3 is 9, so you get a remainder of 1. When you divide 9 by 3, the result is exactly 3, with no rounding. Therefore, the remainder is 0. This may be useful if you want to check something “every 10 minutes” as in the recipe earlier in the chapter. You can simply check whether minute % 10 is 0. (For a description on how to do this, see the sidebar “Sneak Peek: The if Statement” later in this chapter.) As you can see from the final example, the remainder operator works just fine with floats as well. It even works with negative numbers, and this can be a little confusing.
>>> 10 % 3
1
>>> 10 % -3
-2
>>> -10 % 3
2
>>> -10 % -3
-1
Looking at these examples, it might not be immediately obvious how it works. It’s probably easier to understand if you look at the companion operation of integer division.
>>> 10 // 3
3
>>> 10 // -3
-4
>>> -10 // 3
-4
>>> -10 // -3
3
>>> 2 ** 3
8
>>> -3 ** 2
-9
>>> (-3) ** 2
9
Given how the division works, it’s not that hard to understand what the remainder must be. The important thing to understand about integer division is that it is rounded down, which for negative numbers is away from zero. That means -10 // 3 is rounded down to -4, not up to -3. The last operator we’ll look at is the exponentiation (or power) operator.
Note that the exponentiation operator binds tighter than the negation (unary minus), so -3**2 is in fact the same as -(3**2). If you want to calculate (-3)**2, you must say so explicitly.
Note that the exponentiation operator binds tighter than the negation (unary minus), so -3**2 is in fact the same as -(3**2). If you want to calculate (-3)**2, you must say so explicitly.
Comments
Post a Comment