Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Multiple values from a function
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, here we are. We've got our city, state, and country, but our function over here, it just returns one thing: a
0:08 string. Or something else, a number or whatever. Functions just return one thing, actually, sorry, this one, but same idea: returns one thing.
0:17 How can we take all that data that we've gathered, the city, state, and country and return them back?
0:21 Well, what we can do is we can create something called a tuple. So a tuple, we'll call this "loc" for a minute, the way we create this in Python,
0:29 we would just say "separate things by commas", and then we could actually return that one thing like this. Let's see what we get if we do this.
0:38 We say Portland, Oregon, USA. Look what the location is. It looks a little bit like a list, but it has parentheses instead of square brackets,
0:46 which means you can.. it means it's a tuple. but the limitations are you can read the values of it, but you can't add to it and stuff.
0:53 But other than that, you can kind of think of it as like a list a little bit. So this is cool.
0:58 And then up here, we have to say we get our one location, but what we really want is the city, state and country. So on the other hand,
1:07 on the reverse side, once I have a tuple, I have this really cool thing that I can do in Python, It's called tuple unpacking. So I can say
1:13 "city, state, country equals location". And then let's just print out these things here, see what we got. If We come over here in Portland,
1:25 Oregon, USA, look what we get when we print this out right there. That's those three things just with a space between them,
1:31 that's how print works if you just put multiple things into it. But they all came back into city,
1:36 state and country. We could even put a break point right here, at the debug, and notice city is Portland, state is Oregon,
1:43 Country is USA, perfect. Now, We don't need these intermediate steps, Actually. I just wrote them so it's really clear what was going on.
1:51 So what I want to do is I just want to say for the moment, city, state, country equals that, and the tuple unpacking will just happen automatically,
1:59 and then down here we created the location and we returned it. We can also just do you like that. We don't need this anymore, okay?
2:07 So one more time. Here we go. We got them back exactly as we had hoped. So how do we return multiple values from here? We just return a tuple.