Python Numbers, Type Casting and Random Numbers

A number is classified into three numerical types in Python. These are int, float, and complex.  These data types are used to store numeric values. They are immutable data types available in Python. In other words, it is treated as a newly allocated object when we change the value of a number data type. These objects are formed when the programmer assigns it to the code. Examples are given below,

a1 = 5

a2 = 10

Numerical Types and Type Function

These three numeric types are widely used in the python programming language. It is an important part of Python. We can check the type of the variable by using the type() function. We have to pass the variable name as an argument of the type function and print it. 

Example:

a = 10    # It is integer

b = 5.6  # It is float

c = 5j   # It is complex

print(type(a))

print(type(b))

print(type(c))

Del Statement

By the use of the del statement, we can delete the reference to a number object. This statement is used for both the single object and multiple objects. 

Example:

del a

del b, c

Integer

These types are frequently known as integers or ints. These are only positive or negative whole numbers. It has no decimal point and unlimited length.

Example:

a = 2

b = 35421036975887711

c = -3258877

print(type(a))

print(type(b))

print(type(c))

#all are integer

Float

It is called Float or floating-point number. It is simply a number that can be positive or negative numbers. It contains one or more decimals that divides the integer and fractional parts. Floats can be represented as scientific notation. It can hold E or e that indicates the power of 10.

Example:

a = 2.30

b = 10.0

c = -32.66

d=35e5

e=-7.1E10

print(type(a))

print(type(b))

print(type(c))

print(type(d))

print(type(e))

#all are floats

Complex

These types of numbers are known as complex numbers. It is represented as a form of x + yj. Here x and y are floating-point numbers and j demonstrates the square root of -1. This j is an imaginary number. In this equation, the real part is x and y is the imaginary part. These have not many uses in Python programming. But this is the unique feature that this language has. 

Example:

a = 5+9j

b = 9j

c = -2j

print(type(a))

print(type(b))

print(type(c))

#these are complex numbers

Some number of examples are given below,

Integer Float Complex
100 10.0 3.6j
5 15.88 41j
-87 -2.6 9.64e-14j
051 34.98+e8 .58j
-049 -50 -652.33+0J
-0*540 -55.9e12 6e+9J
0*78 78.99-E7 7.85E-17J

Type Casting in Python

This is an important feature and always useable concept in Python. Even all programming language has this concept. For Python, we can convert from one type to another for all the three numeric types such as int, float, and complex. We have to type int(a), long(a), float(x), and complex(x) to convert a to a plain integer, long integer, floating-point number, and complex number respectively. 

Example:

a = 12    # integer type

b = 12.8  # floating type

c = 15j   # complex type

#convert from int to float

x = float(a)

#convert from float to int

y = int(b)

#convert from int to complex

z = complex(c)

print(x)

print(y)

print(z)

print(type(x))

print(type(y))

print(type(z))

Random Number

We can create random numbers in Python. But it does not have a random() function. To make random numbers, the Random module is used. It is a built-in module in Python.

Example:

import random

print(random.randrange(1, 100))

#this will print any random number between 1 to 100