Data Structures in Python#

Python data structures

Up to this point, we’ve been working with what are called “primitive” data types in Python. These include integer, float, string, and Boolean.

But we can imagine scenarios where we want to be able to work with more complex data structures or more complex ways of storing and accessing information in a programming language.

In this chapter, we’re going to focus on some of the one-dimensional (or linear) data structures available to us in Python. These include strings, lists, tuples, sets, and dictionaries.

NameSyntaxExampleDescription
Stringstr(), """Hello world!"Sequence of characters
Listlist(), []["apple", "banana", "pear"], [1, 3, 5, 7]Sequence of objects/values
Dictionarydict(), {}{'first_name': 'Knute', 'last_name':'Rockne', 'class':'1918'}Key-value pairs
Setset(), {}{"apple", "banana", "pear"}, {1, 3, 5, 7}Unordered group of unique values
Tupletuple(), ()("apple", "banana", "pear"), (1, 3, 5, 7)Ordered group of values that can include duplicates

We can use a few key questions or attributes to distinguish or compare these data structures:

  • Mutability: Can values in the structure be changed once it has been created or assigned to a variable?

  • Order: Does the order of values in the structure have meaning/significance, or is order not significant?

  • Indexing/Slicing: Can values in the structure be accessed using their position or index? Can we isolate values in the structure using their position?

  • Duplicates: Does the structure allow duplicate values?

This chapter provides an overview of foundational programming concepts in the areas of data structures and data storage, with a focus on Python syntax.

Topics covered include:

  • Linear and associative arrays

  • Indexing

  • String objects and methods

  • Lists and list methods

  • Dictionaries

  • Tuples

  • Sets

Acknowledgements#

Elements of this chapter were adapted from materials developed by:

Chapter Table of Contents#

Application#

Your answers to this chapter’s application questions should be added to the notebook template.

Submit the Colab link on Canvas for the assignment submission.