NUMBER TYPES IN PYTHON


In this tutorial, we are going to learn about number types in python, the three main number types are complex, decimal and complex. We will be going through each one of them with a handy example.


There are three main types of numbers in Python:

  • Integer
  • Decimal
  • Complex

These different numbers can be assigned to the variables and to verify the type of any object in Python, use the type() function:

x = 1 # int y = 2.8 # float z = 1j # complex print(type(x)) print(type(y)) print(type(z))

The difference between integers and float numbers is that a float number is separated by a decimal. So ’10’ is an integer and ‘10.5’ is a float number. Python gives us the freedom to store the integer, floating and complex numbers and also lets us do conversion operations between them.


Integers in Python

Python can hold signed integers.

a=7
print(a)

Output: 7

It can hold a value of any length, the only limitation being the amount of memory available.

a=9999999999999999999999999999999999999
print(a)

Output: 9999999999999999999999999999999999999

There are three types of int Python number types:

Number Types in Python

  • type() function

    It takes one argument, and returns which class it belongs to.

a=9999999999999999999999999999999999999 print(a) print(type(a))
  • isinstance()

It takes two parameters as the argument. So the isinstance() function checks whether or not the object is an instance or subclass of classinfo and returns True if it is, else it returns false.

Here, 2 is the real part, and 3j is the imaginary part. To denote the irrational part, however, you can’t use the letter ‘i’, like you would do on paper.





a=9999999999999999999999999999999999999 print(a) print(type(a)) print(isinstance(a,bool))

Since it belongs to the class ‘int’ instead, it returns False.

  • Exponential numbers

You can write an exponential number using the letter ‘e’ between the mantissa and the exponent.

print(2e5)

Remember that this is power of 10. To raise a number to another’s power, we use the ** operator.
If you face difficulty in Python number types, please comment.


Float in Python

Float, or “floating point number” is a number, positive or negative, containing one or more decimals.

from math import pi print(pi)

A float value is only accurate upto 15 decimal places. After that, it rounds the number off.


Complex Numbers in Python

Complex numbers are written with a “j” as the imaginary part:

a=2+3j print(a)

Coefficient to the imaginary part

Here, 2 is the real part, and 3j is the imaginary part. To denote the irrational part, however, you can’t use the letter ‘i’, like you would do on paper.

a=2+3i
print(a)

SyntaxError: invalid syntax

Also, it is mandatory to provide a coefficient to the imaginary part.

  • Operations on complex numbers

Finally, you can perform the basic operations on complex numbers too.

a=2+3j
b=2+5j
print(a+b)

Output:

(4+8j)




In this tutorial, we learnt about Python number types. We looked at int, float, and complex numbers in detail. You can try out your own combination and test accordingly.