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:
Checking the Type of a Number
To find out which numeric type a variable belongs to, you can use the
type() function.
Example:
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:
Floating Point Numbers (float)
A float is a number that contains a decimal point. It can also represent
numbers written in scientific notation.
Decimal Values
Example:
Scientific Notation
Python allows floats to be written using e or E to represent powers of
10.
Example:
Complex Numbers (complex)
Complex numbers consist of a real part and an imaginary part. The
imaginary part is represented using the letter j.
Example:
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:
⚠️ 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.
Example:
This program generates a random integer between 1 and 20 (both
inclusive).