13 - how to use tuples in python (Python tutorial for beginners 2019)

**Understanding Tuples in Python**

Tuples are a very stable structuring kind of data type. This stability allows us to use them in various ways, making them a valuable tool in our Python programming journey. One of the cool things about tuples is that they can be unpacked, which means we can create new variables from their values.

Let's get started by creating a simple tuple representing a person. We'll say that every person has a name, an age, and a favorite food. We can do this in Python like so: `Nancy_Pants = (25, 'Pizza')`. This tuple represents a person with the name Nancy Pants, who is 25 years old and loves pizza.

Now, let's talk about why tuples are structured. Because they're so structured, we can actually use them to create new variables in one go. We can do this by using what's called tuple unpacking. Let's say we want to create three new variables: `name`, `age`, and `favorite_food`. We can do this by simply assigning the values of the tuple to these variables like so: `(name, age, favorite_food) = Nancy_Pants`. This is equivalent to writing:

`person = (Nancy_Pants)

name = person[0]

age = person[1]

favorite_food = person[2]`

As you can see, we've unpacked the tuple into three separate variables. This makes it much easier to access and manipulate the data.

One of the best things about tuples is that they're ordered. This means that we can use indexing to access specific elements in the tuple. For example, `Nancy_Pants[0]` would give us the first element, which is the age (25). Similarly, `Nancy_Pants[1]` would give us the second element, which is the favorite food ('Pizza').

Now, let's talk about how we can use tuple unpacking in Python. We can create three new variables like so: `(name, age, favorite_food) = Nancy_Pants`. This creates a list of two people, each represented as a tuple. We can then iterate over this list using a for loop, which would allow us to access the data in the tuples.

For example, let's say we have two people: `John_Doe = (20, 'Burger')` and `Jane_Smith = (25, 'Pizza')`. We can create a list of these two people like so:

```python

people = [(John_Doe, Jane_Smith)]

```

We can then iterate over this list using a for loop like so:

```python

for person in people:

name, age, favorite_food = person

print(name)

```

This would output: `20` and `25`, which are the ages of the two people.

Another cool thing about tuples is that we can actually create three new variables in one line using tuple unpacking. For example:

```python

name, age, favorite_food = 'John_Doe', 20, 'Burger'

```

This creates a list of two people, each represented as a tuple, and then assigns the values to the variables `name`, `age`, and `favorite_food`.

Now, let's talk about how we can use tuples in loops. We can use tuple unpacking to create new variables from the data in the tuple. For example:

```python

fruits = ('Apple', 'Banana', 'Cherry')

for fruit in fruits:

name, age, favorite_food = fruit

print(name)

```

This would output: `Apple`, `Banana`, and `Cherry`, which are the values of the tuple.

Finally, let's talk about how we can use tuples to iterate over a list of tuples. We can do this by using a for loop and tuple unpacking like so:

```python

people = [(25, 'Pizza'), (20, 'Burger')]

for person in people:

name, age, favorite_food = person

print(name)

```

This would output: `25` and `20`, which are the ages of the two people.

**Conclusion**

Tuples are a very useful data structure in Python. They're stable, structured, and can be unpacked to create new variables. This makes them ideal for use in loops and other applications where we need to access and manipulate data in a flexible way. Whether you're creating a list of tuples or iterating over a tuple in a loop, tuple unpacking is an essential tool that every Python developer should know.

