Linux/Unix > Python Essential Reference, Second Edition > 4. Operators and Expressions > Operations on Numbers | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137104029061157121123232120

### Operations on Numbers

The following operations can be applied to all numeric types:

Operation | Description
--- | ---
`x` `+` `y` | Addition
`x` `-` `y` | Subtraction
`x` `*` `y` | Multiplication
`x` `/` `y` | Division
`x` `**` `y` | Power (`x` `y` )
`x` `%` `y` | Modulo (`x` `mod` `y` )
`-``x` | Unary minus
`+``x` | Unary plus

For integers, division truncates the result to an integer. Thus, `7/4` is `1`, not `1.75`. The modulo operator returns the remainder of the division `x` `/` `y` . For example, `7 % 4` is `3`. For floating-point numbers, the modulo operator returns the floating-point remainder of `x` `/` `y` , which is `x` `- int(``x` `/` `y` `) *` `y` . For complex numbers, the modulo operator returns `x` `- int((``x` `/` `y` `).``real` `) *` `y` .

The following shifting and bitwise logical operators can only be applied to integers and long integers:

Operation | Description
--- | ---
`x` `<<` `y` | Left shift
`x` `>>` `y` | Right shift
`x` `&` `y` | Bitwise and
`x` `|` `y` | Bitwise or
`x` `^` `y` | Bitwise xor (exclusive or)
`~``x` | Bitwise negation

The bitwise operators assume that integers are represented in a 2’s complement binary representation. For long integers, the bitwise operators operate as if the sign bit is infinitely extended to the left.

In addition, you can apply the following built-in functions to all the numerical types:

Function | Description
--- | ---
`abs(``x` `)` | Absolute value
`divmod(``x` `,``y` `)` | Returns `(int(``x` `/` `y` `),` `x` `%` `y` `)`
`pow(``x` `,``y` `[,``modulo` `])` | Returns `(``x` `**` `y` `)` `x` `%` `modulo`
`round(``x` `,[``n` `])` | Rounds to the nearest multiple of `10-n` (floating-point numbers only)

The `abs()` function returns the absolute value of a number. The `divmod()` function returns the quotient and remainder of a division operation. The `pow()` function can be used in place of the `**` operator, but also supports the ternary power-modulo function (often used in cryptographic algorithms). The `round()` function rounds a floating-point number `x` to the nearest multiple of 10 to the power of `-``n` . If `n` is omitted, it’s set to `0`. If `x` is equally close to two multiples, rounding is performed away from zero (for example, `0.5` is rounded to `1` and `-0.5` is rounded to `-1`).

The following comparison operators have the standard mathematical interpretation and return an integer value of `1` for true, `0` for false:

Operation | Description
--- | ---
`x` `<` `y` | Less than
`x` `>` `y` | Greater than
`x` `==` `y` | Equal to
`x` `!=` `y` | Not equal to (same as `<>`)
`x` `>=` `y` | Greater than or equal to
`x` `<=` `y` | Less than or equal to

Comparisons can be chained together, such as in `w` `<` `x` `<` `y` `<` `z` . Such expressions are evaluated as `w` `<` `x` `and` `x` `<` `y` `and` `y` `<` `z` . Expressions such as `x` `<` `y` `>` `z` are legal, but are likely to confuse anyone else reading the code (it’s important to note that no comparison is made between `x` and `z` in such an expression).

Comparisons involving complex numbers are only valid for equality (`= =`) and inequality (`!=`). Attempts to compare complex numbers with `<`, `<=`, `>`, and `>=` are mathematically meaningless and will raise a `TypeError` exception.

Operations involving numbers are valid only if the operands are of the same type. If the types differ, a coercion operation is performed to convert one of the types to the other:

1. If either operand is a complex number, the other operand is converted to a complex number.

2. If either operand is a floating-point number, the other is converted to a float.

3. If either operand is a long integer, the other is converted to a long integer.

4. Otherwise, both numbers must be integers and no conversion is performed.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.