- Podcasts
- Python To You
- _6 Python Tutorial for Beginners _ Tuple _ Set in Python
_6 Python Tutorial for Beginners _ Tuple _ Set in Python
Python To You
- October 10, 2026
- 7 min
About this episode
No description.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
_6 Python Tutorial for Beginners _ Tuple _ Set in Python
Welcome back aliens, my name is Dhavan Reddy and let's continue with the series on Python. So in the last video, we have talked about list and we were supposed to complete tuples and set, but unfortunately it was a lengthy video. So let's complete those things in this video. So when we learned about list, it's very simple, right? We have a list of values. Now this list can be of different type. It can be of type integer or a string or float. Now we have another type which is called as tuple. Now what is tuple? So tuple is almost same as list. We can have a collection of value there. The difference is in list we can change value because list is mutable and tuple, you guessed it right, is immutable. That means you cannot change the value. Okay, but where it can be used? That's a different thing, you know. So let's try it out. First, let's create a tuple. So we'll say tup equal to and the way you create a tuple is We can use some brackets, right? At least we used to use a square bracket and we should specify values. There's a thing here. When we use a square bracket, see nowhere we are mentioning that we are going to use list, right? So by default, this square bracket means list. How do we use tuple? Because we cannot use square bracket now. So we have to use a round bracket. So round bracket you find it's a tuple. 21, 36, 14, 25. Now, once you specify these values here, you cannot change it. So, we got our tuple. If I say enter, you can see we got 21, 36, 14, and 25. Now, let's try to change the value. Let's try it. So, I will say tup. Can I use square bracket? First, let me fetch the value. So, I will say 1. Enter. It works. Okay. So, when you want to fetch values, we can use square bracket. Okay. Let's try to change the value. So, I will say 1. Tube of one is equal to I want to change from 36 to 33. I will say enter. You can see we got an error. It says tuple object does not support item assignment. That means you cannot basically change the value. Can we use certain methods which we had in list? Let's try. So if I say tup dot control space, you can see we have two methods. One is count and second is index. Okay, if you're getting confused, what is count? Count was there in least as well. So count will count the number of occurrence of a number. Example, let's say in your tuple, you have 10 values and out of which you have some values like five, which is repeated two times. So if you say five, it will give you answer two. And you can specify, you can fetch the index value for a particular element. But you don't have any method which will replace the value which will append a value. You can see you don't have those things here. Tuple is like list but then you cannot change the value of it. Now when you should be using tuple? So when you know that you have a list and you don't want to change the values of it and yes in certain projects we have this requirement. You want to set a values and you don't want to change it. So of course you will go for tuple there. Since we don't change values in tuple, So iteration in tuples is faster than in list. So that's where you can use tuple if you want to enhance the speed of your execution. So that's about tuple. Now it's time to look at set. So what is set? Now, if you remember the mathematical definition of set, it is simply a collection of elements. It is a collection of unique elements. Okay. So let's try. So I will say set. I will say yes. S equal to. How do I define set? Because the square bracket is for list. Round bracket is for tuple. We are remaining with only one bracket now, which is curly bracket. So curly bracket is for set. In fact, in math as well, I guess we use curly brackets. So now I will say 22, 25, 14, 21. Comma, let's say five. So I got these values here. Now these are random values, no specific sequence. So these are not sorted. Now there's one more thing with set, you know, first of all, I will say enter. You can see we got a set. I will not be printing it now. So let's talk about a concept here. Now set never follows a sequence. Okay, so it will not follow the sequence in which you have added those elements. Now, if you think if I type S and if I say enter, you will get the same values. Yes, you will get the same values, but not in the same sequence. OK, that's important here. Let's enter. You can see the values. We got 5, 14, 21, 22 and 25. I know what you're thinking. You might be thinking this is sorted. No, not exactly. Let me just try once again. I will say S equal to. again a curly brackets and then 25 comma 14 comma 98 comma 63 okay I'm going for random values okay I'm not I don't have these values in my mind I'm just looking at my keyboard and just hitting those numbers and I will also try to repeat some values here because last time I forgot to repeat the value this time I will do it I will let me repeat 98 yeah now if I say enter and if I print the value of s look at the sequence first of all we got 98 only once and second look at the sequence there is no ascending or descending order it is random values so you cannot be sure with the sequence in set the reason is set uses a concept of hash and using hash we we improve the performance we want to fetch the element as fast as possible so it simply says okay just go with this go with the flow so we want speed then sequence here but yes if you want to make a sequence you can just do certain things like sorting elements and all those stuff But here, set will not for the sequence. Now, can we change the value of set? Let's try. Okay, first of all, can we use index number here? If I say 2, you can see in set, indexing is not supported. You know why? Because we don't have proper sequence, right? What you will do with the indexing? So, yes, in set, we cannot do that. Can we change the value? That's an issue, right? So, if you say s dot, you can see our method called s add, using which you can add the value. And then, what else we can use? We can use pop. We can use remove. So this thing works in set, you know, so you can you can try it out. You can do some experiment and it should work. So that's how we use set. So we have tuple and we have set. So let me just go for a quick recap. So we have a list. List is like a collection of values. Now, this can be of different type or of same type. Doesn't matter. And we have a concept where we can add values. We can remove values. We can update values. Can we? Yes, we can. So then we have tuples where tuples is same as least. The difference would be in tuples, you cannot change the values because they are immutable. And then we have set. Now set is almost same as least. The difference would be it will not maintain the sequence and it will not support duplicate values. So that's important, right? So that's least, tuple, and set for you. So I hope you enjoyed this session. Let me know in the comment section and do click on the like button if you're enjoying it. Thank you so much for watching, everyone. Bye-bye.
