Python Numeric Data Types – Int, Float, Complex

Free Python course with 25 real-time projects Start Now!!

Let us dig into Python numeric data types.

As we have seen, a python number can be- Python int, Python float, or even Python complex number.

Long is no longer supported by Python 3.x.

So, let’s begin with the python number types tutorial.

Python Number Types - Python Int, Float, Complex Numbers

Python Number Types – Python Int, Float, Complex Numbers

Python Numeric Data Types

A number is an arithmetic entity that lets us measure something.

Python allows us to store the integer, floating, and complex numbers and also lets us convert between them.

Since Python is dynamically-typed, there is no need to specify the type of data for a variable.

So now let’s start with python number types.

  • None- The None keyword indicates the absence of value.

Python int

Python can hold signed integers.

>>> a=7
>>> a



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

>>> a=9999999999999999999999999999999999999
>>> a



There are three int types of Python number types:

Python Number Types - Python Int, Float, Complete Numbers

Python Number Types – Python Int

1. Python type() function

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

>>> a=9999999999999999999999999999999999999
>>> type(a)


<class ‘int’>

2. Python isinstance() function

It takes two arguments. The first is the construct(ex- a variable or a list), and the second is a class.

It returns True or False based on whether the construct belongs to that class.

Suppose we want to check if ‘a’ belongs to class bool. We write the following code for the same.

>>> isinstance(a,bool)



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

3. Python 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.

Python float

Python also supports floating-point real values. An int cannot store the value of the mathematical constant pi, but a float can.

>>> from math import pi
>>> pi


>>> type(pi)


<class ‘float’>

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

>>> a=1.1111111111111111119
>>> a



Note that division results in floats.

>>> 2/2



Python Complex Numbers

A complex number is a Python number type made of real and imaginary parts. It is represented as a+bj.

>>> a=2+3j
>>> a



1. 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


SyntaxError: invalid syntax

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

>>> a=2+j


Traceback (most recent call last):File “<pyshell#33>”, line 1, in <module>


NameError: name ‘j’ is not defined

In this case, a coefficient of 1 will do.

>>> a=2+1j
>>> a



2. Operations on complex numbers

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

>>> a=2+3j
>>> b=2+5j
>>> a+b


>>> a*=2
>>> a



Here, *= is an in-place assignment operator.

Any Doubt yet in Python number Type? Please Comment.

Writing numbers in binary, octal, and hexadecimal in Python

More often than not, programmers need to deal with numbers other than decimal. To do this, you can use appropriate prefixes.

Number System Prefix
Binary 0b or 0B
Octal 0o or 0O
Hexadecimal 0x or 0X

1. Binary Numbers in Python

When you want to write a binary number, use the prefix 0b or 0B. For example, we know that the binary for 7 is 111.

>>> print(0b111)



You can also apply conversion functions on these numbers.

>>> int(0b10)



2. Octal Numbers in Python

The prefix for octal is 0o or 0O.

>>> print(0O10)



The following code causes an error. This is because the octal number system does not have the number 8. It has the numbers 0-7.

>>> print(0O8)


SyntaxError: invalid token
>>> float(0B10)



3. Hexadecimal Numbers in Python

The hexadecimal number system has numbers 0-9 and then A-F. For that, use the prefix 0x or 0X.

>>> print(0xFF)


>>> print(0xFE)



Python Conversion Functions

Although most times Python does the conversion as needed, you can do it explicitly if you want.

These functions allow us to convert one numeric type into another python numeric data types.

Python Number Types - Python Int, Float, Complete Numbers

Python Number Types – Python Conversion Functions

1. int() in Python

Python int() function can convert another numeric type into an int.

It can also convert other types into an int, but in this tutorial, we focus on numeric types.

>>> int(7)


>>> int(7.7)



As you can see, it does not round the number 7.7 up to 8; it truncates the 0.7.

However, you cannot convert a complex number into an int.

>>> int(2+3j)


Traceback (most recent call last):File “<pyshell#22>”, line 1, in <module>


TypeError: can’t convert complex to int

>>> int(3j)


Traceback (most recent call last):File “<pyshell#23>”, line 1, in <module>


TypeError: can’t convert complex to int

We can also apply this function on representations other than decimal, i.e., binary, octal, and hexadecimal.

>>> int(0b10)


>>> int(0xF)



2. float() in Python

This function converts another numeric type into a float.

>>> float(110)


>>> float(110.0)



Like int(), float() can’t convert a complex either.

>>> float(3j)


Traceback (most recent call last):File “<pyshell#26>”, line 1, in <module>


TypeError: can’t convert complex to float

>>> float(0o10)



Here, we applied it to an octal number.

3. complex() in Python

The complex() function converts another numeric type into a complex number.

>>> complex(2)


>>> complex(2.3)


>>> complex(2+3.0j)



4. bin() in Python

The bin() function returns the binary value of a number.

>>> bin(2)



However, you can’t apply it to a float value or a complex value. The same is true for oct() and hex() functions too.

>>> bin(2.3)


Traceback (most recent call last):File “<pyshell#49>”, line 1, in <module>


TypeError: ‘float’ object cannot be interpreted as an integer

>>> bin(2+3j)


Traceback (most recent call last):File “<pyshell#50>”, line 1, in <module>


TypeError: ‘complex’ object cannot be interpreted as an integer

5. oct() in Python

This function returns the octal value of a number.

>>> oct(8)



We know that 8.0 is the same as 8, but the function doesn’t think the same. It is a float, so it cannot convert it into an oct.

>>> oct(8.0)


Traceback (most recent call last):File “<pyshell#59>”, line 1, in <module>


