Lab 04: Sequences Part 1: Lists and Tuples¶
Let's start diving into some data structures, specifically sequences! We have dabbled with strings so far, and that is one example of a sequence (or iterable). In the next two labs, we will be exploring more types of sequence data structures. This one will focus more on Lists and Tuples - we will be building upon what we already know about strings and how to manipulate them here.
Getting Started¶
Like with all data types in Python, everything is considered an object (again, we will get to this in a future lab). To put shortly, what this means here (at least for now) is that a lot of the data types we have in Python have what we call properties (i.e., some collection of variables) and methods (i.e., basically functions, but there's a reason why we use the term "method" instead here).
I could detail a lot more here, but I think other resources out there have a more exhaustive detailing of each sequence/iterable data structure we will be exploring here.
Recap: Strings¶
Before we get into the meat and potatoes (i.e., talk about lists and tuples), let's revisit strings first. What is covered here is mostly translatable to the workings behind Python's lists and tuples, save for some differences here and there.
Recall that a string essentially means a string of characters, often surrounded by single (') or double (").
More specifically, we can define strings as a sequence of characters.
Lists¶
A list is a data structure that behaves like a string, but they are not just a collection characters. With lists, you can create a list of values of varying data types and group them as one data structure. This is similar to array data structures you may find in other programming languages like C or Java, but those are often fixed in size/length, and they can only contain items of the same data type at declaration. That restriction does not exist for Python's lists, hence you can infer that they are more dynamic.
If all that has confused you, think of a shopping list. A useful shopping list contains items that you would need or want to get from a shop/supermarket/etc. In essence, Python's list works like that - it's a collection of values.
What is a data structure?
A data structure
A list is denoted using square parentheses [ and ].
Tuples¶
A tuple is another data structure that is quite similar to lists, but with a major difference: tuples are immutable. What this means is that when a tuple data structure is declared,
Lab Activity 01: List and Tuple Methods¶
What was detailed so far was enough to understand what they are. In this lab activity, we will explore what methods are available to them.
Lab Activity 02: A Simple Counter Queueing System¶
Let's create something simple with one queue system.