Hello and welcome to this video.
In this video I'm going to show you how to create a tuple using a tuple constructor.
This involves using double parent theses to create a tuple using a tuple constructor or you first need
to give the tuple a name.
So I'm going to create a tuple I'm going to call it animals and then I use the equals to and then I'll
type in the word tuple and then I'll have double parentheses.
And inside the inner parentheses is where I will add the values I want to be in my two pulsars say a
lion under a comma add another one.
I'll say Taiga do comma and add one more let's make this one A.
So that basically creates the tuple using a tuple constructor.
So again if you want to print this out on the screen we can do is use a for loop to look through them
or we can just use the print statement to just print the tuple so we can just say print inside the parentheses
for the print statement.
We just type in the name of the tuple and then we save us the file using control s..
Now if I open my command prompt if you're on a Mac you need to use the terminal.
So this is my command prompt I'm just going to type the name of a file which is on my desktop.
You can see I've navigated to my desktop so I'll just call the file which is called My on this code
tuples to p y that you just print out the values of the tuple so you can say it's got lion has got Tiger
and it's got the bear.
You can also access the individual values in the tuple again by using the index lion will have an index
of 0.
Tiger will have an index of 1 and they will have an index of.
There is a special method you can use.
If I wanted to find out the length of the items in my tuple I could just do a print statement or a print
inside the parentheses I type in Len method and inside that I will just add the name of my tuple which
is animals.
So I'm just going to comment out.
Line number two.
I don't want line number two to be printed out.
I just want the line number three executed.
So just to control s to save and open up my command prompt.
So I'm just going to clear this screen and now type in the name of the tuple which is my on the score
tuples in the P Y press printer.
I can see he is giving me is return 3 which means the length of the elements in my tuple is 3.
So lie on 1 2 3.
Once you create a true pool you cannot modify it.
The only thing you can do is delete it.
If for example if Pa tried to add to my tuple I will expect to get an error or say if I say animals
which is the name of my tuples and I will use a method called add so to add.
And then inside the parentheses I'll add the value of what I need to add.
So let's say I want to add dog to that to pull just the way I was represented by I expect the code to
error because you cannot change a tuple once you've created it.
So if I do on a print statement here and say print animals I will expect this line of code to error
because you cannot change the tuple once it has been created.
So let me open up my command prompt again.
I just typing.
That's the name of the farm which is this file here.
If I press Penta you can see it will execute this line of code which prints the lamp which is three
but you can see the line for it did not execute it did not execute line 5 because of the error.
Let me show you what the Arrow says is say attribute error tuple object has no attribute at.
Okay so we cannot add to a tuple.
You cannot also change the value.
So let's say 5 put a comment here and then I come here a typing animals animals.
All right.
I say the.
Let's say we have Lion horn in there.
0.
I want to change that to let's say a cheetah
not sure that's the right spelling but.
Okay.
And then I'll save control.
Yes.
Now if I tried to run my python file again I expect there to be an error from line number five and six
because I'm trying to modify an existing value but it's not going to let me do that.
So I press enter.
He can see Prince three here which is line three of the code.
It executes that.
OK.
No problem.
Line 4 I commented out I'm 5 and 6 giving me an error.
He can see it says here type error or tuple object does not support item assignment.
So I'm trying to reassign a different value to an item that's already in the list.
It will not do that.
The only thing you can do to a tuple once you've created a tuple is delete it and you can delete it
just by using the keyword DL followed by space and then the name of the tuple you can see on line six
here.
I am deleting the tuple.
I've also commented out all the other lines of code.
That means those block of code will not be executed.
So I'm just gonna do a control as to save now a file run this file.
I expect to get an error to tell me that that does not exist.
All right.
So you can see here telling me here.
Name arrow animals is not defined and that is because I have deleted the tuple on line six here.
So that's it for this video.
Thanks for watching and bye for now.