# Python Number Types – Python Int, Float, Complex Numbers

## 1. Python Number Types

In our article on **Python Variables and Data Types**, we learnt about different data types supported by Python. Now, we will dig a little deeper into those Python number 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.

## 2. Python Number Types

Before starting with Python number types in, let us revise** Python basics** and various **syntax used in Python** for better understanding.

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.

## 3. Python int

Python can hold signed integers.

>>> a=7 >>> a

7

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

>>> a=9999999999999999999999999999999999999 >>> a

9999999999999999999999999999999999999

There are three types of this Python number types:

**i. type() function**

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

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

<class ‘int’>

**ii. 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)

False

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

**iii. 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.

If you face difficulty in Python number types, please comment.

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

3.141592653589793

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

1.1111111111111112

Note that division results in floats.

>>> 2/2

1.0

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

(2+3j)

**i. 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>

a=2+j

NameError: name ‘j’ is not defined

In this case, a coefficient of 1 will do.

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

(2+1j)

**ii. Operations on complex numbers**

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

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

(4+8j)

>>> a*=2 >>> a

(4+6j)

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

Any Doubt yet in Python number Type? Please Comment.

## 6. Writing numbers in binary, octal, and hexadecimal

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 |

### i. Binary

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)

7

You can also apply conversion functions on these numbers.

>>> int(0b10)

2

### ii. Octal

The prefix for octal is 0o or 0O.

>>> print(0O10)

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)

SyntaxError: invalid token

>>> float(0B10)

2.0

### iii. Hexadecimal

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

>>> print(0xFF)

255

>>> print(0xFE)

254

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

### i. int()

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

7

>>> int(7.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>

int(2+3j)

TypeError: can’t convert complex to int

>>> int(3j)

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)

2

>>> int(0xF)

15

### ii. float()

This function converts another numeric type into a float.

>>> float(110)

110.0

>>> float(110.0)

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>

float(3j)

TypeError: can’t convert complex to float

>>> float(0o10)

8.0

Here, we applied it to an octal number.

### iii. complex()

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

>>> complex(2)

(2+0j)

>>> complex(2.3)

(2.3+0j)

>>> complex(2+3.0j)

(2+3j)

### iv. bin()

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

>>> bin(2)

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

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)

Traceback (most recent call last):

File “<pyshell#50>”, line 1, in <module>

bin(2+3j)

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

### v. oct()

This function returns the octal value of a number.

>>> oct(8)

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

Traceback (most recent call last):

File “<pyshell#59>”, line 1, in <module>

oct(8.0)

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

### vi. hex()

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

>>> hex(255)

‘0xff’

>>> hex(0)

‘0x0’

>>> hex(0)

‘0x0’

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

False

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

>>> 1.1+2.2

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

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

0.1000000000000000055511151231257827021181583404541015625

Did you see what happened here? The Decimal() function preserved the significance.This was the Decimal Function Python number type.

## 9. The fractions Module

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

3/2

It can also take two arguments.

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

1/3

## 10. The math Module

Another essential module is the math module. It has all important mathematical functions like exp, trigonometric functions, logarithmic functions, factorial, and more.

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

120

>>> math.exp(3)

20.085536923187668

>>> math.tan(90)

-1.995200412208242

This was all about the Python number types tutorial.

## 11. Conclusion – Python Number Types

In this lesson, we learnt about Python number 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. We also looked at some important modules- decimal, fractions, math. Hope you like the Python Number Type Tutorial. Try your own combinations and leave your feedback on Python number types tutorial in the comments.

Refer **Best Books for Python** for in depth Python learning.

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

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!

True is 1, True is 0b1, True == 1, True == 0b1

(False, False, True, True)

why?

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

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

>>> a*=2

>>> a

(4+6j)

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

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

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

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

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?

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.

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 .

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.

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

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

I hope this will help you!!

Hello,

I have one question the question is that when i check that

a = 10

isinstance(a,bool)

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