In Python, a number is a built-in data type used to represent numeric values. Python supports various types of numbers:
1. Integers (int
)
These are whole numbers, positive or negative, without any fractional part.
Example
x = 20 # Integer
y = -10 # Negative integer
2. Floating-point numbers (float
)
These are numbers with decimal points or in exponential notation.
Example:
x = 20.5 # Floating-point number
y = -5.7 # Negative float
z = 3.5e2 # 3.5 * 10^2 (Exponential notation, equivalent to 350.0)
3. Complex numbers (complex
)
These numbers are written in the form a + bj
, where a
is the real part, and b
is the imaginary part.
Example:
x = 3 + 4j # Complex number
y = 2j # Pure imaginary number
Type conversion between numbers
You can convert between different number types using the int()
, float()
, and complex()
functions.
Example:
x = 5 # Integer
y = float(x) # Converts x to 5.0 (float)
z = 3.6
w = int(z) # Converts z to 3 (integer, truncating the decimal part)
c = complex(x, y) # Converts to complex number (5+5.0j)