Python Tutorial for Beginners 14 - Python Tuples

 


hey guys welcome to the next blog on Python tutorial for beginners in this blog we are going to see how to use tuples in Python so first of all what are tuples so tuples are very similar to lists that is they are used to store the collection of elements in a single variable but there is a very important difference between them and that difference is tuples are immutable immutable means once tuples are created they cannot be changed or the content in them cannot be changed so let's see how we can declare a couple so I'm going to declare a variable called X and I'm going to assign some values to it now in case of list we have seen we used the square brackets in the case of tuples we use these parentheses in order to store the list of elements so I can just write 1 5 3 4 and 8 for example and then press ENTER and it's going to create a tuple now in order to print the tupple you can use x and it's going to print this table now as I said tuples are similar to lists in some ways and that means you can call a tuple element by index in tupple so all the tuple elements are arranged by their index and when you want to for example get the first element here you give the index 0 and then press ENTER and it's going to give you the value which is saved at index 0 let's get the value which is saved at index 4 and I am going to press ENTER and you can see it gives 8 which is saved at index 4 now when I try to give an index which is not there for example 100 it's going to give us error which says index error tuple index out of range now as I said tuples are immutable that is you cannot change the content of a tuple if they are initialized once so let's try to test this so I'm going to just sign the value at index zero which is let's say two and I'm going to press ENTER and you can see it says type error tuple object does not support item assignment that is you cannot assign any other value to a tuple element if they are once initialized now that also means that you cannot use some of the functions which you used with list in the tuples for example you cannot remove any element you cannot append any element you cannot change any element so for example when I write X here and then press dot you can see there are few methods which are available with the tupple and most important is this count method so let's try to use this count method and I want to know how many number of eight which are there in this tuple and it will give me one because eight is only present once in this couple if it is present twice in this couple here you will see the result too now in order to get the length of the tuple you can use the inbuilt function which is a Lian and then the tupple name and then press ENTER and it's going to give you the length of the tuples now in the previous blog I have told you that you can save multiple data type values in lists let's see if we can do the same with tuples or not so I'm going to give the parentheses I'm going to give the first value as integer second value as string third value as for example the decimal value and I'm going to press ENTER and I'm going to get the value of y and you can see it's totally possible to save multiple data type values in our table so now I have declared two tuples one is X and other is why so let's print X and Y's once again very quickly and now what we want to do is we want to use a concatenation operators so as I said tuples are immutable but let me define a third tuple here and I want to add Exce tapu and white apple here and i want to assign the value to z is it possible let's check so I'm going to just once again see the contents in the Z variable and you can see when you use this plus operator or concatenation operator it's going to join these two tupple and create a new tuple with the combination of the values of both these tuples now there is one interesting thing which you can do with a tuple which is you can declare a variable and let me define a tuple with only one element and I'm going to just give high here and then after this element I'm going to provide comma here so this is important I have provided this comma and I haven't provided the second element in the tuple and I can use an S tricks here and I can use some value here so let's say I have provided five here and I'm going to press ENTER and then I'm going to see the values inside this a variable and now you can see hi string is saved five times in a variable in the form of a tuple so you can once again get this value at index two for example and it's going to give you the value which is stored at index two which is high itself you can also use inbuilt function called Max here to get the maximum value out of a tuples so Max and I have provided X couple here and you will be able to see it will give us the maximum value stored in that tuples and you can see here it is the maximum value which is there inside the tupple we can also get the minimum value using mi and function and it's going to give you the minimum value which is stored inside the tuple and also you can use our de l function to delete our tuples so let's say I want to delete this tuple which is Z here so I can just write the name of the tuple after the ELN space and then press enter and now once again when I try to get the value of said it's going to give me error which says name is not defined so in order to delete the tupple you can use that de l function so this is how you can use tuples in Python I hope you have enjoyed this blog and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News