Loading video player…

Find a Location (Solution)

00:00 Let’s start by creating the location tuple.

00:05 So remember, you can create a tuple by using the parentheses, or you can actually leave off the parentheses, but you need comma-separated values. I always like to put the parentheses because it makes it more clear that I’m creating a tuple.

00:17 So here the first value was 6.51, a floating-point number, the second one, 3.39. And then we have a string, "Lagos", and then another string, "Nigeria".

00:33 You can see here that tuples can hold different types of values. So here we have two floating-point numbers and then two strings. location looks good

00:44 and it’s of type tuple. The next task was to access at index two, and I can do that by typing the name of the tuple, location, then the square brackets, and then putting the integer for the index that I want to access.

01:01 So index two, it’s going to be the name of the city, Lagos. The third task was to practice tuple unpacking and assign each of those values in the tuple to one variable, starting by latitude, longitude, city, and country.

01:20 And I’ll set that equal to location. So there’s a location variable here, points to the tuple that contains the values, and it has four values in it, and I’m putting four variables on the left side of the assignment statement.

01:35 So then Python can unpack the location tuple and assign each value of the tuple to one of the variables on the left side. So 6.51 is going to go into latitude, et cetera.

01:46 Let’s look whether that works right away. So latitude now points to 6.51 longitude to 3.39. city to "Lagos" and country to "Nigeria".

02:01 I’m doing this in the REPL, which is why I’ve omitted the calls to print(). But if you did this in a script, in order to actually display it, you’d have to wrap it into a print() call.

02:13 Same output, otherwise. Cool. That’s the task.

Become a Member to join the conversation.