Numbers in Python
We introduced the Number data type in the previous article and in this article, we will learn more about it. So what are we waiting for let’s learn?
We can categorize the Numbers in Python into three numeric types. Those are listed below:
int
float
complex
To create variables with the numeric types we need to just assign the relevant value to variables. Below is the example code for the same:
To verify the type of variable or object we can use type() as previously discussed.
Integer (Int):
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
The below figure shows that how we can declare the int variable and verify the type of variable.
Float:
Float, or “floating-point number” is a number, positive or negative, containing one or more decimals.
You can refer to the below figures to understand the declaration and verification of the float type variables.
Float type can also be represented as a scientific number with an “e” to indicate the power of 10.
Complex:
Complex numbers are written with a “j” as the imaginary part and blow figure is the example for declaring the complex numbers:
Type Conversion
We can convert from one numeric type to another with the int(), float(), and complex() methods as given below:
Note: You cannot convert complex numbers into another number type.
Random Number
Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers. For example: