Okay, let's take a look at these things called tuples,
or some people call them tuples.
I actually don't know which is the right way,
tuple, tuple. You can say it either way, I guess.
So a tuple in Python is basically a list, but it cannot be
changed. So a little while ago we saw a list is equal to
'[1, 2, 3, 4]', and we could print the list, and then we could do
'lst.append', and we could put 5 in there,
and then we could print that list, and we see that we added something
just like this, '[1, 2, 3, 4]', '[1, 2, 3, 4, 5]'.
But a tuple does not work that way.
A tuple, it cannot be modified.
This is really good for performance increases, but it's also
really, really good just for keeping your data the same.
Some things just don't need to change.
So for instance, let's say I have a tuple, and you open it,
and close it with a regular set of parentheses here.
And let's do '('Kalob', 'Henry', 'Zephyr')'
And this is not going to be called 'tup'.
This is going to be called a 'group'.
So it's me and my two cats.
Now, on some instances of Python I have noticed that Python
will get this confused for a function.
I don't know why that happens, but if that does ever happen
to you, you can just throw a comma at the end here,
and that comma with the closing parentheses, Python goes,
"Oh, I know that's not a function.
There's nothing there. That's got to be a tuple".
So we have this 'group', and in this 'group', let's say we wanted to
add a new person to this 'group'.
Well, let's hit '.', and then tab, and we see that we don't
have 'append' in there.
We have 'count' and 'index', but we don't have 'append'.
So let's go ahead and try this
anyways, 'append', a new pet called "Axel", I guess. "AttributeError:
object has no attribute 'append'."
It simply does not exist.
We cannot do anything with that.
It does have 'count', which is pretty cool.
So I could count the number of times "Kalob" shows up.
It shows up once.
I'll also show the number of times "Henry" shows up, which is also once.
And we can actually change this as well.
So let's put "Henry" in here just to show the 'count' example.
We've got "Henry" in here twice.
I just reran those cells.
And so it's '('Kalob', 'Henry', 'Zephyr', 'Henry')' now.
'group.append' still doesn't work. Down here,
'group.count(
"Henry")', now shows up as two.
Now, one thing to keep in mind here, is with lists and tuples, they
do not have to be unique items.
We'll talk about sets a little bit later where every item
is going to be unique.
But that's not the case here.
So effectively we are stuck with '(Kalob', 'Henry', 'Zephyr', 'Henry')'.
And let's say we wanted to get rid of "Henry".
How do we get rid of this?
Well, with a list, we simply did the list name or the variable name, '.remove',
and let's do "Henry". Oh, that doesn't work.
Let's do another example here where instead of running 'remove',
we run 'pop'.
Oh, we can't even get rid of that.
So what do we do?
The only other option we have here is we can create a new
tuple. So let's call this 'new_group', and we are going to
say 'group', and we're going to slice this.
We're going to start at number 0,
this is an index.
So 0-1-2, and we want to go up to number 3, but not include number 3.
So 0-1-2-3 not including 3.
[no audio]
And let's do this, 'new_group', and we have removed it.
But we've also created a new variable.
So now we have two variables with basically the same data
in it, and we're no longer being very efficient.
Now, as an example of how a tuple is a sequence, we can use
a for loop. So 'for item in new_group', because we don't want
the old 'group' anymore, 'print(item)'.
Now, that 'item' is going to be "Kalob", "Henry", and "Zephyr", in that
order, and a tuple will always maintain that order.
Other data types do not necessarily do that.
Lists do, strings do, and tuples do.
They will maintain their order.
Sets do not, and dictionaries are not always guaranteed to
maintain their order, depending on the version of Python you're using.
But when it comes to a data type like a tuple, once it is
set, it is set in stone, and the only way to get rid of it
is to delete it. So we could do 'del group'.
And if we do 'group', we're going to see an error.
'group is not defined' anymore, because we deleted it.
So that is a tuple.
A tuple is a lot like a list, but it cannot be changed.
It is completely immutable.
It's that word here.
I did not mean for that to execute, but it is completely
immutable, which means once you set your data in here, you
set those items, they are set in stone.
There are ways around that by creating a new variable and
deleting the old one,
but at that point, you're still not being very efficient at all.
And it sort of defeats the purpose of using a tuple.
If you need to actually modify your tuple at all, you're
probably better off using a list.
But in Python, tuples have their place, and it is good to
know how they look and how they act.
And the fact that you can't really do too much with a tuple
is actually pretty nice
sometimes. Sometimes you want that data integrity.
Go ahead and give this a shot.
Create a tuple.
Where do we have a tuple? Tuple-Tuple-Tuple.
'group = ('Kalob', 'Henry', 'Zephyr', 'Henry')'.
Create your own tuple, and then just print that out,
and then what I would like you to do is try to run '.append'
as well as '.remove', and you should see an 'AttributeError' for both of those.
If you see those errors, you're doing the right thing. That's okay.
Errors are okay to have. Once you see those 'AttributeError',
let's head on over to that next lesson where we learn about
sets, I believe is the next one where it's a lot like a list
and a tuple, but it's totally unique and doesn't always maintain its
order. So have fun with this.
And let's head on over to that next lesson whenever you're ready.