Creating a Tuple
00:00 There’s two main ways to create a tuple. The first would be to use a tuple literal. This is where you use the literal syntax of comma-separated values wrapped in parentheses to define a tuple.
00:12
For example, this line of code creates the variable, my
_tuple
, and within it stores a tuple with the integer values (1, 2)
. Alternatively, because tuple is the default sequence type, you can omit the parentheses and the results will be the same.
00:29
The other way to produce a tuple would be to use the tuple constructor. To create a tuple using the tuple constructor, you must pass it an iterable. So in this example, we pass a list containing the integer values (1, 2)
to the tuple constructor, which then creates a tuple with those values, ultimately storing it in the variable my_tuple
.
00:50 Now let’s go to the REPL and see some more examples. First, open the Python REPL. You’ll notice mine looks a little different. Like I mentioned, I’m using bpython, a Python REPL with a few more features than the built-in REPL.
01:03 But this is purely for cosmetic reasons. So if you’re using the built-in REPL yourself, it’s not going to make a difference. It’s time to create a tuple using the literal syntax.
01:13
So in this example, you define a tuple literal by starting with an opening parenthesis and adding some values. First the string “Jane Doe”, next, the int 25
.
01:25
Next the float 1.75
, and finally the string “Canada”. Then you finish off with a closing parenthesis.
01:35 So you can imagine how this represents a row in a user’s table in a database. Note how the values are of three different types: string, int, and float. This clearly shows the heterogeneous nature of the tuple.
01:48
So how about another example? Here you define a variable point
holding the integer values two and seven, and this is still a tuple.
01:59 Remember, when using tuple literals, the parentheses are optional. So, can you also create a single element tuple? Yes, but it’s a little bit tricky.
02:19
As you can see here, when you try to define this variable one_word
and you try to make a tuple out of the string “Hello” and no other values, by simply adding parentheses, this is not a tuple and the parentheses are effectively ignored in this case. What you have to do is add a comma.
02:45 By adding a comma after the value, you indicate that this is in fact meant to be a tuple. And it works the same without the parentheses as well.
03:02
The variable one_number
now holds the tuple with a single value, the integer 42
.
03:10 Now you can try creating a tuple with a tuple constructor.
03:19
Here, the same tuple point
is being defined by using the tuple constructor and to the tuple constructor you pass a list containing the integer values two and seven.
03:30 It’s very important that you pass an iterable object to the tuple constructor. Otherwise, you’ll not be able to create a tuple.
03:43
As you can see, if you try to pass a series of values instead of an iterable, you’ll get a TypeError
, because the tuple constructor requires that it receives only one argument, and that argument should be an iterable.
03:57 Let’s see another interesting quirk of the tuple constructor.
04:04 Because strings are iterable containers of characters, if you pass a string to the tuple constructor, the output will be a tuple where each element is a character of that string.
04:16 So in this case, you created a tuple by passing the string “Pythonista” into the tuple constructor, and the output was a tuple of all of those characters.
04:25 You may intend to do this, and you may not intend to do this, so this is something to watch out for when you’re using the tuple constructor. You can also use the constructor with no arguments.
04:44 The results are the same, but remember, tuples are immutable, meaning that once you’ve got your empty tuple, there’s not a whole lot you can do with it. So there’s really not many use cases for an empty tuple.
04:58 So now that you’re well-versed on creating tuples, how can you access the elements inside of them? That’s the next lesson, retrieving elements from a tuple.
Become a Member to join the conversation.