15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

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.

98.7183139527<br />

>>> 8 / 3<br />

2<br />

>>> 8.0 / 3.0<br />

2.66666666667<br />

>>> 8 % 3<br />

2<br />

>>> (60. - 32.) * ( 5. / 9. )<br />

15.5555555556<br />

>>> 14 * 0x04<br />

56<br />

>>> 0170 / 4<br />

30<br />

>>> 0x80 + 0777<br />

639<br />

>>> 45L * 22L<br />

990L<br />

>>> 16399L + 0xA94E8L<br />

709879L<br />

>>> -2147483648L - 52147483648L<br />

-54294967296L<br />

>>> 64.375+1j + 4.23-8.5j<br />

(68.605-7.5j)<br />

>>> 0+1j ** 2 # same as 0+(lj**2)<br />

(-1+0j)<br />

>>> 1+1j ** 2 # same as 1+(lj**2)<br />

0j<br />

>>> (1+1j) ** 2<br />

2j<br />

Note how the exponentiation operator is still higher in priority than the binding addition operator that<br />

delimits the real and imaginary components of a complex number. Regarding the last example above,<br />

we grouped the components of the complex number together to obtain the desired result.<br />

5.5.4. *Bit Operators (Integer-Only)<br />

<strong>Python</strong> integers may be manipulated bitwise and the standard bit operations are supported: inversion,<br />

bitwise AND, OR, and exclusive OR (aka XOR), and left and right shifting. Here are some facts regarding<br />

the bit operators:<br />

● Negative numbers are treated as their 2's complement value.<br />

● Left and right shifts of N bits are equivalent to multiplication and division by (2 ** N) without<br />

overflow checking.<br />

● For longs, the bit operators use a "modified" form of 2's complement, acting as if the sign bit<br />

were extended infinitely to the left.<br />

The bit inversion operator ( ~ ) has the same precedence as the arithmetic unary operators, the highest<br />

of all bit operators. The bit shift operators ( > ) come next, having a precedence one level<br />

below that of the standard plus and minus operators, and finally we have the bitwise AND, XOR, and OR<br />

operators (&, ^, | ), respectively. All of the bitwise operators are presented in the order of descending<br />

priority in Table 5.4.<br />

Table 5.4. Integer Type Bitwise Operators

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

Saved successfully!

Ooh no, something went wrong!