Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Concept: Tuples

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's talk about tuples
0:01 these are one of our core concepts from this application,
0:04 we saw that tuples aggregate or bind together
0:07 related sets of data but that data doesn't have to be all the same type,
0:11 here we have three numbers and the string
0:14 and this is going to represent some kind of measurement
0:17 now here we are going to put the temperature
0:19 and we've used the convention and that's really all it is
0:22 it's a convention that the temperature is the first value
0:26 and the measurement quality is going to be the fourth item
0:28 so the third index, again, just a convention.
0:31 Now, it's really the commas we saw that help define a tuple
0:35 it's not the parenthesis even though they look
0:38 kind of like there are key part to it
0:40 kind of here this line is equivalent to the one right at the top
0:43 when you print it out regardless of whether you put the parenthesis
0:45 Python print it with the parenthesis,
0:47 the other thing we saw is that you can unpack tuples into individual variables
0:51 so here we have a tuple m which looks like this
0:55 and we have four individual variables we want one that holds a temperature
0:58 one that holds the latitude, the longitude and finally the quality,
1:01 we can just reverse the assignment
1:03 so the bunch of variables equals the tuple and Python will unpack it.
1:06 Here you can see the values that get put into the individual variables.
1:11 We saw that tuples are really helpful in quickly binding together
1:14 related sets of data that you can pass around as a single item
1:18 however, consuming them was not so easy
1:22 you just index into them and you have to know
1:24 hey, the fourth thing is the quality so always use bracket 3 to get hold of it
1:29 And that's where named tuples come in
1:31 so if we import the collections module, we can say collections.named tuple
1:35 and we can define a type so here is the measurement,
1:38 this thing doesn't have values, it's a template to generate tuples
1:42 that do have values but conforming to a particular structure
1:45 where the first thing is the temperature,
1:47 the second thing is latitude, third longitude and fourth the quality.
1:51 Here we create an instance of our measurement class,
1:53 passing the four values, this works because the orders are lined up just right
1:56 we also saw we can use key value pairs and say like
1:59 temp=22.5 quality=strong and then the order doesn't matter
2:04 and then we can treat them just like tuples
2:06 here we can get the temperature and say bracket 0
2:08 but the real power is that they now have attributes
2:11 we can say m. and then the names that we put here
2:14 representing the positions so m.temp is temperature
2:17 m.quality is quality, when you print it out it's a little more obvious
2:20 like hey this is a measurement
2:22 and the various values correspond to temperature 22.5 and so on
2:26 that's just Python's formatting.
2:28 So my recommendation to you is when you are thinking about using a tuple
2:31 and you care about getting the values back out, and when wouldn't you,
2:35 very well may make sense to create a named tuple
2:38 to help you and all the other people that work with your code later on
2:42 be more successful with it.