Chapter 4. Understanding Data Types in Python
Effective data-driven science and computation requires understanding how data is stored and manipulated. This chapter outlines and contrasts how arrays of data are handled in the Python language itself, and how NumPy improves on this. Understanding this difference is fundamental to understanding much of the material throughout the rest of the book.
Users of Python are often drawn in by its ease of use, one piece of which is dynamic typing. While a statically typed language like C or Java requires each variable to be explicitly declared, a dynamically typed language like Python skips this specification. For example, in C you might specify a particular operation as follows:
/* C code */intresult=0;for(inti=0;i<100;i++){result+=i;}
While in Python the equivalent operation could be written this way:
# Python coderesult=0foriinrange(100):result+=i
Notice one main difference: in C, the data types of each variable are explicitly declared, while in Python the types are dynamically inferred. This means, for example, that we can assign any kind of data to any variable:
# Python codex=4x="four"
Here we’ve switched the contents of x from an integer to a
string. The same thing in C would lead (depending on compiler settings)
to a compilation error or other unintended consequences:
/* C code */intx=4;x="four";// FAILS
This sort of flexibility is one element that makes Python and other dynamically typed languages ...