Python Numbers

Python has three types of numeric data types. They are:

  • int
  • float
  • complex

int

The int data type is a numeric type without fractional point.

Example

x = 100
print(x)
  
Output
100

float

The float data type is a numeric type with fractional point.

Example

x = 100.70
y = 53e43
print(x)
print(y)
  
Output
100.70
5.3e+44

complex

The complex data type is a numeric type with real and imaginary components denoted by j character.

Example

z = 7j
print(z)
  
Output
7j

Type Conversion

There are functions such as int(), float(), complex() available in Python to convert from one type to another.

Example

a = 10
b = 20.28
c = 8j
#convert from int to float
print(float(a))

#convert from float to int
print(int(b))

#convert from int to complex
print(int(c))
  
Output
<class 'float'>
<class 'int'>
<class 'complex'>

Note: A complex number cannot be converted into int or float type.

Generate Random Numbers

Python has a built-in module random for generating random numbers.

Example

import random
print(random.randrange(1,100))