Python - Datatype - Number Tutorial
Number datatype stored numeric values like int, float, long and complex.
The number data type is immutable, which means once the value is allocated, it cannot be changed later. Changing the value will result in the newly allocated object.
It is created when you assign a value to a variable.
Example-
var_a = 3
After assigning value to variable var_a, the number object var_a gets created.
You can also delete the single or multiple number objects at once using the del statement.
For single object-
del var_a
For multiple objects-
del var_a, var_b
Python support four different numeric datatype-
1] Int – It is a signed value. It is the whole number with no decimal point.
2] Float – It is a signed value. It is used for decimal values.
3] Long – It is a signed value. Its range is more than the integer value.
4] Complex – It is of the form x + yi, where x is a real number and y is an imaginary number.
We can use the type() function to know which class is a variable or value belong to.
We can also use the isinstance() function to check whether the variable belongs to a particular class or not.
Example-
var_a = 2
var_b = 2.0
var_c = 1 + 2j
print(type(var_a))
print(type(var_b))
print(type(var_c))
print(isinstance(var_c, complex))
print(isinstance(var_a, float))
Output-
<class 'int'>
<class 'float'>
<class 'complex'>
True
False
Generally, we use decimal numbers (base 10), but to represent the number into binary (0b), octal (0o), and hexadecimal (0x). We have to place the appropriate prefix in front of it.
Example-
# Binary number
print(0b0101010)
# Hexadecimal number
print(0xA8)
# Octal number
print(0o12)
Output –
42
168
10
Type Conversion
We can convert one variable class or datatype into another class or datatype using type conversion techniques.
We can convert any variable to integer using int(),float using float(), complex using complex().It can even convert string.
Example-
# int variable used with float variable will output as float
print(1 + 2.0)
# using int()
print(int(3.0))
# using float()
print(float(9))
print(float("65"))
# using complex()
print(complex(1))
print(complex("3+2j"))
Output-
3.0
3
9.0
65.0
(1+0j)
(3+2j)
Python Fraction
To get a fraction value from a floating number, we can import the fractions module, and can create a Fraction object.
Let's have a look-
Example-
import fractions
# fraction of 1.5 is 3/2
print(fractions.Fraction(1.5))
# fraction of 1,5 -comma-separated value is 1/5
print(fractions.Fraction(1,5))
# fraction of 1.1 is 2476979795053773/2251799813685248 - unexpected value
# Therefore we can user it in a string format
print(fractions.Fraction(1.1))
# fraction of '1.1' in string format is 11/10
print(fractions.Fraction('1.1'))
# Addition of fraction
print(fractions.Fraction('1.1') + fractions.Fraction(1,5))
Output-
3/2
1/5
2476979795053773/2251799813685248
11/10
13/10
For some floating value( e,g 1.1) we get unexpected value. In that case, we can use floating values in the string.
Mathematical Function
Python offers the following mathematics function to perform calculations easier. To call a mathematical function, we need to import a math module into some of the functions.
1] sqrt(a)
This function returns the square root of a, where a > 0. It needs a math module to be imported.
Example-
import math
print(math.sqrt(36))
print(math.sqrt(6))
print(math.sqrt(math.pi))
Output-
6.0
2.449489742783178
1.7724538509055159
2] abs(a)
This function will return the absolute positive value of a. where a can be any value, i.e it can be negative, long, float, etc.
It doesn’t need a math module to be imported.
Example-
print(abs(-35))
print(abs(3.14))
Output-
35
3.14
3] round( a [, n] )
This function will return value a rounded to n digits from the decimal point.
It doesn’t need a math module to be imported.
Example-
print(round(3.1432134332,3))
print(round(-4.2333,2))
Output-
3.143
-4.23
4] ceil(a)
This function will round the value of a to the next largest integer which is greater than a or equal to a. It needs a math module to be imported.
Example-
import math
print(math.ceil(7.03))
print(math.ceil(8.99))
print(math.ceil(-7.88))
print(math.ceil(math.pi))
Output-
8
9
-7
4
5] floor(a)
This function will round the value of a to the largest integer which is less than a or equal to a. It needs a math module to be imported.
Example-
import math
print(math.floor(7.03))
print(math.floor(8.99))
print(math.floor(-7.88))
print(math.floor(math.pi))
Output-
7
8
-8
3
6] pow(a,b)
This function will return a to the power of b. It needs a math module to be imported.
Example-
import math
print(math.pow(7,2))
print(math.pow(6,-3))
print(math.pow(4,0))
print(math.pow(-5,2))
Output-
49.0
0.004629629629629629
1.0
25.0
7] cmp(a,b)
This function will return the sign/indication value.
It will return 1 if a > b or a is greater than b
It will return 0 if a == b or a is equal to b
It will return -1 if a < b or a is less than b
Example-
print(cmp(7,2))
print(cmp(6,6))
print(cmp(0,4))
Output-
1
0
-1
8] min(a1, a2, …)
This function will return the smallest value of its arguments.
It doesn’t need a math module to be imported.
Example-
a = [1, 4.022, -3, 7, 99, -8.33] # List
b = (-3, 7, 9, 2.33) # Tuple
print(min(a))
print(min(b))
print(min(2,-3,9,-7))
Output-
-8.33
-3
-7
9] max(a1, a2, …)
This function will return the largest value of its arguments.
It doesn’t need a math module to be imported.
Example-
a = [1, 4.022, -3, 7, 99, -8.33] # List
b = (-3, 7, 9, 2.33) # Tuple
print(max(a))
print(max(b))
print(max(2,-3,9,-7))
Output-
99
9
9
10] log(a)
This function will return the log value of a.
It needs a math module to be imported.
Example-
import math
print(math.log(1))
print(math.log(10.23))
print(math.log(math.pi))
Output-
0.0
2.325324579963535
1.1447298858494002
11] log10(a)
This function will return the base 10 log value of a.
It needs a math module to be imported.
Example-
import math
print(math.log10(1))
print(math.log10(10.23))
print(math.log10(math.pi))
Output-
0.0
1.0098756337121602
0.4971498726941338
12] exp(a)
This function will return exponential value of a,i.e ex
It needs a math module to be imported.
Example-
import math
print(math.exp(1))
print(math.exp(10.23))
print(math.exp(math.pi))
Output-
2.718281828459045
27722.51006805505
23.140692632779267
13] modf(a)
This function will return the fractional and integer values of a in two items.
It needs a math module to be imported.
Example-
import math
print(math.modf(10.1232))
print(math.modf(1183.33))
print(math.modf(math.pi))
Output-
(0.12320000000000064, 10.0)
(0.32999999999992724, 1183.0)
(0.14159265358979312, 3.0)