Welcome back, everyone, to this lecture, we're going to be discussing Tuples and Boyens.
Tuples are similar to lists, except for the fact that they are immutable.
That means they can't be changed or mutated, so immutability means you can't go in and swap out an
item that's already in that tuple.
So tuples are still a sequence just like a list, but you can't change them now due to the immutability.
That means they're going to have less methods than lists, and they're used when you don't expect or
want a particular user to be able to change the sequence of items.
While we don't manually define tuples, often we do see many Python functions actually return tuples
when returning results recall, we call these items method on a dictionary and actually return tuples
of key value pairs.
Add the key first and then the value.
This is a great use case for a tuple because you want to make sure that the user can't accidentally
swap the value and the key confusing which one is which because it's in a tuple and it's immutable.
You're always going to know, OK, that first item is key.
That's second item is the value.
Billions are going to be our basic building block for logic and control flow, allowing us to indicate
if something is true or false.
Let's explore these concepts in Python.
OK, here we are inside the Python file.
What I want to do is simply create a tuple if you want to create a list.
Remember, that was square brackets like one two three.
And those are the items in the list for a tuple.
We'll say my tuple.
This is similar to a list, except you use parentheses.
So still, the same different types of items can go in there and then we can go ahead and print out
my tuple.
Save that change.
Go ahead and run this.
And now I see the tuple.
Now look what's going to happen if I try to reassign an item in a list?
For example, if I say my list grab item index zero and say it's equal to new and let's go ahead and
then print out my list.
Save that change.
No, I'm still printing out the tuple as well.
If I run that now, I see new two three and then one two three.
So what's actually happening here?
I'm just grabbing the item at index zero and this list and reassigning it to be new.
No problem for a list.
Now let's try the same thing for a tuple.
So I'm going to switch this out to now say my tuple grab item at index zero and try to set it equal
to new save that chains.
Go ahead and run this.
And now you get this error message, so it says sorry tuple object does not support item assignment.
So as we mentioned, tuples are immutable, so once it's set to one two three, that's it.
You can't go in and swap another item out.
So you're going to want to use tuples when you don't want any user to have the ability to swap in items,
which is why we see it so often used as a return mechanism or just a good way to report values back
to a user.
You say, here's a tuple, I'm not going to allow you to change it that way.
We can avoid confusion later on.
OK.
So again, things like actual indexing is totally fine.
So if you want to actually know the first item in a tuple like, say, index zero, that's totally OK.
You just simply say Python Example pie here and notice it reports back.
OK, item one is at index zero.
Inside this tuple?
No problem.
Well, we can't do.
Is the reassignment now.
Again, as I mentioned, there's not going to be as many methods available on a tuple.
I just want you to get familiar with the fact that parentheses are going to distinguish a tuple and
a tuple is still a sequence of items.
All right.
So that is a tuple.
The other data type I want you to be familiar with right now is a Boolean, which you may have heard
in other languages.
A Boolean is essentially just a true or false.
For example, you can say a is true and then you can print out a.
Save run that and you can see true, the other type of bullying is false.
So that's pretty much it.
It's either true or it's false.
And there's not a whole lot we can do with them right now.
But later on, when we learn about control flow, things like if statements, this is where you begin
to add logic to your code.
Things like, OK, if this is true, then do something else or if this is false.
Go ahead and do something.
And for instance, you're going to see these kind of things and we learn about comparison operators,
for example.
I'm not saying, OK, Prince is one greater than two.
So this is actually a comparison operation.
I'm saying compare one.
Is it greater than two?
So let's go ahead and save that and see the result.
This returns back a boolean false.
Now, let's say, is one less than to save that change.
Go ahead and run this and this returns back.
True.
So we can already see that there's some idea of comparing two different data types within Python to
get back a boolean true or false.
Now that's it.
Just very brief overview of Tuples and Bulletin's.
We'll be using those later on in our code.
OK.
Thanks.
And I'll see you at the next lecture.