Data Structures in Python#
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.
| Name | Syntax | Example | Description |
|---|---|---|---|
| String | str(), "" | "Hello world!" | Sequence of characters |
| List | list(), [] | ["apple", "banana", "pear"], [1, 3, 5, 7] | Sequence of objects/values |
| Dictionary | dict(), {} | {'first_name': 'Knute', 'last_name':'Rockne', 'class':'1918'} | Key-value pairs |
| Set | set(), {} | {"apple", "banana", "pear"}, {1, 3, 5, 7} | Unordered group of unique values |
| Tuple | tuple(), () | ("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#
- Indexing
- Strings
- Lists
- Dictionaries
- Putting It All Together
- Application
- For these last two questions, you can use any of the data structures and arguments/syntax presented in this lab. As part of your answer, include a brief explanation for why you choose a particular data structure for the task.
- Collaborative Activities
Application#
Your answers to this chapter’s application questions should be added to the notebook template.
Google Colab link (ND users only)
Submit the Colab link on Canvas for the assignment submission.