16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12 CHAPTER 1 ■ INSTANT HACKING: THE BASICS<br />

Here 10/3 is 3 because the result is rounded down. But 3×3 is 9, so you get a remainder of<br />

one. When you divide 9 by 3, the result is exactly 3, with no rounding. Therefore, the remainder<br />

is zero. This may be useful if you want <strong>to</strong> check something “every 10 minutes” as in the recipe<br />

earlier in the chapter. You can simply check whether minute % 10 is zero. (For a description on<br />

how <strong>to</strong> do this, see the sidebar “Sneak Peek: The if Statement,” later in the chapter.) As you can<br />

see from the final example, the remainder opera<strong>to</strong>r works just fine with floats as well.<br />

The last opera<strong>to</strong>r is the exponentiation (or power) opera<strong>to</strong>r:<br />

>>> 2 ** 3<br />

8<br />

>>> -3 ** 2<br />

-9<br />

>>> (-3) ** 2<br />

9<br />

Note that the exponentiation opera<strong>to</strong>r binds tighter than the negation (unary minus), so<br />

-3**2 is in fact the same as -(3**2). If you want <strong>to</strong> calculate (-3)**2, you must say so explicitly.<br />

Large Integers<br />

<strong>Python</strong> can handle really large integers:<br />

>>> 1000000000000000000<br />

1000000000000000000L<br />

What happened here? The number suddenly got an L tucked on<strong>to</strong> the end.<br />

■Note If you’re using a version of <strong>Python</strong> older than 2.2, you get the following behavior:<br />

>>> 1000000000000000000<br />

OverflowError: integer literal <strong>to</strong>o large<br />

The newer versions of <strong>Python</strong> are more flexible when dealing with big numbers.<br />

Ordinary integers can’t be larger than 2147483647 (or smaller than –2147483648); if you<br />

want really big numbers, you have <strong>to</strong> use longs. A long (or long integer) is written just like an<br />

ordinary integer but with an L at the end. (You can, in theory, use a lowercase l as well, but that<br />

looks all <strong>to</strong>o much like the digit 1, so I’d advise against it.)<br />

In the previous attempt, <strong>Python</strong> converted the integer <strong>to</strong> a long, but you can do that yourself,<br />

<strong>to</strong>o. Let’s try that big number again:<br />

>>> 1000000000000000000L<br />

1000000000000000000L

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!