Hello and welcome to this video.
In this video I'm going to introduce you to what two tuples are in Python.
What is a tuple a tuple is a list that cannot be changed in any way.
It means they are immutable.
They cannot be mutated or changed.
The only thing you can do to a tuple once you've created it is delete it.
You can create it you pulled by giving the tuple a name and just add in parentheses and placing the
values inside parentheses.
You can also use what is called a tuple constructor to create a tuple.
Once you've created a tuple you can access the individual elements within a tuple by using the index
number.
The tuples have a zero based index.
What that means is that the first value in the tuple is given an index of zero.
The second is given an index of 1 and so on.
Let me show you how you can create a very basic tuple with just a name and parentheses.
I am using the text editor or code at Tom and I have created a empty python file on my desktop.
I've coded my on this code tuples and I've given it a dot P Y extension.
That's what makes a python file.
So now let me begin by giving my tuple in name.
Try to give you two pools descriptive name that illustrate or tells you what it is to pull store in.
So I'm going to create a tuple I'm going to call it fruits and you'll see a course to sign and then
add my parents.
Is inside my parentheses.
I will not add elements all the values I want to be in my tuple.
So that is grapes to a comma and add another value.
I'll say apples always separate the values with a comma.
I'll just add one more and I'll make the berries so you can have as many items or elements as you wish
to do so.
So now this basically creates the tuples so I can just do a control s to save this file a feature on
a Mac.
You can do a command and press to save it.
Once you have created it tuple you can use what is called a for loop to loop through.
That is go through the list and then print it out on the screen.
So let's do that.
So to add a for loop so that we can print the elements of our tuple we just type in for.
I'm just going to use x to represent the values inside the tuple so I'll see X in fruits okay.
Now to a colon.
Press enter.
Then I will use the print statement to print the values.
Now just say X and what this should do is to print out the values or the elements on the screen.
I'm just gonna do a control s to save.
Now if I open up my command prompt this is my command prompt.
If you're on a Mac you need to open up your terminal and navigate to where you've told your file I've
stored my file on my desktop.
So I've navigated to it here you can see my desktop.
So all I need to do is just type in the name of the my file which is my on this call.
Pulls the p y and that should print the elements.
You can see here is printed all the elements is looped through and then printed them on the screen.
So that's how you can loop palm print elements from your tuples on the screen.
You can access individual elements within a tuple by using the index number.
So let's say I want to access the element berries from these fruits tuple.
This is how I would do that.
I'm going to use a print statement to print it out on the screen so I'll do print and inside the parentheses
are passed in the name of my tuple and then within that I'll add the square bracket and add the index
because the first element is grapes so that will have an index of 0.
Apples will be 1 and then the berries will have an index of 2.
So I'm targeting the berries so I'm just going to put a comment here on line two because I don't want
that to be printed.
I don't want that line of code to run.
So the only line of code I want to run is lime for on line one.
So once I've done that I just come here and do control s.
Now if I open up my terminal and just let me clear my terminal and then I'll type in the name of the
file which is my on the score.
Pulls the p why I only expect it to print out the value of berries.
As you can see here is printed out varies because that's what's inside my code pedestal.
I've commented out line to line three.
So the interpreter will ignore those two lines.
So that's basically how you can access individual elements within a tuple.
Thanks for watching.
Bye for now.