In this unit we'll start learning about "collections", also commonly known as "Data Structures". We'll approach collections from a "utility" (abstract) standpoint: what are they used for?, When should we use one or the other?, etc. We won't focus on how they're implemented internally, as that's a much more complicated topic.
The main objective of Collections is to let us store many elements in the same "container". As simple as that. Think how you've been "storing elements" up to this point: probably only using variables. If I ask you to keep track of 3 users' emails, you'd do something like:
user1 = 'firstname.lastname@example.org' user2 = 'email@example.com' user3 = 'firstname.lastname@example.org'
Now, how do you add a new user dynamically (at run time)? You can't think of a new variable, as variables must be defined before running your script. The obvious solution for this is to use a collection. Let me show you an example and we'll then go into more detail about what's going on here.
users = ['email@example.com', 'firstname.lastname@example.org', 'email@example.com'] # new user is firstname.lastname@example.org users.append('email@example.com') print(users) # Prints: ['firstname.lastname@example.org', 'email@example.com', 'firstname.lastname@example.org', 'email@example.com']
users in our previous example is a list. A list is one type of collection that lets you store elements sequentially. It's arguably the most popular type of collection used in Python and we're going to talk in detail about them in our next lesson.