"WEBVTTKind: captionsLanguage: enhey what's up you guys aaron here from clever programmer today and today we are just going to be talking about tuples in python you don't know what that is yet but you're about to find out let's get started alright so a tuple is a first of all it's a weird word I don't know what the heck it means but that's how you spell it tu piel e and I'm all a tuple really is it's a list with constraints so I'm in a previous video I think we all went over lists so a list a list just looks like like this write with brackets you have something like that all a tuple is gonna be is gonna be the exact same thing but with parentheses okay like that so now L is actually a tuple so I'm gonna change this to a t so a tuple is just it looks like just like a list but it has parentheses instead of brackets um that's not the only thing that differentiates them um that's just what it looks like on on the screen when you're coding that's what a tuple will look like but the difference is list could could have anything in there and so can a tuple they are similar in that sense you can index into them using that bracket notation so let me just show you that real quick actually print T at 0 as you can see the 1 prints out cuz that's the first one so you can do that just like a list you can read from it but that's pretty much it the thing the thing about tuples is you can't actually change it you can't you can't add elements to it you can't remove elements to it and I think that's about it you can't add and you can't remove and you can't change things within it if they're immutable if they are also unchangeable so that's that's kind of a little bit a little bit weird to to explain but basically you can't add or change you can't add or remove elements to it it's a very it's a very stable structured kind of data type so you can overwrite the entire thing so if I had tea here and then I and I overrode tea by setting equal to five then yes its entire tube will be deleted but I'm not actually changing things to it so what is this good for you might be asking okay so what's the point of having a to pool if I can just use a list well there are some times you want to be more secure with your data so let's say you had a a credit card okay credit card I just call it credit card like that and you have multiple things on a credit card so this is what I was talking about structured data so a credit card has a credit card number right so let's put that in first I think it's like 16 digits I'll just put want it before like that a bunch you'd have your the credit card number you might have a name on there so let's just say first name or oh no oh let's put a Joe Rogan's because why not and then you would have an expiration date which could be like December 31st 2019 or something like that or on credit cards I think it's just like 11/19 or 20 there we go November November of 2020 and then on a security code usually have like a three digit or four digit security code that you also need in addition to the number right let's just put it in like that so you have a credit card here as a tuple the reason we would have used and it used a tuple here is because every element of the tuple has a consistent meaning okay so none that none of this can be changed I cannot go in and change this number I cannot go in here and change this I cannot go in here and change this but it's all together as a whole them together so it usually you'd be using it for things like this um one thing that is that it's very useful for is actually having a list of tuples so let's you let's say you had multiple credit cards then you could create a second credit card let's just create a second one like this okay and then let's just change or whatever screw it we can just leave it like that so credit card one and credit card two and then let's just say we wanted to have a list credit cards with an S and then you could just create a list with credit card one and credit card to okay and then we could print out all of our credit cards just like that so you got are you guys following well we created two tuples here two separate tuples using the parentheses and then we stuck them in a list and we're gonna print it out so this is handy because let's run this again so it looks nicer this is handy because as you can see we have a bunch of credit cards here that are um structured there it's a structured kind of idea and then you can just put a bunch of these together so you can actually change this list this overarching this wrapping list on the outside you can always add more credit cards to it because it's a list but within each of these little credit card tuples you can't actually change or add anything to it because we don't want it to it's a very stable structuring kind of data type alright so that's pretty much tuples one thing one cool thing I wanna mention about tuples is because they're so structured you can actually do this cool thing called unpacking a tuple okay so let's get rid of these here let's just get rid of everything let's say we have a person okay a variable called person and we're gonna make it a tuple and we'll say that every person has a name right we're not gonna call a name we're gonna call it Nancy okay Nancy pants cuz I like that Nancy pants and then uhm age let's put 25 okay so Nancy Pat's is 25 years old and uh let's also put favorite food okay pizza like that so we have a person here okay as a tuple and because the tuple is structured a cool thing we can actually do in Python is actually just go like this we can create three new variables so name comma age comma fav food favorite food okay we're gonna put this here like this and then we can actually say this is equal to person so what's happening here is we are unpacking this tuple or are we we're unpacking this person tuple so this person tuple has three aspects to it has a name it has an age and it has a favorite food so that's what we're what we're specifying here we're specifying three new variables called name age and favorite food and then we're setting it equal to this tuple the cool thing that happens here though is now I can actually print out each of these things individually completely individually so pretty sure you guys see where I'm going here now we have these three variables separately so instead of going like okay person at 0 and person at one person at 2 etc we can actually just unpack it this way and use more descriptive names instead of person at 0 and person at 1 and whatever so when I run this as you can see all three pop out because we unpacked this tuple one other cool thing is you actually don't even need these parentheses here it's something really cool you can do so you can say okay name name age and fat food you're actually setting three variables completely in one line you're setting it equal to this tuple which is going to correspond in order because the tuple is ordered just like a list but it's structured so that you can actually unpack it this way very very useful thing especially if you are iterating over a bunch of tuples okay let's say you have like I said you could iterate over a list you can also edit over a a list of tuples and then in each iteration you actually unpack the V tuple in the loop itself so let's let's see what that would look like so let's say for name age five food in um oh this would have to be a list let's just create two people so you can see how this works very useful thing with tuples called tuple unpacking Nancy Joe shirt I was not as funny but whatever age 20 and favorite food is pasta okay so we have person 1 and person 2 and then we are just going to create a people list equals person 1 and person 2 oh I I could have just stuck these tuples directly in here but it's just better to use variables like this and I'm putting the variables here it's more clear okay I'm just creating a list here of a list of two people and each person is a tuple so now we can say okay let's iterate over people and since we are unpacking this it's going to iterate this this loop is going to iterate over this entire list and each time it goes through it's actually going to unpack these tuples so that in the iteration I can actually print out everything in here so I can put just like print name actually issues the same is here let me just copy and paste this boom-boom-boom yup just like that so what and then we'll put a space an empty an empty print statement we'll just print nothing okay so we have everything here for two iterations and when we run this what is wrong in people person is not defined oh okay well let's just get rid of this we don't need this here and yeah screw rid of all this oops yeah because that was giving us a problem because I changed this up from person to person one this is I wasn't working anymore so let's hit run and as you can see this loop ran twice and it unpacked both of these tuples okay so you can hold you can hold data structure data and tuples and then you have very easy structured access to them via unpacking so a very useful thing in Python I like using them a lot especially in loops like this down here in the for loop ah but that's pretty much it for this um this video guys thank you very much for watching and I'll see you guys next timehey what's up you guys aaron here from clever programmer today and today we are just going to be talking about tuples in python you don't know what that is yet but you're about to find out let's get started alright so a tuple is a first of all it's a weird word I don't know what the heck it means but that's how you spell it tu piel e and I'm all a tuple really is it's a list with constraints so I'm in a previous video I think we all went over lists so a list a list just looks like like this write with brackets you have something like that all a tuple is gonna be is gonna be the exact same thing but with parentheses okay like that so now L is actually a tuple so I'm gonna change this to a t so a tuple is just it looks like just like a list but it has parentheses instead of brackets um that's not the only thing that differentiates them um that's just what it looks like on on the screen when you're coding that's what a tuple will look like but the difference is list could could have anything in there and so can a tuple they are similar in that sense you can index into them using that bracket notation so let me just show you that real quick actually print T at 0 as you can see the 1 prints out cuz that's the first one so you can do that just like a list you can read from it but that's pretty much it the thing the thing about tuples is you can't actually change it you can't you can't add elements to it you can't remove elements to it and I think that's about it you can't add and you can't remove and you can't change things within it if they're immutable if they are also unchangeable so that's that's kind of a little bit a little bit weird to to explain but basically you can't add or change you can't add or remove elements to it it's a very it's a very stable structured kind of data type so you can overwrite the entire thing so if I had tea here and then I and I overrode tea by setting equal to five then yes its entire tube will be deleted but I'm not actually changing things to it so what is this good for you might be asking okay so what's the point of having a to pool if I can just use a list well there are some times you want to be more secure with your data so let's say you had a a credit card okay credit card I just call it credit card like that and you have multiple things on a credit card so this is what I was talking about structured data so a credit card has a credit card number right so let's put that in first I think it's like 16 digits I'll just put want it before like that a bunch you'd have your the credit card number you might have a name on there so let's just say first name or oh no oh let's put a Joe Rogan's because why not and then you would have an expiration date which could be like December 31st 2019 or something like that or on credit cards I think it's just like 11/19 or 20 there we go November November of 2020 and then on a security code usually have like a three digit or four digit security code that you also need in addition to the number right let's just put it in like that so you have a credit card here as a tuple the reason we would have used and it used a tuple here is because every element of the tuple has a consistent meaning okay so none that none of this can be changed I cannot go in and change this number I cannot go in here and change this I cannot go in here and change this but it's all together as a whole them together so it usually you'd be using it for things like this um one thing that is that it's very useful for is actually having a list of tuples so let's you let's say you had multiple credit cards then you could create a second credit card let's just create a second one like this okay and then let's just change or whatever screw it we can just leave it like that so credit card one and credit card two and then let's just say we wanted to have a list credit cards with an S and then you could just create a list with credit card one and credit card to okay and then we could print out all of our credit cards just like that so you got are you guys following well we created two tuples here two separate tuples using the parentheses and then we stuck them in a list and we're gonna print it out so this is handy because let's run this again so it looks nicer this is handy because as you can see we have a bunch of credit cards here that are um structured there it's a structured kind of idea and then you can just put a bunch of these together so you can actually change this list this overarching this wrapping list on the outside you can always add more credit cards to it because it's a list but within each of these little credit card tuples you can't actually change or add anything to it because we don't want it to it's a very stable structuring kind of data type alright so that's pretty much tuples one thing one cool thing I wanna mention about tuples is because they're so structured you can actually do this cool thing called unpacking a tuple okay so let's get rid of these here let's just get rid of everything let's say we have a person okay a variable called person and we're gonna make it a tuple and we'll say that every person has a name right we're not gonna call a name we're gonna call it Nancy okay Nancy pants cuz I like that Nancy pants and then uhm age let's put 25 okay so Nancy Pat's is 25 years old and uh let's also put favorite food okay pizza like that so we have a person here okay as a tuple and because the tuple is structured a cool thing we can actually do in Python is actually just go like this we can create three new variables so name comma age comma fav food favorite food okay we're gonna put this here like this and then we can actually say this is equal to person so what's happening here is we are unpacking this tuple or are we we're unpacking this person tuple so this person tuple has three aspects to it has a name it has an age and it has a favorite food so that's what we're what we're specifying here we're specifying three new variables called name age and favorite food and then we're setting it equal to this tuple the cool thing that happens here though is now I can actually print out each of these things individually completely individually so pretty sure you guys see where I'm going here now we have these three variables separately so instead of going like okay person at 0 and person at one person at 2 etc we can actually just unpack it this way and use more descriptive names instead of person at 0 and person at 1 and whatever so when I run this as you can see all three pop out because we unpacked this tuple one other cool thing is you actually don't even need these parentheses here it's something really cool you can do so you can say okay name name age and fat food you're actually setting three variables completely in one line you're setting it equal to this tuple which is going to correspond in order because the tuple is ordered just like a list but it's structured so that you can actually unpack it this way very very useful thing especially if you are iterating over a bunch of tuples okay let's say you have like I said you could iterate over a list you can also edit over a a list of tuples and then in each iteration you actually unpack the V tuple in the loop itself so let's let's see what that would look like so let's say for name age five food in um oh this would have to be a list let's just create two people so you can see how this works very useful thing with tuples called tuple unpacking Nancy Joe shirt I was not as funny but whatever age 20 and favorite food is pasta okay so we have person 1 and person 2 and then we are just going to create a people list equals person 1 and person 2 oh I I could have just stuck these tuples directly in here but it's just better to use variables like this and I'm putting the variables here it's more clear okay I'm just creating a list here of a list of two people and each person is a tuple so now we can say okay let's iterate over people and since we are unpacking this it's going to iterate this this loop is going to iterate over this entire list and each time it goes through it's actually going to unpack these tuples so that in the iteration I can actually print out everything in here so I can put just like print name actually issues the same is here let me just copy and paste this boom-boom-boom yup just like that so what and then we'll put a space an empty an empty print statement we'll just print nothing okay so we have everything here for two iterations and when we run this what is wrong in people person is not defined oh okay well let's just get rid of this we don't need this here and yeah screw rid of all this oops yeah because that was giving us a problem because I changed this up from person to person one this is I wasn't working anymore so let's hit run and as you can see this loop ran twice and it unpacked both of these tuples okay so you can hold you can hold data structure data and tuples and then you have very easy structured access to them via unpacking so a very useful thing in Python I like using them a lot especially in loops like this down here in the for loop ah but that's pretty much it for this um this video guys thank you very much for watching and I'll see you guys next time\n"