Introduction To Data Structures In Python - (Lists, Tuples, Sets, Dictionaries) for Beginners | Naser Jamal | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Introduction To Data Structures In Python - (Lists, Tuples, Sets, Dictionaries) for Beginners

teacher avatar Naser Jamal, Computer Engineer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Introduction

      1:10

    • 2.

      Lists in Python 3

      5:47

    • 3.

      List Methods

      7:02

    • 4.

      2D Lists & Nested Loops

      2:11

    • 5.

      Tuples

      3:34

    • 6.

      Sets & Mathematical Operations

      5:29

    • 7.

      Dictionaries

      5:09

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

87

Students

--

Projects

About This Class

In this Python Class, we'll be learning all about Data Structures in Python.

Data Structures are not as hard to master as it seems at first, so buckle up for a 100% hands-on learning journey!

What you'll learn

  • Understand the Fundamentals of Data Structures in Python.
  • Learn, implement and use different Data Structures.

Why would you take this class?

  • Do you wanna be able to structure your code, and make it more reusable?
  • Do you want to Strengthen your Skills as a Developer without spending too much time?
  • Have you ever been confused by the Data Structures topic and wanted a detailed step-by-step learning guide?

 

Content & Overview

With high-quality video lectures, you will learn and work with these concepts:

  • Lists in Python 3
  • List Methods
  • 2D Lists & Nested Loops
  • Tuples
  • Sets & Mathematical Operations
  • Dictionaries
  • .... and more!

Requirements

  • No programming knowledge is required.
  • No need to Install python, you could use Online Python Compilers.
  • Windows / Linux / MacOS operating system.

Meet Your Teacher

Teacher Profile Image

Naser Jamal

Computer Engineer

Teacher

Hello, I'm Naser.

I try to simplify complicated programming topics and present them into short and bite-sized videos!

See full profile

