Python Numbers
In Python, there are three types of numeric data types: integers, floating-point numbers, and complex numbers.
Integers represent whole numbers, both positive and negative. They can be defined using the int
keyword, or by simply writing the number:
x = 42 y = int(3.14)
Floating-point numbers represent decimal numbers, and are defined using the float
keyword:
x = 3.14 y = float(42)
Complex numbers are used to represent numbers with both real and imaginary parts, and are defined using the complex
keyword:
x = 3 + 4j y = complex(5, 6)
Python also provides a number of built-in functions for working with numeric data, such as abs()
, pow()
, round()
, min()
, and max()
. Here are some examples:
x = abs(-42) # returns 42 y = pow(2, 3) # returns 8 z = round(3.14159) # returns 3 a = min(1, 2, 3) # returns 1 b = max(1, 2, 3) # returns 3
Python also provides support for more advanced mathematical operations through the math
module, which provides functions for trigonometry, logarithms, and more. To use the math
module, you must first import it:
import math x = math.sin(0.5) y = math.log(10)
These are just a few examples of the many ways that Python supports working with numeric data.