Number Type Fundamentals
# integer addition
>>> 1 + 1
2
# floating-point multiplication
>>> 10 * 0.5
5.0
# 3 to the power 100
>>> 3 ** 100
515377520732011331036461129765621272702107522001| Literal | Interpretation |
|---|---|
| 1234, -24, 0, 99999999999999 | Integers (unlimited size) |
| 1.23, 1., 3.14e-10, 4E210, 4.0e+210 | Floating-point numbers |
| 0o177, 0x9ff, 0b101010 | Octal, hex, and binary literals in 3.X |
| 0177, 0o177, 0x9ff, 0b101010 | Octal, octal, hex, and binary literals in 2.X |
| 3+4j, 3.0+4.0j, 3J | Complex number literals |
| set(‘spam’), {1, 2, 3, 4} | Sets: 2.X and 3.X construction forms |
| Decimal(‘1.0’), Fraction(1, 3) | Decimal and fraction extension types |
| bool(X), True, False | Boolean type and constants |
math Module
| Function name | Expression |
|---|---|
math.sqrt(x) |
√x |
math.exp(x) |
e^x |
math.log(x) |
ln(x) |
math.log(x, b) |
log_b(x) |
math.log10(x) |
log_10(x) |
math.sin(x) |
sin(x) |
math.cos(x) |
cos(x) |
math.tan(x) |
tan(x) |
math.asin(x) |
arcsin(x) |
math.acos(x) |
arccos(x) |
math.atan(x) |
arctan(x) |
math.sinh(x) |
sinh(x) |
math.cosh(x) |
cosh(x) |
math.tanh(x) |
tanh(x) |
math.asinh(x) |
arsinh(x) |
math.acosh(x) |
arcosh(x) |
math.atanh(x) |
artanh(x) |
math.hypot(x, y) |
The Euclidean norm, √(x² + y²) |
math.factorial(x) |
x! |
math.erf(x) |
The error function at x |
math.gamma(x) |
The gamma function at x, ω(x) |
math.degrees(x) |
Converts x from radians to degrees |
math.radians(x) |
Converts x from degrees to radians |
# import the math module
>>> import math
# base-y log of x
>>> math.log(12, 8)
1.1949875002403856
# base-10 log of x
>>> math.log10(12)
1.0791812460476249
# import the random module
>>> import random
# a draw from a normal distribution with mean = 0 and standard deviation = 1
>>> random.normalvariate(0, 1)
-0.136017752991189
# trigonometric functions
>>> math.cos(0)
1.0
>>> math.sin(0)
0.0
>>> math.tan(0)
0.0
# an expression containing a factorial product
>>> math.factorial(4) - 4 * 3 * 2 * 1
0| Operator | Description |
|---|---|
x + y |
Addition, concatenation |
x - y |
Subtraction, set difference |
x * y |
Multiplication, repetition |
x % y |
Remainder, format |
x / y, x // y |
Division: true and floor |
-x, +x |
Negation, identity |
~x |
Bitwise NOT (inversion) |
x ** y |
Power (exponentiation) |
# let us assign the variables 'x' and 'y' to two number objects
>>> x = 2
>>> y = 4.0
# subtracting an integer from variable 'x'
>>> x - 1
1
# dividing the variable 'y' by an integer
>>> y / 73
0.0547945205479452
# integer-dividing the variable 'y' by an integer
>>> y // 73
0.0
# getting a linear combination of 'x' and 'y'
>>> 3 * x - 5 * y
-14.0
# assigning the variable 'z' to the linear combination of 'x' and 'y'
>>> z = 3 * x - 5 * y| Number | Format | Output | Description |
|---|---|---|---|
| 3.1415926 | {:.2f} |
3.14 | Format float 2 decimal places |
| 3.1415926 | {:+.2f} |
+3.14 | Format float 2 decimal places with sign |
| -1 | {:+.2f} |
-1.00 | Format float 2 decimal places with sign |
| 2.71828 | {:.0f} |
3 | Format float with no decimal places |
| 5 | {:0>2d} |
05 | Pad number with zeros (left padding, width 2) |
| 5 | {:x<4d} |
5xxx | Pad number with x’s (right padding, width 4) |
| 10 | {:x<4d} |
10xx | Pad number with x’s (right padding, width 4) |
| 1000000 | {:,} |
1,000,000 | Number format with comma separator |
| 0.25 | {:.2%} |
25.00% | Format percentage |
| 1000000000 | {:.2e} |
1.00e+09 | Exponent notation |
| 13 | {:10d} |
13 | Right aligned (default, width 10) |
| 13 | {:<10d} |
13 | Left aligned (width 10) |
| 13 | {:^10d} |
13 | Center aligned (width 10) |
# assign the variable 'a' to a floating-point number
>>> a = 0.67544908755
# displaying 'a' with the first two decimals only
>>> "{:.2f}".format(a)
"0.68"
# displaying 'a' with the first three decimals only
>>> "{:.3f}".format(a)
"0.675"# less than
>>> 3 < 2
False
# greater than or equal
>>> 1 <= 2
True
# equal
>>> 2 == 2
True
# not equal
>>> 4 != 4
False
# range test
>>> x = 3
>>> y = 5
>>> z = 4
>>> x < y < z
False
# joined test
>>> x < y and y > z
True
# disjoined test
>>> x < y or y < z
TrueFootnotes
Floating numbers are stored in binaries with an assigned level of precision typically equivalent to 15 or 16 decimals.↩︎
As per the documentation of the Python programming language,
mathcannot be used with complex numbers.↩︎The official Python documentation has an extensive section on operator precedence rules in the section dedicated to syntax of expressions↩︎