-
Notifications
You must be signed in to change notification settings - Fork 2
Python Operations Numeric
Python Docs - Numeric Operations
Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the "narrower" type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Comparisons between numbers of mixed type use the same rule. [2] The constructors int(), float(), and complex() can be used to produce numbers of a specific type.
All numeric types (except complex) support the following operations, sorted by ascending priority (all numeric operations have a higher priority than comparison operations):
Operation | Results | Notes | Full documentation |
---|---|---|---|
x + y |
sum of x and y | ||
x - y |
difference of x and y | ||
x * y |
product of x and y | ||
x / y |
quotient of x and y | ||
x // y |
floored quotient of x and y | (1) | |
x % y |
remainder of x / y | (2) | |
-x |
x negated | ||
+x |
x unchanged | ||
abs(x) |
absolute value or magnitude of x | abs() |
|
int(x) |
x converted to integer | (3)(6) | int() |
float(x) |
x converted to floating point | (4)(6) | float() |
complex(re, im) |
a complex number with real part re, imaginary part im. im defaults to zero. | (6) | complex() |
c.conjugate() |
conjugate of the complex number c | ||
divmod(x, y) |
the pair (x // y, x % y) | (2) | divmod() |
pow(x, y) |
x to the power y | (5) | pow() |
x ** y |
x to the power y | (5) |
Notes:
-
Also referred to as integer division. The resultant value is a whole integer, though the result's type is not necessarily int. The result is always rounded towards minus infinity:
1//2
is0
,(-1)//2
is-1
,1//(-2)
is-1
, and(-1)//(-2)
is0
. -
Not for complex numbers. Instead convert to floats using
abs()
if appropriate. -
Conversion from floating point to integer may round or truncate as in C; see functions
math.floor()
andmath.ceil()
for well- defined conversions. -
float
also accepts the strings“nan”
and“inf”
with an optional prefix“+”
or“-”
for Not a Number (NaN) and positive or negative infinity. -
Python defines
pow(0, 0)
and0 ** 0
to be1
, as is common for programming languages. -
The numeric literals accepted include the digits 0 to 9 or any Unicode equivalent (code points with the
Nd
property).
See Unicode Derived Numeric Type for a complete list of code points with the
Nd
property.
TODO
- Add excercises
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links