Numeric Data Types in Python
Python supports three built-in numeric data types that are used to store numbers:
-
int– whole numbers -
float– decimal numbers -
complex– numbers with a real and imaginary part
A numeric variable is created automatically when a numeric value is assigned to it.
Example: Creating Numeric Variables
Checking the Type of a Number
To find out which numeric type a variable belongs to, you can use the
type()
function.
Example: Verifying Data Types
Integer Numbers (int)
An integer is a whole number that can be positive, negative, or zero. Integers in Python can be very large and do not have a fixed size limit.
Example: Integer Variations
Floating Point Numbers (float)
A float is a number that contains a decimal point. It can also represent numbers written in scientific notation.
Example: Decimal Values
Scientific Notation
Python allows floats to be written using
e or
E to
represent powers of 10.
Complex Numbers (complex)
Complex numbers consist of a real part and an imaginary part. The imaginary
part is represented using the letter
j.
Example: Complex Variables
Converting Between Numeric Types
Python allows you to convert numbers from one type to another using built-in
functions such as
int(),
float(), and
complex().
Example: Type Casting
⚠️ Note: Complex numbers cannot be converted into integers or floats.
Generating Random Numbers
Python does not generate random numbers automatically, but it provides a
built-in module called
random for
this purpose.
