For example, R provides these native, simple data types:
- Character
- Numeric (real or decimal)
- Integer
- Logical
- Complex
Python provides these native, simple data types:
- Boolean
- Integer
- Float
- Complex
- String
anA = chr(65) print(type(anA))The output will be
<class 'str'>
, rather than <class 'char'>
, which is what most languages would provide. Consequently, a string is a scalar in Python but a vector in R. Keeping language differences in mind will help as you perform analysis on your data.Most languages also support what you might term as semi-native data types. For example, Python supports a Fraction
data type that you create by using code like this:
from fractions import Fraction x = Fraction(2, 3) print(x) print(type(x))The fact that you must import
Fraction
means that it’s not available all the time, as something like complex
or int
is. The tip-off that this is not a built-in class is the class output of <class 'fractions.Fraction'>
. However, you get Fraction
with your Python installation, which means that it’s actually a part of the language (hence, semi-native).
External libraries that define additional scalar data types are available for most languages. Access to these additional scalar types is important in some cases. Python provides access to just one data type in any particular category.
For example, if you need to create a variable that represents a number without a decimal portion, you use the integer data type. Using a generic designation like this is useful because it simplifies code and gives the developer a lot less to worry about.However, in scientific calculations, you often need better control over how data appears in memory, which means having more data types — something that numpy
provides for you.
For example, you might need to define a particular scalar as a short
(a value that is 16 bits long). Using numpy, you could define it as myShort = np.short(15)
. You could define a variable of precisely the same size using the np.int16
function. You can discover more about the scalars provided by the NumPy library for Python. You also find that most languages provide means of extending the native types (see the articles at Python.org and greenteapress.com for additional details).