00:11 Before we go to tuple literals though, here’s a quick throwback to string literals. You can create strings by just surrounding characters with quotes, either single quotes at the beginning and the end, or double quotes at the beginning and the end.
00:25 And both of these create a string literal. Now I’m mentioning these because tuples and strings have quite a lot of things in common. As you’ll see in the upcoming lessons, a string literal is surrounded by quotes, and a tuple literal is comma-separated values that are surrounded by parentheses.
One, two, three. Only. The first three get a medal, and now we can look at winning positions. I can see that that’s this tuple:
(1, 2, 3). And to prove that it’s actually a tuple, I’m going to pass it as an argument to the
You end the tuple by closing the parentheses again, and here you go. You’ve got your
employee tuple. And again, let’s double-check that it is indeed a tuple, even though it contains all sorts of different data types.
02:42 Here again, this is a tuple, so this is a way that you can create a tuple literal. You use the parentheses, and you separate by commas. I’ll tell you a little secret, it’s not so much of a secret, but the parentheses are actually optional.
and the type of
numbers is going to be a tuple. And also you see if I just type in
numbers, then Python prints the output with the parentheses around it because this is how tuples are represented.
Now, what’s another sequence type that you’ve already encountered? Those are the strings that I mentioned before. So you can pass in a string to the
tuple() function, and Python will create a tuple for you.
Then you will see that what I get as a result is a tuple, a tuple that consists of all the elements of the string sequence. So remember that strings are sequences of characters, and what the
tuple() function does is it splits it up into the characters that the string consists of and assigns them each as an element in the tuple.
So if you try to call
tuple() and pass it, for example, an integer, then you’ll get an error because the integer object is not iterable. So the argument that you pass to the
tuple() function needs to be iterable or a collection.
Become a Member to join the conversation.