Python for Finance: Analyze Big Financial Data

(Elle) #1

Chapter 4. Data Types and Structures


Bad programmers worry about the code. Good programmers worry about data structures and their relationships.

— Linus Torvalds

This chapter introduces basic data types and data structures of Python. Although the


Python interpreter itself already brings a rich variety of data structures with it, NumPy and


other libraries add to these in a valuable fashion.


The chapter is organized as follows:


Basic data types


The first section introduces basic data types such as int, float, and string.


Basic data structures


The next section introduces the fundamental data structures of Python (e.g., list


objects) and illustrates control structures, functional programming paradigms, and


anonymous functions.


NumPy data structures


The following section is devoted to the characteristics and capabilities of the NumPy


ndarray class and illustrates some of the benefits of this class for scientific and


financial applications.


Vectorization of code


As the final section illustrates, thanks to NumPy’s array class vectorized code is easily


implemented, leading to more compact and also better-performing code.


The spirit of this chapter is to provide a general introduction to Python specifics when it


comes to data types and structures. If you are equipped with a background from another


programing language, say C or Matlab, you should be able to easily grasp the differences


that Python usage might bring along. The topics introduced here are all important and


fundamental for the chapters to come.

Free download pdf