TypeError: ‘float’ object cannot be interpreted as an integer

6. hex() in Python

The hex() function returns the hexadecimal value of a number.

>>> hex(255)


>>> hex(0)


>>> hex(0)



Python Decimal Module

Let’s try out adding 1.1 and 2.2 in the shell, and let’s compare it with 3.3.

>>> (1.1+2.2)==3.3



Why did it return False? Let’s try printing the sum.

>>> 1.1+2.2



Woah, how did this happen? Well, this is duly attributed to hardware limitations, and is not a flaw of Python.

Because the hardware stores decimals as binary fractions, it isn’t possible to store it very accurately.

Let’s take an example.

>>> 1/3



When we divide 1 by 3, it doesn’t return the full value, which is 0.3333333333333333… Python does provide a solution to this problem.

It has the ‘decimal’ module, which lets us choose precision. We will learn about modules in a later lesson.

>>> import decimal
>>> print(decimal.Decimal(0.1))



Did you see what happened here? The Decimal() function preserved the significance.

This was the Decimal Function Python number type.

Fractions Module in Python

Another module that Python provides, the fractions module lets you deal with fractions.

The Fraction() function returns the value in the form of numerator and denominator.

>>> from fractions import Fraction
>>> print(Fraction(1.5))



It can also take two arguments.

>>> print(Fraction(1,3))



Math Module in Python

Another essential module in Python is the math module.

It has all important mathematical functions like exp, trigonometric functions, logarithmic functions, factorial, and more.

>>> import math
>>> math.factorial(5)


>>> math.exp(3)


>>> math.tan(90)



This was all about the Python number types tutorial.


In this lesson, we learnt about Python numeric data types.

We looked at int, float, and complex numbers. We also looked at how to write numbers in binary, octal, and hexadecimal representations.

Then we looked at how to convert one numeric type into another in Python.

We also looked at some important modules- decimal, fractions, math. Hope you like the Python Number Type Tutorial.

16 Responses

  1. Victor Chijioke Onyesoh says:

    The tutorial is very engaging. I was actually following the tutorial on python 3 environment from another window.

    • Data Flair says:

      Hii Victor
      It seems you are interested in learning Python further. We have more detailed published articles, you can refer our left sidebar. Start with the first to the last tutorial and you will be ready to face the race.
      Hope, it will help you!

  2. Damodar says:

    True is 1, True is 0b1, True == 1, True == 0b1
    (False, False, True, True)


    • DataFlair Team says:

      Hi Damodar,
      Thanks for asking the query
      Python returns so because == checks for two values to be equal, while is checks for them to be identical.
      0b1 is binary for 1. Hence, True==1 and True==0b1 return True, while True is 1 and True is 0b1 return False.
      Hope the confusion is clear to you.
      Keep learning

  3. Lalit Siraswa says:

    I dont understand why a have the value (4+6j ) after performing a*=2
    >>> a*=2
    >>> a

    • DataFlair Team says:

      Hi Lalit

      We are glad, you ask a query on Python Number Type Conversion tutorial. In our example, we take a to be 2+3j, which is effectively a complex number with 2 as the real part and 3 as the coefficient in the imaginary part.
      When we perform a*=2, we multiply it by 2. This means we multiply both parts of the number by 2.

      (2+3j)*2 = 2*2 + 3j*2
      = 4 + 6j
      Hope, it will solve your query.

  4. JYOTI SHARMA says:

    It says that decimal module helps to choose precision
    >>> import decimal
    >>> print(decimal.Decimal(0.1)). what is the role of Decimal module in above command when precision is not obtained?

    Please clear my confusion about the given two codes regarding the decimal module:
    >>> import decimal
    >>> print(decimal.Decimal(0.1))

    output is : 0.1000000000000000055511151231257827021181583404541015625

    >>> import decimal
    >>> decimal.getcontext().prec = 3
    >>> decimal.Decimal(7.6) + decimal.Decimal(8.7)

    output is:

    Why do have a difference as the precision is defined in both thescases

  5. Durga says:

    Hi Team,

    I would like to appreciate for clear and well explanation.
    I have an doubt in Operation in Complex numbers.



    o/p = (5+2j)

    whether we can assign value to imaginary value and print output
    eg: assign j value as 2
    is it possible in operation of complex numbers?

    • DataFlair Team says:

      Hi Durga

      j is a mathematical complex coefficient. It is not a variable and should not be assigned values.

      If you’d still like to do it though, you can try toggling the complex number between the string and complex form to extract all the values and then calculate a result from multiplication.
      Hope, it helps.

  6. Mukesh Goswami says:

    your tutorial is very awesome, however i’m struggling to follow the sequence of series. I think this should be better aligned . for e.g number types ,should come after variable and data types, moreover you are talking about variable type in this topic which is coming after this topic, so not sure how this is planned . Also in single topic you cover all concepts, later on you have different discussion on those topics. getting tougher for me to follow. Please help me in understanding how the topics are sequenced .

    • Kusuma says:

      hey I’m also suffering with this. I don’t which module is first the sequence is little bit confused. some what Iam not understanding the concepts which are introduced in between the modules.If you find the solution please tell me.

  7. Nisha says:

    What is the difference between in-place assignment and assignment operator?

    • DataFlair Team says:

      Hey Nisha,

      Here is the answer to your question.

      In the in-place operator we perform the operation and assign the variable in the same step
      for example:

      The assignment operator is using the single ‘=’ sign to store values
      x = x+20

      I hope this will help you!!

  8. Manish Daga says:

    I have one question the question is that when i check that
    a = 10
    is show false
    but when i use int instance of bool it shows me an error that
    TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.