Site icon DataFlair

Python Numeric Data Types – Int, Float, Complex

Python course with 57 real-time projects - Learn Python

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.

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.

Exit mobile version