 Hey everybody, it's Brian and this is the fourth Python tutorial. We are going to go over a few different things here. So first thing we're going to do is make a new file and we're going to call this the very descriptive video or. Now the first thing we're going to cover is going to be lists. We've talked about lists and tuples and things like that and I keep saying we're going to get to it. We're going to get to it. Well, guess what? We're getting to it. So we're going to talk about lists. Now what is a list? If you're familiar with other languages, a list is an array. In short, if you're not familiar with other languages, well, what is an array? Simply put, think of it as a box and in that box you can place things like, actually let's call this mList and you need these brackets here and we're just going to say one, two, three, four, five. That's a list. So think of it as a box that you can place things in and in this box we have five things. Now you've heard me talk about zero based. That's your first indicator that it's an array. Most languages arrays are zero based. You can pretty much use the term array and list interchangeably. So if you hear somebody say, oh, I was working with an array, they're working with a list. If somebody says I'm working with a list, they're working with an array. Pretty much a similar concept. Obviously, when you get into different languages, they're treated differently, but down in the nitty gritty, they're pretty much the same. The cool thing, however, about Python is you can actually put things of different types in here. So you can also say like, let's see, let's actually, let's redo this whole thing. I want to really get crazy with this, say five, two, one, four, three. And then we're going to say dog, cat, and why not bird, because I can hear my girlfriend's bird in the background squawking away. Can you guys hear that on the video? That's like super loud. I can hear it through my headphones. Anyway, so we're just going to print the list. Save that. Let's run it. And you can see it just prints out pretty much what we just typed in. That's exactly what it looks like in memory. As far as Python's concerned, anyways, I know the C++ people are like, no, that's not what it looks like, but we're in Python land. So when in Python land, do as the Pythons do, or Romans do, or whatever the saying is. All right. Now, why would you want to create a list? You know, you've got this box, you've got things in it, what can you really do with it? Well, think of it in terms of like a line at a bank. There's 10 people in line. Each one of those people is unique. But you need to treat each one of those people as an individual. And that's what you can do with a list. You can actually do some pretty cool things. Like we're going to count the number of cats, because I really like cats. Print and we're going to say there are percent D cats. And I've had a very long day, but a very rich and rewarding day. So I might make a few mistakes. Now remember from our previous tutorials, this is case sensitive. So if you do cat, that's not going to find it. Remember everything's case sensitive. So let's run this. There are one cats, because there's one cat in there. Now, if we were to just, you know, through the magic of copy and paste, let's just throw a cat in here somewhere. I like saying that. Let's just throw a cat in there. There you go. There are two cats. I mean, some programming languages are like trying to herd cats, so anyways. So you can count the number of specific objects in there. You can also get the length. Let's just do this. We'll say, whoops, percent, len, n, m less. I'm pretty sure that's how that works. Yep. There are eight objects in the list. You can count those out. There are eight of them. Now, let's say we want to find the specific position of something. Like we want to find that pesky cat. Find the index of the cat. Now what is index? Well, items in the list are indexed, meaning they have a number in memory. The first position is zero, then one, two, three, four, and on, and on, and on. It's always zero base. Remember that. It always starts with zero. So we want to find where is this? What's the index? What position is it in? We're going to say print the cat is at index, and we're just going to do percent d, percent, and we're going to say, m list, index, and we're going to find that cat. All right, so run this again. The cat is at index six. So if you count this out, remember it's zero base, zero, one, two, whoops, zero, one, two, three, four, five, six. So we're at the sixth base. Whew. All right. We're not done yet. I have a lot to them. So we're going to actually insert an item into this list now. And if this tutorial gets too long, I might break it up into a few videos. I've been trying to keep them a little bit shorter. I've had people tell me, hey, man, I watch your videos on my cell phone, and they get a little bit too lengthy. So insert, and we're going to insert at position two, or I should say index two. We're going to insert a fish. When was the last time you really wanted to insert a fish into anything? Really think about that. All right. So we're going to now print out the list and see what happens here. And you can see at position two, remember zero base, zero, one, two, we now have a fish. Now we're going to append an item. Append just means add it to the end. You'll hear that quite a bit, where I'm going to append a file or append an index or append an array or whatever. Append an object. All right. End list. Oops. Cannot type today. Append, and we're going to add a snake. And we're going to print out the list here. You'll see, sure enough, now we have a snake at the end of our list. Now we want to remove the item, because I'm not a big fan of fish. I'm more of a snake and potatoes kind of guy. Remove the fish. And we're going to say, end list.remove, and remember case sensitive here. And now the fish is now missing, see? Now let's get a little crazy here. Let's say we want to reverse this. And we're going to say, we'll see here, end list.reverse, and then we're going to print it out. So now the list is exactly reversed. It's a mirror image of what it was. Notice how reverse actually took our existing list and modified it. So if we were expecting cat to be at a certain index, it is now at a different index. And you can test that by, you know, running that yourself, saying, what's the index of cat? And we can actually just copy and paste that there and run this again, and we'll see the index of cat is now at two. So you got to be a little careful when you do things like reverse. So what we're going to do next, and I'm just going to add some space here, is we're going to slice and sort. And this is going to be the end of our little list here. Remember a slice, we're talking about like a loaf of bread, you can take a chunk or a slice of the bread out of the loaf. That's exactly what we're going to do here. So we're going to make a new list, and we'll call it N list. Actually, let's call it a new list. That way it's not confusing. And we're going to say mlist.copy. And what we're going to do here is we're going to actually just make a complete copy of that, a mirror image if you will. I shouldn't say mirror because mirrors are reversed, but you know what I mean. We're making a, you know, a clone of this thing. Now we're going to take the new list, and we're going to reverse it back to the way we wanted it. And we're just going to say, new list equals, and this is going to blow your mind a little bit here, new list, and we're going to actually slice this thing. And we're going to say zero to five, and then we're going to sort it. And you're probably going, what in the heck is he doing? Well we're going to explain this here super quick. So what we're doing is we're taking our existing list, we're making a copy of it, shoving it into memory as new list. We're taking new list, which is a copy of that and reversing it. So it'll now be back to this way right here. We're then slicing, we're saying from index zero to five, get another list. Now what we're doing is we're taking our new list, slicing it, and adding that new list into that memory. What does that mean exactly? It means we're overriding that variable. Remember a variable is something that will change. You could very easily create an entire new variable called my new slice or something, but I just wanted to show you that you can actually do that to variables. Now because we've gotten zero through five, we're pretty much only going to get these numbers. Then we're going to call sort, and it's going to print out the new list, which is going to be one, two, three, four, five. Fingers crossed. Let's see what it does. Yay, it worked. Here's our new list right there, one, two, three, four, five. Now in case you're curious, you cannot call sort on mixed types. You'll have to have some way, and I believe we're going to do this in advanced tutorials, of telling it how to sort, but it's much more advanced than where we're at right now. That's why I took out the bird, the cat, the dog, the snake, and et cetera, et cetera, et cetera, because it would throw an error in there. So let's just demonstrate that. Let's say new list, dot append, and we'll say kitty, because I like kitties, and let's try running this and see what happens. Yeah, see, we get unsortable types, there you go. That's why I took that out. So I think we're going to try and keep trudging along here. We're doing pretty good on time. I want to cover tuples, so let's just call this, actually I can probably just keep it all in the same file. Why not? Let's do this. So we know that we're switching up here. Now, we've worked with tuples before, and you're probably like, what in the heck is a tuple? Well, I'm going to copy and paste some stuff in here. Just to save time, I like that sound effect. Anyways, this is a multi-line comment, it's just triple quotes, a tuple, and I actually got this from learnpython.org, and I don't think I could have worded this any better. Tuples are fixed size in nature, whereas lists are dynamic. Meaning once you create a tuple, it's not mutable, meaning you cannot change it. It's read-only. Yes, I'm going to repeat that, it's read-only, meaning you cannot add elements, you cannot remove elements, you can however find elements since it doesn't change things. You can also, in the operator, check to see if something exists. Now why would you want a tuple? Well, in case you wanted read-only. You'll notice a lot of the functions we've been passing things to are tuples. We're passing read-only information. We don't want that function to modify the information we're passing it. So let's just make a tuple here, and we'll call it my tuple. And the syntax for this is very scary, are you ready? We're just going to go one, two, three, four, five. Notice that's the difference right there. A list has the brackets, tuples has parentheses. That's it. Let's do it. Now we're going to print it out. We're going to print my tuple, just so you can see at the very end, one, two, three, four, five. And let's just add tuple. That way we know it's different. There we go. So that's our tuple. And you can even see in the little window here, the list versus the tuple indicator. So you can plainly see they're different. Now as you'd expect, because this is not mutable, meaning we can't change it, when you hit the period after the variable name, there's a lot less options here. We can count and we can index, which we've already done in lists, and they're the exact same syntax. But that's pretty much it. I mean, you can iterate over them, but that's pretty much, you can't really add or insert or anything like that. So you can say, you know, let's do, let's just do it. And index of three is, I got distracted, sorry about that, it's going to happen from time to time. Oops, let's run this. So the index of three is actually two, because remember this is zero based. So pretty much everything you've learned with lists, you can apply that to tuples minus anything that's going to modify that list. You can't add, you can't remove, you can't modify the item, etc., etc. One thing I probably should back up here, let's actually just do this. Modify an item in the list. How would we do that? So let's say, new list, we're back in list mode, sorry, we had to back up here for a second. And you can access it by its index, and this is why index comes important. So we're going to take that first item, because it's zero based, and we're going to say, LOL, why not, just, you know, because it's been one of those days. I hear my daughter say that all the time, LOL, LOL. And when we run this, you'll see because we've reversed, it's our last list, there's LOL. So that's how you would modify an existing item, you call it by its index. Now if you try to do that with a tuple, let's actually just, through the magic of copy and paste here, and we'll change my tuple. And we're going to try and modify this tuple here. What do you think's going to happen? Yep, tuple object does not support item assignment, meaning we cannot modify this. And I'm actually going to leave this in the file. Bad will not work. And I'm going to comment this out here. Just to kind of solidify that a tuple cannot be changed. Whew, you think we're done? Nope, we're not. We're going to cover dictionaries. Now what's a dictionary? Did I spell dictionary right? We're going to say that I did. I had eye surgery, so I no longer need glasses, and sometimes things get really blurry. And when I stare at word too long, it gets very blurry. So forgive me if that's misspelled. All right, so what is a dictionary? Think of a dictionary as a list on steroids. So we're going to say ages. And we're going to say ages is, and we've got yet another type of bracket. I don't know what that's really called. I'll call it a bracket, why not? Brian, and then we're going to add a semicolon, and we're going to say 40. And then a comma. Notice that comma separates things. I'm going to say Heather, which is my daughter. And she's 22. And we're going to print, whoops, print eye surgery. I'm going to blame everything on the eye surgery from now on. In case you're wondering, eye surgery was expensive, but it was well worth the money. I actually did a video on it if you haven't seen it. I recommend you go watch it. All right, let's run this. And at the very end, you'll see here is our dictionary. Now what's going on here? This looks a little different. We've got these brackets. We've got a string and an integer separated by a semicolon. Well, what's going on here is we're actually assigning our own indexes, or keys, as they're called. To kind of solidify that, we're just going to print ages.keys. So we're only going to print the keys. Notice how it returns a dictionary of keys, Heather and Brian. So what we're doing here is we're replacing the index, which this guy right here, the number. We're replacing that with an actual object. And it can be anything. It could be a class. It could be a string. It could be a number. It could be whatever. So now we don't have to remember position 0. We can do some pretty cool things. Let's actually just finish this up here. Print ages.values. We can also print the values out. And we're going to print ages.items. Just to give you an idea of what's really going on here. So the keys are there. The values are there. And the items are, well, you guessed it. Now notice how these are tuples. Kind of neat, huh? But what you can do here is you can do things like, let's say you want to print a specific item. Like, let's say print ages Brian. What do you think that's going to print? Well, it's going to print the value at Brian's index or the key. So it's going to print 40. Sure enough, there's 40. So that's how you can do that. So you don't have to remember, OK, well, this is at position this, this is at position that. No, you can assign your own keys. It's pretty sweet, actually. All right, so we're going to now add an item. Actually, let's delete. Sorry. We're going to say del ages Brian. And remember, these keys are case sensitive. Everything's case sensitive. You should also remember that. And we can, and I'm going to add a note behind here, can use pop. Now what does that mean? Well, delete's just going to delete this key with the associated value. And pop will delete it, but also return it. We don't want to return it. So we just want to delete it. Kind of confusing. All you really need to know is if you want to return it and delete it at the same time, use pop. Otherwise, just use delete. And we're going to actually print this out when we're done. Why not? And now you can see that the only thing in our dictionary is Heather. Well, that's no good. My daughter gets lonely. So we're going to add an item back in. Add an item in. I can tell my eyes are getting tired. That's one thing I didn't like about the eye surgery is now my eyes get very tired very quickly. Makes video games kind of a challenge. To add an item in, you pretty much just call a key that doesn't exist and assign it a value. And then we're going to say print ages items. And now Brian's back in our dictionary. Now modify a value. Let's say ages Brian equal 99. Why not? I'm not that old, but print ages. What do you think is going to happen here? Let's run it and find out. Brian is now 99. Now you might be a little confused, because when you print this out, you'll see that it's a tuple. And you're like, now wait a minute. You cannot modify tuple. You just explained this. That's true. What I've just given you is the dictionary items. This is what it's going to return from the function. But internally, in memory, it's a list. It's actually two lists. And then it manages what points to what. So you can actually modify it. What a mouthful. Well, we've learned quite a bit in this tutorial. We've learned about lists, learned about tuples, and we've learned about dictionaries. And we've learned how to use them pretty well, I might add. There's still a ton more out there to learn. But that's all I'm going to cover to this tutorial just for the sake of time. I'd encourage you to go out to Google and do a little research on your own. Also, be sure to check out my rest of my YouTube channel. I've got tutorials for other languages as well. And I've got, oh my gosh, almost 5 million views. I haven't looked at this in a while. Man. My website is runoff donations. So imagine if everybody that viewed donated a dollar, I would be retired. Anyways, voidroms.com, I haven't put it up yet. I've been really busy. But you can find the source code for this and other tutorials. And there is a Voidroms Facebook group with almost 200 of us in there. I'm hoping to grow that, because I see a lot of collaboration and people just helping each other out. Well, that's it. Talk to you later.