Create Tuples
00:00
In this lesson, you’ll learn how you can create a tuple in Python. You’ll learn about two ways, tuple literals, and you’ll learn about the built-in tuple()
function.
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.
00:42 This is a tuple literal that starts with parentheses and ends with parentheses and in between, it has multiple values that are separated by commas. These values can be of any type.
00:52 They don’t all have to be an integer, like in this first example, but they can be a mix of strings and integers and floating-point numbers. Really any Python data type that you can think of.
01:03 Here is another tuple that you’ve seen before. This would be maybe one employee and the record in the database where you can see the first value is an integer.
01:13 And then you have two strings following that, and it’s all surrounded by parentheses. And each of the elements are separated by commas. This is how you create a tuple literal.
01:23 Let’s go ahead and try that out in IDLE.
01:28 All I did was start with parentheses, then I can type in as many values as I want to. And when I press Enter, you can see that here you get a tuple back out.
01:39 And let’s double-check that it’s really a tuple. So I’ll assign it to a variable that could be maybe winning positions in a race.
01:49
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 type()
function.
02:05
And then Python tells me that this is indeed of the class tuple
. The second one that you’ve seen in the slides would be maybe an employee of your company, and that’s the first employee.
02:19 Her name’s Adisa, and she’s a software developer.
02:26
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.
02:58
What makes a tuple are the comma-separated values. So I could create the same tuple also without the parentheses at the beginning and the end. Let’s just do an example one: 1, 2, 3
.
03:11
If I just type it in like that, I also get a tuple as a result. So assign it again to numbers =
03:22
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.
03:35 But when you define a tuple, you don’t need to put the parentheses there. However, I would suggest to always do it because it makes it more clear what you’re actually creating here.
03:46 So this is how you create a tuple.
03:50
Another way of creating a tuple is by using the built-in tuple()
function. With this, you can pass in another sequence type and then create a tuple from that.
04:01
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.
04:11 Try that out in IDLE as well.
04:15
So assign it to something, word = tuple()
and then pass in a string that says "Python"
.
04:25
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.
04:49
So this is the second way that you can create a tuple from another sequence type by using the tuple()
function. Keep in mind that this needs to be a sequence type.
04:58
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.
05:14 All right, so this is how you can define a tuple. And next up, I’ll continue to compare tuples and strings a bit more because you’ll see they have quite a bit in common.
05:22 And if you have somewhat of an understanding of strings, it’ll help you to better understand tuples as well.
Nikolay Labaznikov on Nov. 4, 2024
Maybe a trailing comma should be mentioned:
>>> qwe = (1)
>>> type(qwe)
<class 'int'>
>>> qwe = (1,)
>>> type(qwe)
<class 'tuple'>
Become a Member to join the conversation.
Anonymous on Feb. 15, 2024
Tripped me up a bit when doing the following:
but this line x = x + tuple([4, 5, 6, 7]) and x += (1, 2, 3), is simply a reassignment, and when thinking logically, reassignment is not the same as modifying an existing obj.
anyways, correct me if i’m wrong 🙂