Python Numbers

A numeric value is a value in the form of a number. Basically there are three types of numbers in python –

  • int
  • float
  • complex

 

A variable of numeric type is created as soon as you assign a numeric value to it. For eg

x = 1

int

‘int’ stands for integer values. Int is a whole number that can be positive, negative or zero. An integer is a number that
is without decimal. There is no limit of length of integers in python.

For example

x = 11

y = -145

print(type(x))

print(type(y))

Output

<class ‘int’>

<class ‘float’>

float

Float stands for ‘floating point number’, which can be a positive or negative number containing decimal.

For example

x = 1.4

y = 12.45

z = -41.67

print(type(x))

Output

<class ‘float’>

complex

A complex number is a number in which one part is a real part and the other part is imaginary.

Note : In mathematics, the imaginary part can be written by using any letter, but in python it should always be written as ‘j’.

For example

x = 4 + 5j

print(type(x))

Output

<class ‘complex’>

Type casting (type conversion) :

A number can be converted from one type to another. For that, there are some inbuit functions in python – int(),
float() and complex().

int() function: The int() function converts an argument into it’s equaivalent integer value.

float() function: The float() function converts an argument into it’s equaivalent float value.

complex() function: The complex() function converts an argument into it’s equaivalent complex value.

Converting from int to float

x = 1

a = float(x)

print(a)

print(type(a))

Output

1.0

<class ‘float’>

Converting from float to int

x = 1.4

a = int(x)

print(a)

print(type(a))

Output

1

<class ‘int’>

Converting from float to complex

x = 4.2

a = complex(x)

print(a)

print(type(a))

Output

1 + 0j

<class ‘complex’>

Note : An integer number or a float number can be converted to a complex number, but a complex number can not be  converted to int or float.