# 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 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```

Output

7

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

```>>> a=9999999999999999999999999999999999999
>>> a```

Output

9999999999999999999999999999999999999

There are three int types of Python number types:

Python Number Types – Python Int

#### 1. Python type() function

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

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

Output

<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)`

Output

False

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)`

200000.0

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```

Output

3.141592653589793
`>>> type(pi)`

Output

<class ‘float’>

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

```>>> a=1.1111111111111111119
>>> a```

Output

1.1111111111111112

Note that division results in floats.

`>>> 2/2`

Output

1.0

### 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```

Output

(2+3j)

#### 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`

Output

SyntaxError: invalid syntax

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

`>>> a=2+j`

Output

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

a=2+j

NameError: name ‘j’ is not defined

In this case, a coefficient of 1 will do.

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

Output

(2+1j)

#### 2. Operations on complex numbers

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

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

Output

(4+8j)
```>>> a*=2
>>> a```

Output

(4+6j)

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)`

Output

7

You can also apply conversion functions on these numbers.

`>>> int(0b10)`

Output

2

#### 2. Octal Numbers in Python

The prefix for octal is 0o or 0O.

`>>> print(0O10)`

Output

8

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)`

Output

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

Output

2.0

#### 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)`

Output

255
`>>> print(0xFE)`

Output

254

### 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 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)`

Output

7
`>>> int(7.7)`

Output

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)`

Output

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

int(2+3j)

TypeError: can’t convert complex to int

`>>> int(3j)`

Output

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

int(3j)

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)`

Output

2
`>>> int(0xF)`

Output

15

#### 2. float() in Python

This function converts another numeric type into a float.

`>>> float(110)`

Output

110.0
`>>> float(110.0)`

Output

110.0

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

`>>> float(3j)`

Output

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

float(3j)

TypeError: can’t convert complex to float

`>>> float(0o10)`

Output

8.0

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)`

Output

(2+0j)
`>>> complex(2.3)`

Output

(2.3+0j)
```>>> complex(2+3.0j)

```

Output

(2+3j)

#### 4. bin() in Python

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

`>>> bin(2)`

Output

‘0b10’

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)`

Output

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

bin(2.3)

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

`>>> bin(2+3j)`

Output

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

bin(2+3j)

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

#### 5. oct() in Python

This function returns the octal value of a number.

`>>> oct(8)`

Output

‘0o10’

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)`

Output

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

oct(8.0)

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)`

Output

‘0xff’
`>>> hex(0)`

Output

‘0x0’
`>>> hex(0)`

Output

‘0x0’

### 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`

Output

False

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

`>>> 1.1+2.2`

Output

3.3000000000000003

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`

Output

0.3333333333333333

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))```

Output

0.1000000000000000055511151231257827021181583404541015625

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))```

Output

3/2

It can also take two arguments.

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

Output

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)```

Output

120
`>>> math.exp(3)`

Output

20.085536923187668
`>>> math.tan(90)`

Output

-1.995200412208242

This was all about the Python number types tutorial.

## Conclusion

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.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google | Facebook

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

2. Damodar says:

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

why?

• 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
(4+6j)

• 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.
Regards,
DataFlair

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
>>>decimal.getcontext().prec=3
>>> 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:
Decimal(‘16.3’)

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

• grigorie_p says:

And I had the same problem and still haven’t found an answer.

• Kusuma says:

Hello me too having this problem and couldn’t find the answer.Please tell me if you know.

5. Durga says:

Hi Team,

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

Ex:

a=2+3j
b=3-1j
c=a+b
print(c)

o/p = (5+2j)

whether we can assign value to imaginary value and print output
eg: assign j value as 2
j=2
c=(5+2*2)
c=10
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:
x=10
x+=20

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