Python provides three integral types: bool, int, and long. The bool type can only take the values True or False, and when used in a numeric context these are treated as 1 and 0. The long type can hold an integer whose size is limited only by the machine's available memory, so integers hundreds of digits long can be created and processed. The only downside is that the long type is slower to pro-
We need to bear in mind the fact that the = operator performs a binding operation rather than an assignment. The name on the left-hand side is bound (or rebound if the name already exists) to the object on the right-hand side. For immutable objects, it makes no difference at all, as we will see in a moment. But for mutable objects, it means that using = will not give us a copy (it just binds another name to the original object), so when we really need a copy we must use a copy() method, or a function from Python's copy module, as discussed shortly.
★The numbers beside some of the keywords indicate the version of Python that introduced them.
cess than the int type. The int type is the same signed integer type provided by most programming languages; however, if an operation is applied to an int that would make its value exceed its range (for example, a value greater than 231 -1 or less than -231 on some machines), the int is automatically type promoted into a long.
Python uses the suffix L to signify a long, and we can do the same in code when necessary. For example: >>> p = 5 ** 35
>>> q = 7L >>> r = 2 + q >>> p, q, r
(2910383045673370361328125L, 7L, 9L)
Integer literals are assumed to be base 10 (decimal) numbers, except those that start with a 0x, which are treated as hexadecimal (base 16), for example, 0x3F, which is decimal 63, and those that start with 0 which are treated as octal (base 8). Any kind of integer literal can have L appended to it to make it into a long.
Python supports the common operators that we would expect for numbers, including +, -, *, /, %, and their augmented cousins, +=, -=, *=, /=, and %=. Python also provides ** for raising a number to a power.
By default, Python's / division operator performs truncating division when both operands are of type int. For example, 5/3, produces 1. This is the norm in most programming languages, but it can be inconvenient in Python since dynamic typing means that a variable might be an int or a float at different times. The solution is to tell Python to always do "true division", which produces floating-point results whenever necessary, and to use the // operator when we really want truncation to occur. We will see how to do this in Chapter 4.
Was this article helpful?
Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.