Python/Tutorial/Basic Types and Operators/int

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Introduction

Ints (short for "integers") are basic counting numbers, 0, and the negative numbers. Using only integers, a surprising amount of math is possible.

Ints, however, have limitations that make floats or Decimals more appealing in certain cases.

We have already examined ints as we explored the basic Python syntax. Now we are going to expand our understanding of integers.

Int Literals

Anything that starts with a number 1-9 and contains only numerals 0-9 is interpreted as an int literal.

>> 157
157
>>> 23
23

Long Literals

You can even use really, really large numbers.

>>> 123456789012345678901234567890
123456789012345678901234567890L

Notice that you didn't have to put an "L" at the end. Python is smart enough that it knows you wanted a long, not a regular old int.

Octal Literals

Be careful! If you start with a zero, you're actually writing in base-8, or octal. Don't make this newbie mistake:

>>> 077
63
>>> 099
  File "<stdin>", line 1
    099
      ^
SyntaxError: invalid token

Hexadecimal Literals

On the same subject, you can write out hexadecimal literals if you start with '0x'.

>>> 0xff
255

Binary Literals

And you can do binary literals as well, starting with '0b'.

>>> 0b11111111
255

Binary, Octal, Decimal, and Hexadecimal

What's all this business with binary, octal, and hexadecimal?

Computers do all their work with bits and bytes. A bit is a binary digit. It can either be on or off, 1 or 0.

If you recall back to your math classes, you may remember hearing about bases other than base-10, decimal. A number in a different base has a different value of the tens, hundreds, etc... columns.

To understand this concept, let's examine the number 248.

  • The "8" in 248 stands for 8. It's in the 1's column, so you multiply by 1 to get its true value.
  • The "4" stands for 40. It's in the 10's column, so you multiply 4 by 10 to get its true value of 40.
  • The "2" stands for 200. It's in the 100's column, so you multiple 2 by 100 to get its true value of 200.

You'll note that 1 is the same as 100, 10 is 101, and 100 is 102.

