In this Python article, we will discuss the different numeric data types (i.e. integers, float, and complex numbers) available in python.
We will discuss various operations that can be performed on them along with some easy examples.
Table of Contents
1. Different Numeric datatype
In python, we have mainly three types of numbers.
Integers numbers int class
Float numbers float class
Complex numbers complex class
Here are some examples of numbers
Examples of different Numeric types in Python
We normally use the decimal numbers or (base 10) number system in our daily lives. But in computer programming, we often have to deal with binary (base 2), hexadecimal (base 16), and octal (base 8) number systems.
Prefix in Python
‘0b’ or ‘0B’
‘0o’ or ‘0O’
‘0x’ or ‘0X’
Binary, hexadecimal and Octal
Binary – It is a number with syntax containing 0b and eight digits in the 0 and 1 combination representing the binary numbers.
Octal – It is a number with syntax containing 0o as prefix.
Hexadecimal – It is a number with syntax containing 0x as prefix.
Lets consider an example to understand how it works.
print(0xFC + 0b11)
Only in Python 2: There is one more datatype associated with long(long integers). It is used when the size of integers is never-ending or unlimited. We just write the integer value and lowercase or uppercase L at the end of the value.
1.1. Integer(int) datatype in Python
int or Integer numbers are those positive or negative numbers that do not have a decimal or these are the number with no fraction value. int in python is the signed integer.
The number which is lead by zeros in an integer value that is non-zero gives an error and is treated invalid. Example: 000123 is an invalid number.
>>> number = 09456
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
Normally, we are taught to write 1000 as 1,000 or 10000 as 10,000. But, we cannot write numbers with delimiter as python does not allow comma as number delimiter instead it allows underscore (_) as a delimiter.
Note: Using a comma(,) will convert it into tuple.
>>> number = 12_34_56_78_90
>>> number = 9,456
We can also convert any given string or float value to an integer with the help of int() function. We should be careful and sure that the another data type represents a number only.
>>> int("here it will fail")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'here it will fail'
1.2. Float datatype in Python
float numbers are those positive or negative number which has a decimal.
1.0, 4.7 ,3.9
Same like int datatype, float can also use delimiter (_) instead of comma.
>>> f = 12_12.245_059
Float datatype has maximum size according to the individual system. If it reaches beyond its maximum size then we refer to it as ‘inf’, ‘Inf’, ‘INFINITY’, or ‘infinity’. Float 2e400 may be considered as infinity for most systems.
We can also convert any given string or int value to a float with the help of float() function.
Some calculations performed by class float in Python may put us in surprise. If we try to make the sum of 2.2 and 4.4, we all know that the sum should be 6.6, but python may not agree.
if (2.2 + 4.4) == 6.6:
So, what is happening here? The reason is that float numbers are converted in computer hardware as binary fractions because computers only understand binary forms (0 & 1). Hence, it is computer hardware’s limitation and not an error.
Let’s take an example. As we know that the fraction 2/3 will give 0.66666… which is infinitely long and never ending so we take the approximate value.
Hence, the decimal fraction 0.1 will be an infinitely long binary fraction of 0.00011001100110011… and our computer will store a finite number of it. Which will be approximate 0.1 but it will never be equal.
We can use the decimal module to overcome such issue. The decimal module has user-settable precision unlike the float numbers that have precision up to 15 decimal places.
As we know about imperfect binary float number, we may get odd outcomes while creating Fraction from float. So we can do in this scenario is that we should use a string fraction as it is also supported by Fraction.
from fractions import Fraction as f
5. Useful Arithmetic Operators
The following table list arithmetic operators on integer values:
To add two values
To subtract right value from left value
To multiply two numbers
To divide left value by right value(float result is obtained)
To Return the remainder of the division of the left value by the right value
To calculate the value of the left-operand raised to the right-operand.