





















































This article by Benjamin Baka, author of the book Python Data Structures and Algorithm, explains the inbuilt data types in Python. Python data types can be divided into 3 categories, numeric, sequence and mapping. There is also the None object that represents a Null, or absence of a value. It should not be forgotten either that other objects such as classes, files and exceptions can also properly be considered types, however they will not be considered here.
(For more resources related to this topic, see here.)
Every value in Python has a data type. Unlike many programming languages, in Python you do not need to explicitly declare the type of a variable. Python keeps track of object types internally.
Python inbuilt data types are outlined in the following table:
Category |
Name |
Description |
None |
None |
The null object |
Numeric |
int |
Integer |
|
float |
Floating point number |
|
complex |
Complex number |
|
bool |
Boolean (True, False) |
Sequences |
str |
String of characters |
|
list |
List of arbitrary objects |
|
Tuple |
Group of arbitrary items |
|
range |
Creates a range of integers. |
Mapping |
dict |
Dictionary of key – value pairs |
|
set |
Mutable, unordered collection of unique items |
|
frozenset |
Immutable set |
The None type is immutable and has one value, None. It is used to represent the absence of a value. It is returned by objects that do not explicitly return a value and evaluates to False in Boolean expressions. It is often used as the default value in optional arguments to allow the function to detect if the caller has passed a value.
All numeric types, apart from bool, are signed and they are all immutable. Booleans have two possible values, True and False. These values are mapped to 1 and 0 respectively. The integer type, int, represents whole numbers of unlimited range. Floating point numbers are represented by the native double precision floating point representation of the machine. Complex numbers are represented by two floating point numbers. they are assigned using the j operator to signify the imaginary part of the complex number. For example : a = 2+3j
We can access the real and imaginary parts by a.real and a.imag respectively.
It should be noted that the native double precision representation of floating point numbers leads to some unexpected results. For example, consider the following:
In[14]: 1-0.9
Out[14]: 0.09999999999998
In [15]: 1-0.9 == 0.1
Out[15]: False
This is a result of the fact that most decimal fractions are not exactly representable as a binary fraction, which is how most underlying hardware represents floating point numbers. For algorithms or applications where this may be an issue Python provides a decimalmodule. This module allows for the exact representation of decimal numbers and facilitates greater control properties such as rounding behaviour, number of significant digits and precision. It defines two objects, a Decimal type, representing decimal numbers and a Context type, representing various computational parameters such as precision, rounding and error handling. An example of its usage can be seen in the following:
In [1]: import decimal
In[2]: x = decimal.Decimal(3.14); y=decimal.Decimal(2.74)
In[3]: x*y
Out[3]: Decimal (‘8.60360000000001010036498883’)
In[4]: decimal.getcontext().prec = 4
In[5]: x * y
Out[5]: Decimal(‘8.604’)
Here we have created a global context and set the precision to 4. The Decimal object can be treated pretty much as you would treat an int or a float. They are subject to all the same mathematical operations and can be used as dictionary keys, placed in sets and so on. In addition, Decimal objects also have several methods for mathematical operations such as natural exponents x.exp(), natural logarithms, x.ln() and base 10 logarithms, x.log10().
Python also has a fractions module that implements a rational number type. The following shows several ways to create fractions:
In [62]: import fractions
In [63]: fractions Fraction(3,4) #creates the fraction ¾
Out[63]: Fraction(3,4)
In [64]: fraction Fraction(0,5) #creates a fraction from a float
Out[64]: Fraction(1,2)
In [65]: fraction Fraction(“.25”) #creates a fraction from a string
Out[65]: Fraction(1,4)
It is also worth mentioning here the NumPy extension. This has types for mathematical objects such as arrays, vectors and matrixes and capabilities for linear algebra, calculation of Fourier transforms, eigenvectors, logical operations and much more.
We have looked at the built in data types and some internal Python modules, most notable the collections module. There are a number of external libraries such as the SciPy stack, and, likewise.
Further resources on this subject: