Hello guys. Welcome to LearnVern. In the last topic we saw about List.
About how we can work with List in Python.
Today we will see how to work with Tuple in Python.
First of all, what is Tuple?
Tuple too is a collection in Python.
But Tuple is immutable here. Immutable means non-changeable.
If you’ve already input something in Tuple, you can not change it during runtime.
If you want to change it, you’ll have to convert the Tuple into a List first & then update it.
Tuple allows you to store multiple datatypes inside it. It even allows Duplicate values to be stored.
Tuple also works from the Index value which starts from 0.
From where does it start? It starts from 0.
We will see further ahead how to use Tuple practically.
We will see how to create Tuple. We will also see Indexing & Slicing.
We will also see its functionality.
We will see all of it practically so that you understand easily.
We will be more focused on practical work just like we did in all the videos until now.
Let’s go under Jupyter.
First of all, I’ll markdown. Hash, how to Create Tuple.
It is very easy to create Tuple.
To begin with, Tuple is recognised by the round bracket.
Let’s see how to make it.
I’ll write here Creating a Tuple.
I took a T1 variable for Tuple in which I want to store Tuple like Apple, Orange, Cherry, some integer values...some float values, and some repeated values as well.
So this is my Tuple which included Strings, Float values, as well as repeated values.
Now I’ll print Current Tuple to see how it looks.
Our current Tuple is ready.
Meaning it allows all types of datatypes but here, there’s 1 disadvantage & that is that it is immutable.
You cannot change immutable items. You cannot change any tuple here.
For instance, we want to see how to check the length of tuples.
How to check the length of tuple.
I have made a tuple named T2 here which includes integer values only.
I have a tuple of 7 values.
We have to check the length of the tuple.
Check the length of the tuple.
So I took the print function & I used the Len Length function in it & wrote T2.
It will show you the length of 7.
If you want to make it more understanding, you can add a string here saying that the size is 7.
So this is how you can check the length of Tuple.
Now if you want to do indexing in tuple.
What do we want to do? Indexing.
Let’s see how we can do indexing that is slicing with tuple.
Indexing or you can call it slicing as well.
How will we do it?
I’ll copy paste our first tuple T1 here.
First of all we will access our 1st tuple. We will see what value is being shown.
I used T1 here. Our current tuple looks like this.
We have to check how to access a particular Tuple’s value.
I’ll write here Accessing the value by using Index.
Suppose I want to access the value of Index 1.
Index 1 value. T1. That is our Tuple’s name.
Square brackets. We want index 1 so we will write 1.
And it did access the index value.
How to bring value? By using a square bracket & putting a value inside.
So you can access the value by using Index.
Now we will further see Accessing the value between Index 1 to 6.
I’ll write, Index between 1 to 6.
Simply write T1. 1 that is our starting point & 6. As we want the index value between 1 and 6.
After running it, we can see that we got the values from 1 to 6.
You have to do Indexing & slicing just like you did in List.
There’s nothing new in this.
Now I want to Access the value from last into tuple.
How to do it?
Print. Last value. T1. (minus) -1.
So we got the last value 4. So we got the last value of tuple that is 4, alright.
This is how you can access tuple by using Index & slicing.
Now suppose I want to Access the value till index 6.
Print. Value till index 6. T1.
We will just write colon & 6. The values from 0 to 6 will be printed.
This is how indexing & slicing works in Tuple.
Next up we will see how to change tuple under List.
We will go to a new cell & markdown Convert Tuple into List.
Let’s see how to do it.
I have a type named T1 available right now.
I printed by Current Tuple. T1. under round brackets.
I’ll write here Before change.
Now I will convert the tuple into a list.
Suppose I took an X variable.
And used List constructor and wrote T1 in it.
Now our tuple will be converted into a list. How?
I’ll write here “After convert” and get X printed.
You can see that the tuple got converted into a list.
Meaning now you can now fire any operation using the list.
Which further means that you can fire all the operations related to List here in Tuple because our tuple is now converted into a list.
So this is how tuple is converted into a list.
I have a tool named tuple constructor.
How to use Tuple constructor.
I took a variable named T3. Used Tuple Constructor.
I took 2 round brackets. Remember not every round bracket is Tuple.
I wrote Apple, Mango & Cherry.
Now I’ll print this T3 & it got converted into Tuple.
So note the double round brackets. Okay?
2 round brackets does not automatically mean tuple.
You created a tuple using Tuple Constructor here.
Now I want to convert Tuple into a list, then add a value in it and then print it back into Tuple.
Add item but after converting Tuple into List.
First of all I’ll create Tuple. Suppose T4.
Under which I took Apple, Orange, Cherry and Mango.
I’ll print Current Tuple T4.
This is my current tuple & I'll check its type as well.
This is Tuple only but confirming it through Type checking.
You can see the class is Tuple only.
I’ll write here Before convert.
First of all, we will convert it into a list.
Convert Tuple into List.
How will we do it? It’s simple.
We took an X variable under which I used a List Constructor.
Our Tuple is stored under what? T4.
Firstly I’ll convert it. Now I’ll print X.
My tuple has been converted into a list.
The square brackets have appeared.
Now I’ll print the type of X as well.
Type X. See it showed List.
I’ll write here, After convert.
Now that we’ve converted our tuple into a list, we will add a value in it & then we will convert it back to Tuple.
Add item into the List. Our list is stored under X right now.
Suppose I want to add something instead of Orange.
So I’ll write 1 that is our Index. I want to add Kiwi instead.
Print Updated List X.
Did you see Orange got replaced by Kiwi.
But all of this is still under List. so I have to convert it into Tuple yet.
Convert List into Tuple again.
It’s a very simple thing to do.
We’ll take a variable Y. We will use Tuple Constructor.
Write X here and then print our Updated Tuple.
Our tuple changed and the data was entered.
I hope you understood all of it so far.
We began with printing our Current Tuple that is the normal Tuple.
Then we converted Tuple into List.
Then we added an item into the List.
Later we converted the List back to Tuple. Alright.
So finally this is our updated Tuple.
Now we will see how to use For Loop with Tuple.
Loops with Tuple.
First of all, we will use For Loop with Tuple.
I made a tuple T5 which includes Apple, Mango, Orange & Kiwi.
These 4 strings are in Tuple.
Now I have to use it with For Loop.
For I in T5.
I iterated this tuple. To Iterate means to bring the values out 1 by 1.
I only have to iterate I here & I got all the values out. See?
So this is how you can use For Loop here in Tuple.
Now if you want to use While Loop in this same way…
While Loop with Tuple.
How will we do it? It’s very simple.
I’ll take the Tuple from above. T5.
And take a variable I whose initialised value is 0.
Why? Because the While Loop is a Conditional Loop.
We will have to stop it no matter what otherwise it will turn into an infinite Loop.
How will we stop it?
We will increment the value of I.
Let’s see how we will do it.
While I is < length of T5.
Meaning until when will the While Loop rotate? It will rotate until the values are available in T5.
Now we will print I.
We have to increment I as well. I = I + 1.
So however many times the Loop rotates, I’s value will increase.
As soon as I’s value will be greater than tuple’s length, it will exit the While Loop.
We will write Else here.
Print Loop Finish.
Now we will run it. You can see 1, 2, 3 here.
It arrived because we printed I here.
I had 1, 2, 3. But we will also have to print Indexing.
We have to print the values from T5.
How will we do it?
Write T5 here and take I in the Index.
However many times I’s Loop rotates, it will bring out the indexing values of Tuples.
The tuples’ value got out and our loop finished.
This is how you can use While Loop in Tuple.
Now we will see how to compare tuples.
I’ll write here how to compare Tuples.
I’ll show you how to compare Tuples here.
Suppose I have a Tuple named tuple1. Under which I have 1, 2, 3. 3 values.
Next I have tuple2. Under which is 1, 2 & 4. 1, 2, 4 but 3 values.
I have 2 tuples but the 3 values in both.
I want to check that first of all, tuple1 = to tuple2.
Print tuple1 == tuple 2.
I’ll run it & it showed False.
Why? Because here it is 3 & here it is 4.
Now I want to check if Tuple is greater than or not.
Tuple 1 < Tuple 2.
Print. Tuple1 < tuple 2.
The answer will be True. Why is this true?
Because tuple 2 has 4 in it which is greater than 3.
Which makes tuple 2 the greater one.
In the same way, tuple 1 > tuple 2.
Print triple 1 > tuple 2.
We will run it and False.
Why? Because Tuple 1 doesn’t have any value which is greater than tuple 2.
So this is how you can compare tuples.
So what did we see today?
We saw - How to create Tuple, how to check the length of Tuple, how to index & slice, how to convert tuple into List, how to use Tuple Constructor, how to add item but after converting tuple into list, how to use Loop with tuple and lastly, how to compare tuples.
In the next video, we will see Dictionary in which we will learn how to make a dictionary in Python.
Thank you guys, see you in the next video
Share a personalized message with your friends.