Python Numbers
In python, the number data types are used to store numeric values.
Python supports three numeric data types:
- int – negative and non-negative numbers having no decimal point
- float – used to store decimal point value
- Complex – It is of the form a + bj in which a is the real part and the in bj j j is the imaginary one.
Let’s understand one by one with the examples:
- int: they are whole numbers, the negative and non-negative numbers having no decimal point. There is no limit to integers in Python.
Python Numbers With Examples
Example 1
output: 2
<class ‘int’>
type(): it is a function through which we specify the type of the object, whether it is int, float, or double.
Float: used to store floating point numbers or decimal point numbers, it helps in storing the result which has decimal point number like 4.34, 4.56788.
Example 2
Divide a number by a number produces a float
Output 0.8
0.7777777777777778
0.9
1.9098360655737705
<class ‘float’>
<class ‘float’>
<class ‘float’>
<class ‘float’>
1.909836065573770 has many digits after decimal so,
Note: accuracy of the float point number is up to 15 decimal places.
Example 3:
Float is produced by multiplication by multiplying two float numbers,
output:
63.0
<class ‘float’>
- Complex: The complex number consists of two parts the real and the imaginary one.
Example 4:
3 + 4j
The 3 is the real component and 4j is the imaginary one(j is the imaginary part)
Example 5:
Output:
<class ‘complex’>
Random Number in Python:
Python has a built-in module which is known as random which is used to generate random numbers.
For using built-in modules we have to import the modules through import.
Syntax:
import module name
we will use randrange() method which returns a randomly selected element from the given range.Let’s understand this by the example
Example 6: