# Python Numbers

As discussed in the Python Data Types chapter, python has various built-in data types such as numbers, strings, etc. are available to perform standard operations on the variables based on the assigned values. Now, we will learn in detail about numbers in python with examples.

In python, the number data types will store only numeric values, and we have three numeric types such as `int`, `float`, and `complex` types to represent the numbers.

Following is the example of creating the variables with different numeric types in python.

a = 10
b = 31.54
c = 3j
print(type(a)) #<class 'int'>
print(type(b)) #<class 'float'>
print(type(c)) #<class 'complex'>

If you observe the above example, we created different variables (a, b, c) in python by assigning integer, floating-point, and complex number types. Here, we used the `type()` function to know the type of variables.

## Python Int (Integer)

In python, if you create a number, i.e., either positive or negative without having any decimal values, we will call it an integer. The `int` is a data type to represent the integers and the `int` type has no maximum size limit.

Following is the example of creating the integer (`int`) variables in python.

a = 10
b = -500
c = 2456435676
print(a)
print(b)
print(c)
print("a type: ", type(a))
print("b type: ", type(b))
print("c type: ", type(c))

If you observe the above example, we created three integer variables (a, b, c) by assigning different numbers without having any decimal values.

When you execute the above python program, you will get the result as shown below.

10
-500
2456435676
a type: <class 'int'>
b type: <class 'int'>
c type: <class 'int'>

## Python Float

In python, if you create a number, i.e., either positive or negative with at least one decimal value, we will call it to float or floating-point numbers. The `float` is a data type, and it is useful to represent the decimal values.

Following is the example of creating the floating-point number (`float`) variables in python.

a = 10.2
b = -500.45
c = 20.37
print(a)
print(b)
print(c)
print("a type: ", type(a))
print("b type: ", type(b))
print("c type: ", type(c))

If you observe the above example, we created three floating-point variables (a, b, c) by assigning different numbers with decimal values.

When you execute the above python program, you will get the result as shown below.

10.2
-500.45
20.37
a type: <class 'float'>
b type: <class 'float'>
c type: <class 'float'>

In python, the float is also useful to represent the scientific numbers with an “e” to indicate the power of 10. For example, 300000 is represented as 3e5, and 467.423 is represented as 4.67423e2.

Following is the example defining the floating-point variables by assigning the scientific numbers in python.

a = 3e5
b = -9E3
c = 4.67423e2
print(a)
print(b)
print(c)
print("a type: ", type(a))
print("b type: ", type(b))
print("c type: ", type(c))

If you observe the above example, we assigned scientific values to variables. When you execute the above python program, you will get the result as shown below.

300000.0
-9000.0
467.423
a type: <class 'float'>
b type: <class 'float'>
c type: <class 'float'>

## Python Complex

In python, complex number is a number that contains both real and imaginary parts. The complex numbers are written with a “`j`” as the imaginary part like `a + bj`, where a is the real component and b multiplied by `j` is an imaginary component.

For example, 2 + 4j is the complex number where 2 is the real part, and 4 is multiplied by j is the imaginary part.

Following is the example of creating the complex number variables in python.

a = 2 + 4j
b = -50j
c = 20.35j
print(a)
print(b)
print(c)
print("a type: ", type(a))
print("b type: ", type(b))
print("c type: ", type(c))

If you observe the above example, we created three variables (a, b, c) by assigning different complex numbers.

When you execute the above python program, you will get the result as shown below.

(2+4j)
(-0-50j)
20.35j
a type: <class 'complex'>
b type: <class 'complex'>
c type: <class 'complex'>

## Random Number

In python, we don’t have any specific functions like random() to generate random values. If you want to generate some random values, you need to import a built-in random module.

Following is the example of importing a built-in random module to generate the random number between 2 to 10 in python.

import random
#generate random value between 2 to 10
print(random.randint(2, 10))

If you observe the above example, we imported a random module using the import keyword. Whenever you execute the above python program, you will get a random value between 2 to 10.

## Type Conversion

In python, you can convert the numbers from one type to another like int to float, float to int, int to complex, string to int, etc. using `int()`, `float()`, and `complex()` methods.

Following is the example of converting the numbers from one type to another in python.

a = 10 #int
b = 31.54 #float
c = 3j #complex
#convert from int to float
x = float(a)
#convert from float to int
y = int(b)
#convert from int to complex
z = complex(b)
print(x)
print(y)
print(z)
print("x type: ", type(x))
print("y type: ", type(y))
print("z type: ", type(z))

If you observe the above example, we convert the variable values from one type to another based on our requirements.

When you execute the above python program, you will get the result as shown below.

10.0
31
(31.54+0j)
x type: <class 'float'>
y type: <class 'int'>
z type: <class 'complex'>

If you observe the above result, we converted numbers from int to float, float to int, int to complex types.

Here, you need to remember that it’s impossible to convert complex numbers into another type in python.