Tuple is like list but unlike in structure and functionality. first basic difference is can be figured out by the structure. List elements are enclosed with '[]' brackets but tuple elements are enclosed with '()' parentheses.
Few properties of tuple:-
1. It is faster than list.
2. Unlike list, tuple is immutable, i.e. tuple's data cannot be changed once created, we should use tuple if we want our data to be safe from accidental change. As you can see in title picture, you
are allowed to see but cannot touch it.
3. We can use tuple as key in dictionaries whereas this is not the case with list, we can’t use list as key in dictionary <Link this post (dictionary) will be available on 16 March
2018>.
Examples:-
Few properties of tuple:-
1. It is faster than list.
2. Unlike list, tuple is immutable, i.e. tuple's data cannot be changed once created, we should use tuple if we want our data to be safe from accidental change. As you can see in title picture, you are allowed to see but cannot touch it.
Examples:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | myTuple=tuple('Python') # it takes only one parameter print(myTuple) #it will break down each character into separate elements, just like list, check the output. myTyple_2 = ('1','2',3,4,'5') print(myTyple_2) #Can hold multiple data type. list_1 = ["This","is","PythonHunter"] myTuple=tuple(list_1) # it can takes list as parameter print(myTuple) print(myTuple[0],myTuple[1],myTuple[2]) myTuple[0] = "changedValue" # this piece of code will give you error as tuple can not be changed. list_1[0] = "changedValue" # this works fine as the list's element can be altered. print(list_1) |
Output:-
Explanation:-
"PythonHunter" ignore this, as this is the output of previous code (he he heeeee).
please accept my apologies, lets start from second line...
myTuple=tuple('Python') break downs each character into elements.
As you can see the output in red color this is an exception (inserted intentionally) to show you that the tuple can not be altered at run time. This exception comes from code line number 14 myTuple[0] = "changedValue",but code line number 16 (list_1[0] = "changedValue") gives no error as we are altering List and lists are flexible and can be altered at run time and this code works fine.
comment the code line number at 14 for skipping the error.
Tuples and lists are same to each other in other properties like list can hold tuple object as its element and vice versa tuple can hold a list object as its element, and traversing (picking up elements and nested elements) are as same as list. if you understand the list object you don't need to waste you time to understand the tuple object. Only difference is that tuples cannot be altered at run time.
Example:-
1 2 3 4 5 6 7 | #list object containing tuple as its element crapList = ['1','2','3',('Hello','Python','Hunter')] print(crapList[3]) #tuple object containing list as its element example = ('1','2',3,4,'5',('nested','tuple','object'),['list','Object']) print(example[6][1]) |
That's it for now, various other other operation on List and tuple e.g Slicing, copy, deep Copy, shallow Copy, nesting, pit falling are described under this blog.
This post is just about to getting to know about tuple, various operation are described under this link.
ReplyDelete