Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Returning multiple values via Tuples

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's finish up our program.
0:02 The last thing we need to do from this get_weather_from_html() function
0:06 is actually return the weather report
0:08 we are technically printing it here
0:11 so we are communicating it back to the user
0:13 but imagine we want to call this function with different data
0:16 and maybe save it to a database
0:19 or we get the temperature back for different locations
0:21 and then we show them on a map,
0:23 we can't communicate these 4 pieces of information back
0:27 to the caller of this function,
0:29 that's what we are going to do now.
0:31 We are going to use this concept called a tuple.
0:33 There are many data structures we could come up with,
0:35 we could put these all in the lists and send them back
0:37 that would work but it's a little too flexible,
0:40 it's not quite as light weight and constraining as we would hope, right,
0:44 we want to get these values back and work with them
0:47 we don't want to append more values and things like that,
0:49 there is also dictionaries again, maybe little too rich if you will,
0:54 so tuples you'll see are just right,
0:56 although there is going to be a challenge to working with them
1:00 and we'll see that the collections module in Python saves the day.
1:04 So we want to return something from this method,
1:07 we'll start with return right, what do we put, it's easy to return the temperature,
1:10 it's easy to return the condition,
1:13 but how do I return the temperature and condition,
1:15 I can only return one thing form this function.
1:17 Well, it turns out, in Python you can define this thing called a tuple.
1:21 let's go look at that really quick up here.
1:23 So, in Python we can define a tuple by just putting some values together,
1:26 they don't have to be the same type, they can be like 1, 7, the word cat,
1:31 and even some kind of complex data structure like a list of 1, 2, 3,
1:35 that would be an item in our tuple.
1:38 Now if we just print out what this is here it should look
1:41 almost exactly like what I have written.
1:43 So here is the 1, 7, cat and so on,
1:46 and we can get individual items if I wanted the second item,
1:49 so the 7 these are all zero based, so 1 would be the second item,
1:54 and I can access it like this, we can even do cool things
1:57 like take a tuple and assign them to a set of variables on one shot,
2:01 this is called unpacking a tuple so I could have the number 1,
2:05 the second number I could have the string so we'll say S
2:08 and then we'll have L for list, if I wanted to assign
2:10 all the values in the tuple the first one to n1,
2:13 the second one to n2, the third one to s, the fourth one to L
2:16 I can just say this and Python will literally unpack those
2:20 and if I just print out n1, n2, n3, S, L
2:23 you'll see that actually unpacked those right,
2:27 this is not the tuples it's just one after another printing them out.
2:30 The other thing to notice is these parenthesis here are actually optional,
2:34 even though they kind of help point out hey this is the tuple,
2:37 it's not the parenthesis that define the tuple, it's actually the commas.
2:42 So here we have this thing separated by a bunch of commas,
2:45 and sometimes you'll see weird code like this where you have just 1 item
2:50 but you want it as a tuple so you can sort of treat it uniformly,
2:54 so I could say 1, and that would be the tuple right there
2:58 it crashed because I later tried to get the second item,
3:00 so when you see weird code like that, it's all about the tuples.
3:03 Now, with that in mind, let's go finish up this app.
3:06 Back in the weather function, we want to return the condition,
3:10 the temperature, the scale and the location,
3:12 and the format here is almost exactly what we want to do
3:15 so we can return a tuple like this,
3:18 and PyCharm has little squiggles on here to say you know,
3:20 those parenthesis- not really needed, ok,
3:24 so we are going to call this up here let's go to where it's used at the top
3:29 and we are going to capture the report here,
3:32 make sure we get the indentation right,
3:34 now suppose I would like to get the temperature.
3:38 So for a minute, they get my what would you type here
3:41 so I am going to say the temp in this location is
3:45 and I want to do a string.format and I would access the value
3:48 from the report by saying some index between 0 and 3
3:52 because we know there is 4 items,
3:54 which one is it, I don't know, let's see I think it was the second one,
3:59 let's try just take a guess right, yes, I was right,
4:03 but it's not at all obvious what to put there,
4:09 there is nothing about this data structure that will help me understand
4:12 that the second one is the temperature
4:14 and the only way to know really is to go down here
4:17 and actually look at the return value explicitly
4:20 and say oh ok, it's the second one.
4:22 And, that makes this hard to work with
4:25 and especially if I am looking up here at this code
4:27 suppose we want to make this a little more interesting.
4:30 Say the temperature in is let's do the report like it's overcast or sunny,
4:37 and we'll say 27 degrees Fahrenheit.
4:41 So I am going to have to write something like this here,
4:44 so what numbers do I put, well, we know the temperature
4:47 which goes here is going to be this one so this one is right
4:50 I think it was condition was the 0,
4:54 this one let's say is the third and that must be the second,
4:59 is this right, who knows right, this is super hard to work with,
5:02 let's try, maybe we get super lucky.
5:06 The temperature is Celsius, the temperature is in Celsius
5:10 it's mostly cloudy and 8.4 in Portland Oregon,
5:13 that is not a super sentence, right,
5:16 but how am I suppose to know from this data structure, right,
5:19 working with tuples they are great to bundle up sets of related data
5:23 but they are kind of hard to work with so we can do better.