Transcript for:
Understanding Tuples in Python

foreign onwards we will understand tuples in details this lecture is the introduction lecture to tuples so without any further delay let's get started the topics of this presentation are introduction to tuples Tuple with one item length of a tuple and the Tuple Constructor let's understand these topics one by one let's start with the first topic that is Introduction to tuples so what is a tuple a tuple is a collection of items which are indexed ordered and immutable we will understand the meaning of these terms as we proceed with an example right now you just need to understand that a tuple is a collection of items just like a list but there is a difference we will understand that difference the syntax is as follows first we need to specify the name of the Tuple then the equal sign which is the assignment operator then Within These parentheses we can specify as many items as we want so the visual difference between a tuple and a list is that a list is always wrapped within square brackets on the other hand a tuple is always wrapped within round brackets now let's understand the meaning of these three terms with the help of an example let's open our Command Prompt and let's activate the python interactive shell now let's type this command cars equal to Audi Mercedes BMW so we have a total of three cars in this Tuple now let's hit enter and type cars to verify that the Tuple is created let's hit enter again we are getting this Tuple with three items OD Mercedes and BMW this is what we intended to do now let's understand the meaning of indexed first the Tuple items are indexed this means that each item has an index this item has index 0 this item has index 1 and this item has index two so each item in a tuple has an index so we can access an item using its index that is why tuples are called indexed now what about immutable we will understand ordered in a moment but let's first understand what is the meaning of immutable tuples are immutable which means that tuples are unchangeable we cannot make any change to a tuple to verify this that tuples are immutable let's try to replace this item Audi with Ferrari for this we will type this command cars 0 equal to Ferrari here within square brackets we can specify the index here the index is 0 which means that we are trying to access this item and with the help of this assignment operator we want to replace this item with this item Ferrari is it possible to do this let's hit enter and check this after hitting enter we will get type error double object does not support item assignment this means we can not update a tuple we cannot replace any item of a tuple we can access double items but we cannot add remove or change any item of a tuple so it is clear that tuples are immutable now what about ordered once the order is defined it remains exactly the same if you have decided to place Audi first then Mercedes then BMW then the order will remain same throughout its lifetime we cannot change this order later this is the reason why tuples are ordered I hope with this all these terms are clear and the definition is also clear a tuple is a collection of items which are indexed ordered and immutable now let's move on to the next point a tuple can have duplicates so we are allowed to have duplicates within a tuple in order to verify this let's consider one simple example this time let's type cars equal to Audi Mercedes Mercedes BMW Mercedes is repeated twice in this Tuple and it is perfectly fine if you hit enter and type cars and again if we hit enter we will get this Tuple with Mercedes repeated twice I hope with this it is also clear that we can have duplicates in a tuple recall that we cannot have duplicates in a dictionary we cannot have duplicate key value pairs but we can duplicate items in a list so just like list we can duplicate items in a tuple two now we are done with the introduction to tuples let's move on to the next topic which is Tuple with one item let's understand how to create a tuple with just a single item in order to create a tuple with one item we need to follow the syntax first we can specify the name of the Tuple then the equal sign and within parentheses we can specify the item that we want just one single item and after this it is mandatory to put the comma without comma python will never recognize this as a tuple let's consider one example now this time let's type car equal to Audi after this string or D I have added comma as well this comma is important now let's hit enter and type car let's hit enter again python has recognized this as Tuple now let's try to create a tuple with single item without comma let's see what happens in that case let's type car equal to or D So within parentheses just this string Audi there is no comma after the string now let's hit enter and type car let's hit enter again this is problematic we want the Tuple with one item Audi but we are getting this string Audi so python is unable to recognize this as the Tuple so it is mandatory to put comma after the item I hope with this it is clear how to create a tuple with just one item now let's move on to the next topic that is length of a tuple we can easily determine the length of a tuple by using the Len function so length of a tuple can be determined using the Len function how let's see one example let's open our Command Prompt and let's type cars equal to OD Mercedes BMW in this Tuple there are a total of three items the Len function also gives us the same result we will get 3 as the result let's see how let's hit enter now and type Len cars within parentheses we have to pass cars now let's hit enter we will get 3 as the result so with this we are done with this topic Also let's move on to the next topic which is the Tuple Constructor the Tuple Constructor provides us an alternative way to create a tuple now let's try to create the same Tuple cars with the help of the Tuple Constructor for this we will again open our Command Prompt and type this command cars equal to Tuple within parentheses we need another pair of parentheses and Within These parentheses we can specify the three items that we want in our cars Tuple so remember we need two pairs of parentheses now let's hit enter and type cars let's again hit enter we will get the same Tuple with three items or the Mercedes and BMW so the Tuple Constructor provides us the alternative way to create a tuple so now we are done with all the topics of this presentation I hope these topics are clear okay friends this is it for now thank you for watching this presentation I will see you in the next one [Music] [Applause] [Music]