NUMERIC DATA TYPES
Python uses following three types of numeric data types. type() function in Python is used to get data type of object.
Data Type | Examples | Output | Description |
int | x=1 print(type(x)) | <class โintโ> | Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. |
float | x = 3.5 print(type(x)) | <class โfloatโ> | floating point number is a number, positive or negative, containing one or more decimals. |
complex | x = 1j print(type(x)) | <class โcomplexโ> | Complex numbers are written with a โjโ as the imaginary part. |
Type Conversion in Python
We can convert from one type to another with the int(), float(), and complex() functions as shown below:
int to floatย conversionย in Python |
x=1 a=float(x) print(a) print(type(a)) |
Output |
1.0 <class โfloatโ> |
float to intย conversionย in Python |
x=3.5 a=int(x) print(a) print(type(a)) |
Output |
3 <class โintโ> |
int to complexย conversion in Python |
x=1 a=complex(x) print(a) print(type(a)) |
Output |
(1+0j) <class โcomplexโ> |
Random Number in Python
Python uses a built-in module called random that can be used to make random numbers as shown below:
Example of Random Number generation in Python |
import random print(random.randrange(1, 5)) |
import randomย will import random module.
random.randrange(1, 5)ย function will generate random number between 1 and 4.
Output will be any random number-
Output for Example of Random Number generation in Python |
2 |
Saraswat World Changed status to publish June 14, 2023
Leave a Reply