Or, a different way of looking at it:

  • 1 = 1 (no 10's)
  • 10 = 1 * 10 (1 10)
  • 100 = 1 * 10 * 10 (2 10's)

With binary, you use 2 instead of 10. So each column has a value, starting from the right, of 1, 2, 4, 8, 16, 32, 64, etc...

With octal, you use 8 in instead of 10. So each column has a value of 1, 8, 64, 512, etc...

With hexadecimal, you use 16 instead of 10. So each column has a value of 1, 16, 256, 4096, etc...

What numerals are you allowed to use? In decimal, we have base-10, so 10 numerals. These are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

With binary, base-2, there are only 2 numerals: 0 and 1.

Octal has 8 numerals, 0, 1, 2, 3, 4, 5, 6, 7. (8 and 9 are not in octal!)

Hexadecimal has 16 numerals, 0-9 and the additions of a, b, c, d, e and f, which mean 10, 11, 12, 13, 14 and 15 respectively.

hex and binary

Hexadecimal and binary share a special relationship. You see, four digits in binary is the same as 1 digit in hexadecimal. Let me explain.

Binary Hexadecimal
0b0000 0
0b0001 1
0b0010 2
0b0011 3
0b0100 4
0b0101 5
0b0110 6
0b0111 7
0b1000 8
0b1001 9
0b1010 a
0b1011 b
0b1100 c
0b1101 d
0b1110 e
0b1111 f

This makes hexadecimal a neat, compact representation of binary. The conversion between the two is very straightforward. If you have a long sequence of binary, then you can break it into groups of 4 and then translate it to hexadecimal quite easily, and vice-versa.

I've added spaces below so you can see this more clearly. Just keep in mind you can't add spaces in numbers in Python.

0b1110 0101 0010 0111 0011 1011 0101 1101 0001 1000
0x   e    5    2    7    3    b    5    d    1    8

int and long

Your machine may be a 32-bit or a 64-bit machine. This means that natively it uses numbers that have 32 binary digits or 64 binary digits.

On 32-bit machines, the numbers between -4,294,967,296 and 4,294,967,295 (plus or minus 4 billion) can be accurately represented. Since this is the native number size for the architecture, operations that involve numbers within this range are very, very fast. It's what your computer is born to do.

On 64-bit machines, the numbers between -18,446,744,073,709,551,616 and 18,446,744,073,709,551,615 (plus or minus 18 pentillion) can be accurately represented by one of the native numbers. These numbers also are very fast when computed.

However, there are cases where you'd like to exceed you're architecture's native number size. This is what longs are for. They can accurately represent any number of any size (provided your computer has enough memory). Operations on these numbers are fast, but significantly slower than the native ints. This is because it has to do calculations the way you have to on a piece of paper: by breaking the number down into smaller numbers that the computer can work with.

The transition between int and long is very smooth in Python. When it is done, you'll see an 'L' appear at the end of the number. In the vast majority of cases, you do not have to worry about int limits at all.

Mathematical Operators

These operators handle ints.

  • +x: Does nothing.
  • -x: Takes the negative of the number. Switches negative to positive and vice-versa.
  • x + y: Adds x and y together.
  • x - y: Subtracts y from x.
  • x * y: Multiples x and y together.
  • x / y or x//y: Divides x by y, then rounds down, using the same logic as math.floor(): the largest integer smaller than what you'd get if you actually did real division.
  • x % y: modulo or mod. This returns the remainder after division. You'd be surprised how useful this one is, so don't ignore it!
  • x * *y: Raises x to the power of y.

Source: http://docs.python.org/release/2.6.6/reference/expressions.html#unary-arithmetic-and-bitwise-operations, http://docs.python.org/release/2.6.6/reference/expressions.html#binary-arithmetic-operations

Bitwise Operations

Bit Shifting

There are two shifting operations. What this means is basically to divide or multiple by 2 raised the power of the other number. Note that you can't shift negative numbers.

  • x << y: Returns x*(2**y), which is the same as if you shifted the binary representation of your number to the left by y bits. Or, in other words, added y 0's to the right in binary.
  • x >> y: Returns x/(2**y), which is a right shift, which basically drops y bits from the number.

When would you use shifting? The only case is when you are working with binary operators below. Sometimes you have a number that has several bits, each with a different meaning. You can create or analyze the bits one by one with shifts.

Source: http://docs.python.org/release/2.6.6/reference/expressions.html#shifting-operations

not

  • ~x: Returns x, with all the bits inverted, up until the highest bit, plus one additional high bit for good measure.

This is generally used in conjunction with the following bit-wise operators.

Source: http://docs.python.org/release/2.6.6/reference/expressions.html#unary-arithmetic-and-bitwise-operations

and, or, xor

The bit-wise operations work much like the boolean logic operators and, or, and not. The difference is that the operations are performed on each pair of bits, one at a time.

  • x & y: Bitwise 'and'.
  • x | y: Bitwise 'or'
  • x ^ y: Bitwoise 'xor'.

Xor needs a little explanation. It is true only when the two bits are different. IE,

  • 1 ^ 1 => 0
  • 0 ^ 0 => 0
  • 1 ^ 0 => 1
  • 0 ^ 1 => 1

When would you use bitwise operations? In the bad old days when everyone programmed in C, memory was limited and disk space was expensive, people tried to pack a much information into single bytes as possible. This is still true, to a degree, with many file binary file formats. In order to pull out the meanings:

  • Take the byte, 'and' it with a mask to see if any of the bits in the mask are set.
  • Take the byte, 'or' it with a mask to set particular bits.

For xor, its use is generally cryptography and to a lesser degree, computer graphics.

Source: http://docs.python.org/release/2.6.6/reference/expressions.html#binary-bitwise-operations

Comparators

We've looked at comparators in the boolean section.

Order of Operations

The mathematical operators behave much like their mathematical counterparts. There are few surprised. What you need to know, however, is how these operators combine with the operators we have already covered, and operators yet to be covered.

Now that you have a sufficient dictionary of operators, you should consult the reference documentation to see how they compare.

Source: http://docs.python.org/release/2.6.6/reference/expressions.html#summary

Built-In Functions

There are a number of built-in functions that apply exclusively to ints.

  • int([x[, base]]): Converts a string or another number to an int or long. Without any arguments, returns 0.
  • abs(x): Returns the absolute value. (Makes negative numbers positive, and keeps positive numbers positive.)
  • cmp(x, y): Compares x and y, returning:
    • 0 if they are equal
    • -1 if x is smaller
    • 1 if y is smaller
  • long([x, [base]]): Same as int(), but returns a long.
  • pow(x, y[, z]): Returns x**y. If you pass in z, then it returns (x**y)%z in an efficient way.

These functions help you see what the binary, octal, and hexadecimal representation of a number is. Note that we haven't covered strings yet, so for now, just use it to look at the representations.

  • bin(x): Binary representation. Note that negative numbers are '-0b...'.
  • oct(x): Octal representation.
  • hex(x): Hexadecimal representation.

The results of the above can be fed into int() or long() and they shouldn't complain, as long as you do not specify bases.

Practice

  • Lots of tricky operations, building one on another.
  • Lots of functions combined with operators.
  • Analysis of a syntax of an expression, walking through like Python does.
  • Practice writing your own expressions for specific purposes.

Conclusion