Table of Contents » Chapter 2 : Data (Input) : Introduction to Data Structure (DS)
Introduction to Data Structures (DS)
Contents
- Overview
- Types of Data Structures
Overview
In programming, data structures are a way of organizing and storing data in a computer so that it can be accessed and modified efficiently. They define the physical form of the data (how it's stored in memory) and the set of operations that can be performed on it. Different data structures are suited to other kinds of applications, and the choice of a particular data structure can affect the efficiency of a program. The effective use of data structures can enhance the performance and scalability of software applications.
At an early stage of learning programming, variables and strings are the closest concepts you have learned so far to a data structure. These two concepts introduce the idea of storing data in memory while our programs run.
Variables, themselves, are not data structures. However, they are useful in understanding how we store data in memory. That is, as seen in Figure 1. we create a variable (label) and assign it a datum (value) using the assignment operator (=). The data value is stored in memory (Figure 2), and then we can access that data using the variable name. In code, it might look like this:
age = 25
print(age)

Using variables to store and access a datum is foundational to programming. We often need to store and access more than one datum, though, and when those data are related in some way, then we'll use a data structure. We create a variable name (label) to identify data structures as well; the difference between this and the example in Figure 1 is that through the variable that points to a data structure, we can access more than one datum. An example of this that we have already worked with is a string.
In Python, a string is a data structure. Specifically, it's a sequence data structure that maintains a sequence of elements in a specific order. In the case of a string, these elements are characters. As we saw on Page 2.2.12 Strings ↗, strings are a sequence of alphanumeric characters labeled with a variable name, as shown in Figure 3.

In this example, the variable full_name is the label for the sequence of nine characters shown. We can use indexing and string slicing, like full_nanme[2], is because the string is a data structure. Python handles storing the set of characters that make up this string so that we can access any part of it through the variable name. This concept of having access to more than one data element in a string will carry us to the following data structures we will explore: lists and dictionaries.
Lists in Python are ordered collections of items that can be of any type. Unlike strings, lists are mutable, meaning their contents can be changed after they are created. They can be thought of as dynamic arrays, but with the added benefit of being able to hold different types of data.
Dictionaries in Python are unordered collections of items. Each item in a dictionary has a key/value pair. Dictionaries are optimized to retrieve values when the key is known. They are mutable, which means they can be changed after they are created.
We will explore lists and dictionaries on the following pages.