Книга: A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1
Назад: Chapter 1: Why Data Structures Matter
Дальше: The Array: The Foundational Data Structure

Data Structures

Let’s talk about data.

Data is a broad term that refers to all types of information, down to the most basic numbers and strings. In the simple but classic “Hello World!” program, the string "Hello World!" is a piece of data. In fact, even the most complex pieces of data usually break down into a bunch of numbers and strings.

Data structures refer to how data is organized. You’re going to learn how the same data can be organized in a variety of ways.

Let’s look at the following code:

 x = "Hello! "
 y = "How are you "
 z = "today?"
 
 print(x + y + z)

This simple program deals with three pieces of data, outputting three strings to make one coherent message. If we were to describe how the data is organized in this program, we’d say that we have three independent strings, each contained within a single variable.

However, this same data can also be stored in an array:

 array = ["Hello! ", "How are you ", "today?"]
 
 print(array[0] + array[1] + array[2])

You’re going to learn in this book that the organization of data doesn’t just matter for organization’s sake, but can significantly impact how fast your code runs. Depending on how you choose to organize your data, your program may run faster or slower by orders of magnitude. And if you’re building a program that needs to deal with lots of data, or a web app used by thousands of people simultaneously, the data structures you select may affect whether your software runs at all or simply conks out because it can’t handle the load.

When you have a solid grasp on data structures’ performance implications on the software you’re creating, you’ll have the keys to write fast and elegant code, and your expertise as a software engineer will be greatly enhanced.

In this chapter, we’re going to begin our analysis of two data structures: arrays and sets. While the two data structures may seem almost identical, you’re going to learn the tools to analyze the performance implications of each choice.

Назад: Chapter 1: Why Data Structures Matter
Дальше: The Array: The Foundational Data Structure