Let's discuss tuples now, tuples are a very swift like concept, and they make our lives extremely
easy.
It took me a long time to come round to tuples.
I didn't even know they existed until I started digging, particularly for courses like this, and went,
oh, that's handy.
So let's say you've got two variables.
One is an error code, which is faurot for an integer.
One is an error message, which is a string not found.
Now, if you wanted to tell the user in your app that that resource wasn't found, you would do not
a print, but something else similar.
You'd say string error code.
And then you'd say, plus.
Espace, plus the error message.
OK, and if we run that, we're going to get the output, you think we're going to get 404 not found.
Now, the problem is that we have two variables that describe one situation, that it would be really
handy if those variables were mashed together, if you like, into a single variable.
That's what tuples are for.
So we can say let our error.
Equal to open and close brackets, these are just the regular brackets.
404 and.
Not.
Found.
And because we've put everything in brackets, our system knows that this is a tuple.
So how do you access the tuple part of this?
Well, we're going to have the same old print statement where we turn something from an integer into
a string.
We reference the tuple and then we put it point zero and you'll notice that's what it suggests for us,
point zero.
And then we're going to add the space and then add the error.
Dot one.
So, of course, if we print this, we will get the same error, print it out, 404 not found, except
now we have two lines of code instead of three.
OK, so let's look at the notes here, what else have I got for you, right?
Oh, an advanced code now.
The advanced error code might look a little something like this, you know what, I'm just going to
copy this because you don't want to see me typed.
Let's go advanced code would look like this, so we have a 404 and then inside the tuple we have another
tuple.
And so that might contain a specific error code for the not found and then a specific error message
and to access these things.
We use the following syntax, 021, you know how to access zero, but if we go to one and we put another
dot, it allows us to access zero and one.
Now, if you were using a tuple to decipher XML or JSON that was coming back from your Web server,
I would say it wouldn't be the best way of deciphering your error codes and things because it's better
to use a model that's just the way that we do things.
Encoding use models, not tuples.
But there are other places where tuples will come in handy.
So what I'm saying is don't use this for a 404 not found, but it does make a good example to teach
you guys.
OK, so what else have we got here?
We can print that out, of course, but we're not going to do that.
Let's get equal, are there we go.
So let's copy this and drop it into here.
Let's start equal the following.
So when you define a tuple, you can actually give names to the individual elements.
So if I was going to access this stat dot now you don't have indexes as in zero and one, you have the
actual names.
So Stackpole would come out, of course, as 404 so you can name them.
And in practice, it's actually quite a good idea to name them.
But by the point you've gotten round to naming your tuples, you may as well have created a data class
or a model class of some sort.
If you don't know what those words mean yet, don't worry.
You will write a little bit of homework, a little task for you.
The following, please create a nested name tuple that holds your age first and last name and then print
out your full name and age.
So a nested tuple is another word for this, where we put a tuple inside a tuple, it's nesting inside
it.
So you've got your homework.
Good luck.