Numbers#
Number data types store numeric values. Python supports the following numerical types:
int — integers (no size limit in Python 3)
float — floating-point real values
complex — complex numbers (e.g.,
3+4j)
Note
Python 2 had a separate long type. In Python 3, int handles arbitrarily large integers automatically.
Type Conversion#
Function |
Description |
|---|---|
|
Convert x to an integer |
|
Convert x to a floating-point number |
|
Convert x to a complex number with imaginary part zero |
Math Functions#
These functions require import math:
Function |
Description |
|---|---|
|
Absolute value of x |
|
The exponential of x (e^x) |
|
Natural logarithm of x (x > 0) |
|
Base-10 logarithm of x (x > 0) |
|
Square root of x (x > 0) |
|
Cosine of x (in radians) |
|
Sine of x (in radians) |
Constants#
math.pi— the mathematical constant πmath.e— the mathematical constant e
Examples#
>>> (50 - 5 * 6) / 4
5.0
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
>>> 17 / 4 # True division (returns float in Python 3)
4.25
>>> 17 // 4 # Floor division (returns integer quotient)
4
>>> 17 % 4 # Modulus (remainder)
1
Warning
Python 2 vs Python 3
In Python 2, 17/4 returned 4 (integer division). In Python 3, / always returns a float. Use // for integer division.