Level: All Levels

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: Hello and welcome to the complete by three data structures masterclass. This class is fully updated with hands-on learning to help guide you from 0 to heal and Python data structures. Python has four built-in data structures. They are lists, tuples, sets, and dictionaries. If you're learning Python, it's very crucial to learn those and the differences in between them. Luckily for you, this class does just that. In only 30 minutes of video lectures, you will learn all those different data structures, the differences between them, how and when to use them. This course is beginner friendly and straight to the point that you waste less time in theory and more time in hands-on experience. By the end of this course, you will have mastered the fundamentals of those four built-in data structures. And you will be able to recognize them when reading someone else's Python code. And if that's not enough to top it all off, we will have an interactive exercise as our class project, where we will quickly recap the fundamentals of those data structures to make sure that we have a deeper understanding of them. So what are you waiting for? Enroll today and elevate your Python programming skills. I'll see you inside the course. 2. Lists in Python 3: Hello and welcome back to my video. Today we would be taking a look at lists that are four built-in data types in Python use to store data. They are lists, tuples, sets, and dictionaries. We will start by learning lists, which are used to store multiple items in a single variable. So let's start by creating a simple list called cars for example. And now let's print our list. As you can see, it looks exactly like our list, including the square brackets, we can print individual items in our list using index. So for example, if I want to print Toyota, I would do the following. Similarly, if I want to access the second element in my list, which happens to be Volkswagen, I have to sit my index to one. Remember, lists start with 0. Okay, so let's say I want to access the last element in my list, which in this case happens to be Mercedes. And I don't feel like counting how many elements are in my list. I could do that simply by setting my index two minus one and minus two for Honda, and so on and so forth. Let's say for example, I want to print all the elements in my list starting from four. I could do that by simply using colon to select a range of items. So what if I don't want to print items starting from two all the way to the end. Let's say we want to set a start and an end. We could do that with colon two. So we will start from two and we will end at four. So this will start from two, which is four. Remember, because our list starts counting from 0 all the way to four, which is Mercedes, but there is a catch, it wouldn't print Mercedes. So when we set an enter colon, it ends at that element itself, but it doesn't include it in our list. You can also modify a certain item and you're less than. So. For example, if I wanted to change Volkswagen, BMW, I could do that by using index. Keep in mind, our list could include strings, integers, booleans, you name it all at the same time. So for example. Okay, so that's it for the basics. Let's say we want to write a program to find the largest number on the following list. There are plenty of ways to do that, and we're gonna go through some of them. The first method is resorting our list in an ascending order. I'm printing the last element in the list using minus one. So we will sort our list using salt method. And now our list is sorted from smallest all the way to the largest number at the end. Now we will print the last element in our list, which should be the largest number. The second method is max, slightly easier and very straightforward. So this is how we use it. Those are two fairly easy and straightforward methods, has slightly more complex method is using for loops and if statements. To do that, we will do the following three steps. Create a variable and assign it to the first element in our list. Iterate through the numbers list, or in other words, loop through the list. If any number is bigger than the largest number we found so far, it should be updated. So let's create a variable and assign it to the first element in our list. Now, let's create a for loop that iterates over each item in our list. Now, let's create an if statement that checks if a number is bigger than the largest number variable that we created. If that's the case, then technically this new number is the new biggest number we found. Thus, we should do the following. Now, when our loop is done and it went through all the elements in our list, largest number, should we update it to the biggest number in our list? Thus, we should print this number as the largest. So let's run our program, see how it looks like. That's it for today's video. Thank you so much for watching and I hope to see you in the next one. 3. List Methods: Hello and welcome back to my video. Today we'll be taking a look at list methods. The first method we're going to be taking a look at is insert. So say we have the following list. I want to add an item to this list. For that, I will use insert. So this is how we will use it. This insert method takes two arguments. The first argument is where we want to place our item. So let's say we want to place it after China, which is going to be two. Because remember United States as add 0, China is at one. So our new item is gonna be at two. So now we set the position. Now after the comma will enter whatever we want to insert two on our list. For example. Now let's print our list and see how it looks like. So let's say you want to add an item at the end of your list and you don't want to go through the hassle of counting how many elements you have. You could do that using a bend. Bend only takes one argument. That is the element you want to add to the list, because the position is already determined, we can also remove an item from our list using remove method. Remove also takes one argument. We could remove the last item in our list using pop method. If we want to remove all the items in our list, we can use clear method. This method also takes no arguments. If we want to know the position of an item in our list, we can use index. This can also be used to check for the existence of an item in our list. Let's try Italy for example. Since we don't have Italy and our less technically it should return an error. But because it returns an error, it's not very recommended. We could use this. Instead. This returns false, which is a Boolean value we could work with. So if we have a numbered list, we could sort it using sort. This method takes no arguments. If we want to count how many times an element occurred in our list, we could use count. Now we enter the elements we want account, let's say two, for example. So with occurred three times in our list. This method takes only one argument. So we saw how we could sort our list in an ascending order, starting from the smallest value to the largest value, we could reverse our less to all sorted in descending order using reverse. So now it goes from the largest value all the way to the smallest value. We can copy a list using COBie method. So for example, this is countries and this is countries too. Now, if any changes happening to countries, it will not affect countries too. So let's remove the last element in our country is less. As you can see, they're not identical because it's only COVID, whatever it was, but it's not updating it. However, if I equal countries to two countries. Now whatever changes happen to countries which would also apply to countries to join lists together, we could either use extent. This is extent, or we could simply make countries equal countries one. Plus, I mean technically this is not countries one, but it's just countries. Let's see. So both will give us the same results. So now we're at the last segment of the video. So assuming we were given this list which includes a duplicate, Germany was mentioned twice in I was asked to remove the duplicates from my list. Of course, there are plenty of solutions, but we're going to do that using for loops. So first, we want to create an empty list to store our items. Now we will create a for loop that checks if those items are not in our list than store them in our list. So let's take a look at what this for loop does. It says for each item in countries. So it's looping through each item in countries. It says if country, which is this item is not an duplicates, then add it. So this will add the item contrary to our no duplicates list. Now you might say, okay, this would add this item, this item, this item, it will eventually add this item to because it will loop through all the list. This wouldn't happen because we said f country, which is the following item, is not in duplicate because we've added it previously, it will not be added. Let's print our list and see how it looks like. As you can see, our loop works just fine. And it's added all the elements except the duplicates. So it's essentially a copy of our list without the duplicates. So that's it for today's video. Thank you so much for watching and I hope to see you on the next one. 4. 2D Lists & Nested Loops: Hello and welcome back to my video. Today we will be taking a look at 2D lists. As the name suggests, they are 2D list, meaning they have two-dimensions instead of one dimensionless, in which all the elements are stored in one row. So let's create a 2D list and see how it looks like. So in this list, each row is a list of its own. And to access items in this list, we will be using two square brackets instead of one. So for example, if I'm going to ax is number one, I'll do the following. Similarly, if I want to access five, our set, my index to 11, meaning the second row and the second column. And of course we all know lists in Python starts with 0. So that's why this is one instead of two. We could even create a fourth row and have it as 0. It doesn't need to be three items. Now, what if I want to print this to the list using print right away? It doesn't seem right. As you can see, we have brackets all over the place. So let's use a for loop to print this list. Well, this is still not good enough. This is where nested loops come in play. So let's add another loop inside of our loop to iterate through each item in this list. Now we have successfully printed each item individually. So that's it for today's video. Thank you so much for watching and I hope to see you on the next one. 5. Tuples: Hello and welcome back to my video. Today we will be taking a look at doubled. A tuple is a collection of Python objects that cannot be changed. Doubles, like lists are sequences. The major distinction between tuples and lists is that tuples, unlike lists, cannot be altered. Tuples use parenthesis, whereas lists use square brackets. So this is how a list looks like. This is how a tuple looks like. If I type names, dot, as you can see, I get plenty of options because it's a list. However, if I type names to dot, I have only two options. That is count and index because it's a tuple. So that's the main difference. We use parentheses to define them and they have less options than lists. We could print individual items though, just like lists. So for example, this will print the first item in our tuple. However, if I try to change that item like we usually do with lists, I would get an error. Tuple object does not support item assignment for that lists of more common and you're more likely to use them. However, they have their own users for locking the contents of your own traveled so that you don't accidentally change them later on. You can also like tuples and this format. Just to make sure it's really a tuple, we can do that by using type up and let's print that. We learned that we cannot add items to a tuple. However, we can add tables together. So for example, now let's print our new tuple. We can not tell you move individual items in a tuple. However, we can delete a tuple completely. Here are four built-in functions we can use with tuples. Len function which returns four because we have four items in our tuple. Max function which returns 90 because 90 is the largest value on our main function, she turns 15 because it's our minimum value. And finally, we can use double function to convert our list. So that's it for today's video. Thank you so much for watching and I hope to see you on the next one. 6. Sets & Mathematical Operations: Hello and welcome back to my video. Today we will be taking a look at sets. It's a very useful data structure similar to lists and tuples. But the major differences that sets cannot have multiple occurrences of the same element, or in other words, duplicates. So let's take a look at some applications for sets. So say we have the following list which contains duplicates. We can remove those duplicates by converting it to a set. So let's create a set to store our values. Now let's print our set and see how it looks like. As you can see, we use curly brackets to define sets and they have not containing our duplicates from the previous list. Similar to lists, we get plenty of options like adding or removing items. For example, we could remove an item using the Move. However, if we tried to remove an item that is not in our set, we will get an error. That's why it's safer to use this code. Because if we tried to discard an item and our set that isn't there, we won't get an error. We could add an item using ad and so on and so forth. You can explore more options when you type your set dot. And as you can see, there are plenty of options. Sets shine with their mathematical operation uses in Python. So let's take a look at some of them. At first glance, this set of operation might seem confusing, but really it boils down to four sections. Those are the four sections we'll be taking a look at. I made this table to visualize those operations easier. So let's take a look at union. To find the union of two sets we could either use. It will return a new set with components from both sets. One answer to, however, elements will not be repeated. That sets components are all one-of-a-kind. You could do this with more than two sets, two. So the difference between using union and vertical bar operator is that union will accept any iterable as an argument. While vertical dash will only accept sets as an argument. Otherwise, it will attend an adult. So now let's take a look at intersection. To find the intersection of two sets, we either use. It returns a new set containing elements shared by the two sets. So because only 24 were shared between these two sets, this is the only elements we have returned, similar to the previous one. The difference between using intersection or the AND operator is that the intersection will accept any iterable as an argument. And the AND operator will only accept sets as an argument otherwise, to turn an adult. So now let's take a look at difference to find the difference of two sets we either use. It will return a new set containing elements from set one and set two, because 35 were not answered two, those are the only elements we have returned, similar to the previous one, the difference between difference and the minus operators. That difference accepts any terrible, but this only accepts sets as an argument. So now let's take a look at symmetric difference. The find the symmetric difference between two sets we either use. It will return a new set containing items from either set one or two, but not from the both sets. So set two has a unique item of 79. It's not included in set to one. That's why it's added here. Same goes for set one. It includes a unique element of 53. That's why it's also here. And finally, the difference between the symmetric difference and the circumflex operator is that the symmetric difference can accept any iterable as an argument. While the circumflex operator will only accept sets as an argument, otherwise it will return an error. So that's it for today's video. Thank you so much for watching and I hope to see you on the next one. 7. Dictionaries: Hello and welcome back to my video. Today we will be taking a look at dictionaries in Python. Three dictionaries are used to store data values like a map, unlike other datatypes that told only a single value as an element, dictionaries hold a key value pair. Suppose we're making a website to bless or doesn't buy items online, similar to Amazon, Let's begin by making a dictionary to store our values. We have several key data, so let's list those. Now we'll create a dictionary. To create a dictionary, we will use a left breast and to close it, we will use the right place. We use commas to separate elements and quotation marks to define them inside of our dictionary. Now, our dictionary includes keys such as item, type, seller, year, and so on and so forth. And we have values such as phone, samsung, and take cells. So this is how our dictionary would look like in a table. As you can see, our dictionary can hold values varying from strings, integers, double float values, and so on and so forth. We can verify that our dictionary order is indeed dictionary using the type function. We can also create a dictionary using the dict constructor. For example. We can add additional elements to all dictionary using brackets. Keep in mind you have to use quotes when you're adding items. However, in the deck constructor, you don't have to use quotes around the key name. So let's print our dictionary and see how it looks like. Now we will take a look at how we can access data in our dictionary to print the type from our dictionary order to, we will do the following. However, if I enter the key name that is not in my dictionary, I will get an arrow. To prevent returning a key error, we could either use an operator or try. Let's start with n. So let's run our code and see how it looks like. We could also handle a key error using try. So the reason I'm using here or here, because if you remember, when I tried to print the following, this is what the error would get, a key error type of error. So that's why I'm telling my program to accept key arrow. There's more stuff we could do with dictionaries. So let's print the directory for order two. Let's try the getMethod. But to see how it works, we could use now to use this getMethod in our favor to find if we have a key name called age in our dictionary, we could do the following. Now let's print check. Finally, let's learn how to print all the data in our dictionary. We could simply do that by using a for loop that iterates over all the items in our dictionary. For example. For a more simpler way to print our dictionary values, we could use the items method. So that's it for today's video. Thank you so much for watching and I hope to see you on the next one.