|
| 1 | +Python has three different types of built-in numbers: integers ([`int`][int]), floating-point ([`float`][float]), and complex ([`complex`][complex]). Fractions ([`fractions.Fraction`][fractions]) and Decimals ([`decimal.Decimal`][decimals]) are also available via import from the standard library. |
| 2 | + |
| 3 | +Whole numbers (_including hex, octals and binary numbers_) **without** decimal places are identified as `ints`: |
| 4 | + |
| 5 | +```python |
| 6 | +#whole number |
| 7 | +>>> 1234 |
| 8 | +1234 |
| 9 | +>>> type(1234) |
| 10 | +<class 'int'> |
| 11 | + |
| 12 | +>>> -12 |
| 13 | +-12 |
| 14 | +``` |
| 15 | + |
| 16 | +Hex numbers are written/printed with `0x` prefix: |
| 17 | + |
| 18 | +```python |
| 19 | +#hex number |
| 20 | +>>> 0x17 |
| 21 | +23 |
| 22 | +>>> type(0x17) |
| 23 | +<class 'int'> |
| 24 | +``` |
| 25 | + |
| 26 | +Octals are written with a `0o` prefix: |
| 27 | + |
| 28 | +```python |
| 29 | +#octal number |
| 30 | +>>> 0o446 |
| 31 | +294 |
| 32 | +>>> type(0o446) |
| 33 | +<class 'int'> |
| 34 | +``` |
| 35 | + |
| 36 | +Binary numbers are prefixed with `0b`, and written with only zeros and ones: |
| 37 | + |
| 38 | +```python |
| 39 | +#binary number |
| 40 | +>>> 0b1100110 |
| 41 | +102 |
| 42 | +>>> type(0b1100110) |
| 43 | +<class 'int'> |
| 44 | +``` |
| 45 | + |
| 46 | +Each of these `int` displays can be converted into the other via constructor: |
| 47 | + |
| 48 | +```python |
| 49 | + |
| 50 | +>>> starting_number = 1234 |
| 51 | + |
| 52 | +>>> hex(starting_number) |
| 53 | +'0x4d2' |
| 54 | + |
| 55 | +>>> oct(starting_number) |
| 56 | +'0o2322' |
| 57 | + |
| 58 | +>>> bin(starting_number) |
| 59 | +'0b10011010010' |
| 60 | + |
| 61 | +>>> hex(0b10011010010) |
| 62 | +'0x4d2' |
| 63 | + |
| 64 | +>>> int(0x4d2) |
| 65 | +1234 |
| 66 | +``` |
| 67 | + |
| 68 | +Numbers containing a decimal point (_with or without any numbers following_) are identified as `floats`: |
| 69 | + |
| 70 | +```python |
| 71 | +>>> 3.45 |
| 72 | +3.45 |
| 73 | +>>> type(3.45) |
| 74 | +<class 'float'> |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +Appending `j` or `J` to a number creates an _imaginary number_ -- a `complex` number with a zero real part. `ints` or `floats` can then be added to an imaginary number to create a `complex` number with both real and imaginary parts: |
| 79 | + |
| 80 | +```python |
| 81 | +>>> 3j |
| 82 | +3j |
| 83 | +>>> type(3j) |
| 84 | +<class 'complex'> |
| 85 | + |
| 86 | +>>> 3.5+4j |
| 87 | +(3.5+4j) |
| 88 | +``` |
| 89 | + |
| 90 | +### Arithmetic |
| 91 | + |
| 92 | +Python fully supports arithmetic between these different number types, and will convert narrower numbers to match their less narrow counterparts when used with the binary arithmetic operators (`+`, `-`, `*`, `/`, `//`, and `%`). |
| 93 | + |
| 94 | +Python considers `ints` narrower than `floats`, which are considered narrower than `complex` numbers. Comparisons between different number types behaves as as if the _exact_ values of those numbers were being compared: |
| 95 | + |
| 96 | +```python |
| 97 | +#the int is widened to a float here, and a float is returned |
| 98 | +>>> 3 + 4.0 |
| 99 | +7.0 |
| 100 | + |
| 101 | +#the int is widened to a complex number, and a complex number is returned |
| 102 | +>>> 6/(3+2j) |
| 103 | +(2+2j) |
| 104 | + |
| 105 | +#division always returns a float, even if integers are used |
| 106 | +>>> 6/2 |
| 107 | +3.0 |
| 108 | + |
| 109 | +#if an int result is needed, you can use floor division to truncate the result |
| 110 | +>>> 6//2 |
| 111 | +3 |
| 112 | + |
| 113 | +#when comparing, exact values are used |
| 114 | +>>> 23 == 0x17 |
| 115 | +True |
| 116 | + |
| 117 | +>>> 0b10111 == 0x17 |
| 118 | +True |
| 119 | + |
| 120 | +>>> 6 == (6+0j) |
| 121 | +True |
| 122 | +``` |
| 123 | + |
| 124 | +All numbers (except complex) support all [arithmetic operations][arithmetic operations], evaluated according to [operator precedence][operator precedence]. Support for mathematical functions (beyond `+`, `-`, `/`) for complex numbers can be found in the [cmath][cmath] module. |
| 125 | + |
| 126 | +### Precision & Representation |
| 127 | + |
| 128 | +Integers in Python have [arbitrary precision](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic) -- the amount of digits is limited only by the available memory of the host system. |
| 129 | + |
| 130 | +Floating point numbers are usually implemented using a `double` in C (_15 decimal places of precision_), but will vary in representation based on the host system. Complex numbers have a `real` and an `imaginary` part, both of which are represented by floating point numbers. |
| 131 | + |
| 132 | +For a more detailed discussions of the issues and limitations of floating point arithmetic across programming langages, take a look at [0.30000000000000004.com][0.30000000000000004.com] and [The Python Tutorial][floating point math]. |
| 133 | + |
| 134 | +[int]: https://docs.python.org/3/library/functions.html#int |
| 135 | +[float]: https://docs.python.org/3/library/functions.html#float |
| 136 | +[complex]: https://docs.python.org/3/library/functions.html#complex |
| 137 | +[fractions]: https://docs.python.org/3/library/fractions.html |
| 138 | +[decimals]: https://docs.python.org/3/library/decimal.html#module-decimal |
| 139 | +[0.30000000000000004.com]: https://0.30000000000000004.com/ |
| 140 | +[cmath]: https://docs.python.org/3.9/library/cmath.html |
| 141 | +[arethmetic operations]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex |
| 142 | +[operator precedence]: https://docs.python.org/3/reference/expressions.html#operator-precedence |
| 143 | +[floating point math]: https://docs.python.org/3.9/tutorial/floatingpoint.html |
0 commit comments