#100DaysOfWeb in Python Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Tuples
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Tuples are a lightweight, immutable data structure in Python that's kind of like a list but that can't be changed once you create them.
0:08
And you'll see many very cool techniques that make Python readable and easy to use are actually clever applications of tuples.
0:17
On the first line here, we are defining a tuple m, the way you define a tuple is you list out the values and you separate them by commas.
0:24
When you look at it, it appears like the parenthesis are part of the definition, and when you print tuples you'll see that the parenthesis do appear
0:32
but it's not actually the parenthesis that create them, it's the commas. We want to get the value out over here we want to get the temperature,
0:38
which is the first value, we would say m[0], so zero-based index into them. If we want the last value, the fourth one,
0:45
we would say m[3], that's the quality of the measurements. Notice below we are redefining m, this time without the parentheses,
0:51
just the commas and we print it out and we get exactly the same thing again, so like I said, it's the commas that define the tuple not the parentheses,
0:59
there is a few edge cases where you will actually need to put the parentheses but for the most part, commas. Finally, tuples can be unpacked,
1:08
or assigned to a group of variables that contain the individual values. So down here you can see we have a "t" for temperature,
1:16
"la" for latitude "lo" for longitude, and "q" for quality, and those are the four measurements in our tuple,
1:23
we want to assign those and instead of doing like we did above where you index each item out and assign them individually,
1:28
we can do this all in one shot, so here we can say variable, four variables separated by commas equals the tuple,
1:35
and that reverses the assignment so you can see "t" has the right value of 22, latitude 44, longitude 19 and the quality is strong.