Section 5.8. Related Modules


5.8. Related Modules

There are a number of modules in the Python standard library that add on to the functionality of the operators and built-in functions for numeric types. Table 5.8 lists the key modules for use with numeric types. Refer to the literature or online documentation for more information on these modules.

Table 5.8. Numeric Type Related Modules

Module

Contents

decimal

Decimal floating point class Decimal

array

Efficient arrays of numeric values (characters, ints, floats, etc.)

math/cmath

Standard C library mathematical functions; most functions available in math are implemented for complex numbers in the cmath module

operator

Numeric operators available as function calls, i.e., operator.sub(m, n) is equivalent to the difference (m - n) for numbers m and n

random

Various pseudo-random number generators (obsoletes rand and wHRandom)


For advanced numerical and scientific mathematics applications, there are well-known third-party packages Numeric (NumPy) and SciPy, which may be of interest to you. More information on those two packages can be found at:

http://numeric.scipy.org/

http://scipy.org/

Core Module: random

The random module is the general-purpose place to go if you are looking for random numbers. This module comes with various pseudo-random number generators and comes seeded with the current timestamp so it is ready to go as soon as it has loaded. Here are some of the most commonly used functions in the random module:

randint()

Takes two integer values and returns a random integer between those values inclusive

randrange()

Takes the same input as range() and returns a random integer that falls within that range

uniform()

Does almost the same thing as randint(), but returns a float and is inclusive only of the smaller number (exclusive of the larger number)

random()

Works just like uniform() except that the smaller number is fixed at 0.0, and the larger number is fixed at 1.0

choice()

Given a sequence (see Chapter 6), randomly selects and returns a sequence item



We have now come to the conclusion of our tour of all of Python's numeric types. A summary of operators and built-in functions for numeric types is given in Table 5.9.

Table 5.9. Operators and Built-in Functions for All Numeric Types

Operator/Built-in

Description

Int

Long

Float

Complex

Result[a]

abs()

Absolute value

number[a]

chr()

Character

  

str

coerce()

Numeric coercion

tuple

complex()

Complex factory function

complex

divmod()

Division/modulo

tuple

float()

Float factory function

float

hex()

Hexadecimal string

  

str

int()

Int factory function

int

long()

Long factory function

long

oct()

Octal string

  

str

ord()

Ordinal

  

(str)

 

int

pow()

Exponentiation

number

round()

Float rounding

  

 

float

**[b]

Exponentiation

number

+[c]

No change

number

-[c]

Negation

number

~[c]

Bit inversion

  

int/long

**[b]

Exponentiation

number

*

Multiplication

number

/

Classic or true division

number

//

Floor division

number

%

Modulo/remainder

number

+

Addition

number

-

Subtraction

number

<<

Bit left shift

  

int/long

>>

Bit right shift

  

int/long

&

Bitwise AND

  

int/long

^

Bitwise XOR

  

int/long

|

Bitwise OR

  

int/long


[a] A result of "number" indicates any of the four numeric types, perhaps the same as the operands.

[b] ** has a unique relationship with unary operators; see Section 5.5.3 and Table 5.2.

[c] Unary operator.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net