Python_notes_list_tuple
Python_notes_list_tuple
==============================================
1. list
==============================================
Index:
=>Properties of list
=>Types of list
a) emty list
b) non-empty list
=>Operations on list
=>Pre-defined functions in list
=>Nested List / Inner List
=>Programming Examples
=========================================================
Properties of list
=>'list' is one of the pre-defined class and treated as list data type.
------------------------------------------------------------------------------------------------
=>The purpose of list data type is that " To store Multiple Values either of same
Type OR Different Type OR Both the Types in Single Object with Unique and
Duplicate Values".
------------------------------------------------------------------------------------------------
=>The Values / Elements of list must be stored / Organized with Square
Brackets [ ] and Values must be separated by comma.
------------------------------------------------------------------------------------------------
=>An object of list maintains Insertion order.
------------------------------------------------------------------------------------------------
=>On object of list, we can perform Both Indexing and Slicing Operations.
------------------------------------------------------------------------------------------------
=>An object of list belongs to Mutable
------------------------------------------------------------------------------------------------
=>W.r.t list class, we can create 2 types of list objects. They are
a) Empty List
b) Non-Empty List
------------------------------------------------------------------------------------------------
a) Empty List:
=>An empty list is one, which does not contain any Elements and whose length
is 0.
=>Syntax: varname=[]
(OR)
varname=list()
------------------------------------------------------------------------------------------------
b) Non-Empty List:
=>An Non-Empty list is one, which contains Elements and whose length is >0.
=>Syntax: varname=[Val1,Val2.....Val-n]
(OR)
varname=list(object)
------------------------------------------------------------------------------------------------
Examples:
>>> l1=[10,20,30,10,40,50,20,10]
>>> print(l1,type(l1))----------------[10, 20, 30, 10, 40, 50, 20, 10] <class 'list'>
>>> l2=[20,"Rossum",34.56,True,2+3j]
>>> print(l2,type(l2))---------------------[20, 'Rossum', 34.56, True, (2+3j)]
<class 'list'>
>>> l2[0]----------------20
>>> l2[-1]-------------(2+3j)
>>> l2[1:4]-----------['Rossum', 34.56, True]
>>> l2[::2]--------------[20, 34.56, (2+3j)]
>>> l2[::-1]-----------[(2+3j), True, 34.56, 'Rossum', 20]
---------------------------
>>> l2=[20,"Rossum",34.56,True,2+3j]
>>> print(l2,type(l2),id(l2))--------[20, 'Rossum', 34.56, True, (2+3j)] <class
'list'> 2705927635008
>>> l2[0]=30
>>> print(l2,type(l2),id(l2))----[30, 'Rossum', 34.56, True, (2+3j)] <class 'list'>
2705927635008
>>> l2[2:4]=[44.55,False]
>>> print(l2,type(l2),id(l2))----[30, 'Rossum', 44.55, False, (2+3j)] <class 'list'>
2705927635008
---------------------------------------
>>> l1=[10,"Rossum",34.56]
>>> print(l1,type(l1),len(l1))----------------[10, 'Rossum', 34.56] <class 'list'>
3
>>> l2=[]
>>> print(l2,type(l2),len(l2))--------------- [] <class 'list' > 0
OR
>>> l3=list()
>>> print(l3,type(l3),len(l3))-------------- [] <class 'list'> 0
-------------------------------------
>>> l1=[10,20,30,40,50,60]
>>> print(l1,type(l1))-------------------------[10, 20, 30, 40, 50, 60] <class 'list'>
>>> b=bytes(l1)
>>> print(b,type(b))-------------------------b'\n\x14\x1e(2<' <class 'bytes'>
>>> l2=list(b)
>>> print(l2,type(l2))---------------------[10, 20, 30, 40, 50, 60] <class 'list'>
>>> s="MISSISSIPPI"
>>> l3=list(s)
>>> print(l3,type(l3))-----['M', 'I', 'S', 'S', 'I', 'S', 'S', 'I', 'P', 'P', 'I'] <class 'list'>
------------------------
>>> a=[10]
>>> print(a,type(a))------------------[10] <class 'list'>
>>> x=100
>>> b=list(x)------------------TypeError: 'int' object is not iterable
To solve the above Error, Use the following
>>> b=list([x])
>>> print(b,type(b))--------------------[100] <class 'list'>
>>> b=list(x,) ------------TypeError: 'int' object is not iterable
------------------------------------------------------------------------------------------------
==================================================
Pre-defined functions in list
==================================================
=>We know that, on the object of list, we can perform Both Indexing and
Slicing Operations.
------------------------------------------------------------------------------------------------
=>Along with Indexing and Slicing Operations, we can Perform Various
Operations by using Pre-Defined Functions present in list object. They are
------------------------------------------------------------------------------------------------
1. append()
------------------------------------------------------------------------------------------------
Syntax: listobj.append(Value)
=>This Function is used for adding the values to list object at end.
Example
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst))----[10, 'Rossum', 34.56] 2705927639808
>>> lst.append("PYTHON")
>>> print(lst,id(lst))----[10, 'Rossum', 34.56, 'PYTHON'] 2705927639808
>>> lst.append(True)
>>> lst.append(1+2.5j)
>>> print(lst,id(lst))----[10, 'Rossum', 34.56, 'PYTHON', True, (1+2.5j)]
2705927639808
------------------------------------------------------------------------------------------------
2. insert()
Syntax: listobj.insert(Index,Value)
=>This Function is used for adding the value to list object at Specified Index
Note: -
=>When we enter Invalid Positive Index then the value inserted at Last/End of
List object
=>When we enter Invalid Negative Index then the value inserted at First of List
object
------------------------------------------------------------------------------------------------
Examples
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst))---------[10, 'Rossum', 34.56] 2705923376704
>>> lst.insert(2,"PYTHON")
>>> print(lst,id(lst))-----------[10, 'Rossum', 'PYTHON', 34.56] 2705923376704
>>> lst[-1]=44.56
>>> print(lst,id(lst))--------[10, 'Rossum', 'PYTHON', 44.56] 2705923376704
>>> lst.insert(2,True)
>>> print(lst,id(lst))-----[10, 'Rossum', True, 'PYTHON', 44.56]
2705923376704
>>> lst.insert(-1,2+3j)
>>> print(lst,id(lst))-----[10, 'Rossum', True, 'PYTHON', (2+3j), 44.56]
2705923376704
-------------------------------------
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst))-----------[10, 'Rossum', 34.56] 2705927639296
>>> lst.insert(10,"PYTHON")
>>> print(lst,id(lst))-----------[10, 'Rossum', 34.56, 'PYTHON'] 2705927639296
>>> lst.insert(-10,"HYD")
>>> print(lst,id(lst))----------['HYD', 10, 'Rossum', 34.56, 'PYTHON']
2705927639296
------------------------------------------------------------------------------------------------
1. clear()
Syntax: listobj.clear()
=>This Function is used for Removing all the elements of Non-Empty List
object
=>When we call clear() on empty list object then we get No Output / None
Examples:
------------------
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst),len(lst))---------------[10, 'Rossum', 34.56] 2705927639808 3
>>> lst.clear()
>>> print(lst,id(lst),len(lst))------------ [] 2705927639808 0
----------------------------------
>>> lst.clear() -------------- No Ouput
(OR)
>>> print(lst.clear())----------None
>>> [].clear()-------------------No Output
(OR)
>>> print([].clear())-------------None
>>> print(list().clear())----------None
------------------------------------------------------------------------------------------------
4. remove () ----Based on Value
------------------------------------------------------------------------------------------------
=>Syntax: listobj.remove(Value)
=>This Function is used for Removing the First Occurence of Specified
Element of list object.
=>If the Specified Element does not exist in list object then we get ValueError.
------------------------------------------------------------------------------------------------
Examples:
>>> lst=[10,"Rossum",34.56,"Python"]
>>> print(lst,len(lst))--------------------[10, 'Rossum', 34.56, 'Python'] 4
>>> lst.remove("Rossum")
>>> print(lst,len(lst))------------------[10, 34.56, 'Python'] 3
>>> lst.remove(34.56)
>>> print(lst,len(lst))--------------------[10, 'Python'] 2
>>> lst.remove("Python")
>>> print(lst,len(lst))------------------[10] 1
>>> lst.remove(10)
>>> print(lst,len(lst))------------------[] 0
>>> lst.remove("Python")--------------ValueError: list.remove(x): x not in list
>>> list().remove(100)-------------------ValueError: list.remove(x): x not in list
---------------------------------------
>>> lst1=[10,20,30,10,30,"Python",True]
>>> print(lst1,len(lst1))---------------[10, 20, 30, 10, 30, 'Python', True] 7
>>> lst1.remove(10)
>>> print(lst1,len(lst1))--------------[20, 30, 10, 30, 'Python', True] 6
>>> lst1.remove(10)
>>> print(lst1,len(lst1))--------------[20, 30, 30, 'Python', True] 5
>>> lst1.remove(30)
>>> print(lst1,len(lst1))--------------[20, 30, 'Python', True] 4
>>> lst1.remove(30)
>>> print(lst1,len(lst1))--------------[20, 'Python', True] 3
------------------------------------------------------------------------------------------------
5. pop(index)----Index Based
------------------------------------------------------------------------------------------------
=>Syntax: listobj.pop(index)
=>This Function is used for Removing the Element of listobj based on Index.
=>If the Index is invalid then we get IndexError
Examples:
>>> lst1=[10,20,30,10,30,"Python",True]
>>> print(lst1)--------------------------[10, 20, 30, 10, 30, 'Python', True]
>>> lst1.pop(3)-------------------------10
>>> print(lst1)--------------------------[10, 20, 30, 30, 'Python', True]
>>> lst1.pop(-4)-----------------------30
>>> print(lst1)--------------------------[10, 20, 30, 'Python', True]
>>> lst1.pop(0)------------------------10
>>> print(lst1)--------------------------[20, 30, 'Python', True]
>>> lst1.pop(0)------------------------20
>>> print(lst1)------------------------[30, 'Python', True]
>>> lst1.pop(0)----------------------30
>>> print(lst1)-------------------------['Python', True]
>>> lst1.pop(0)-----------------------'Python'
>>> print(lst1)------------------------[True]
>>> lst1.pop(0)-----------------------True
>>> print(lst1)-------------------------[]
>>> lst1.pop(0)--------------------------IndexError: pop from empty list
>>> list().pop(-4)------------------------IndexError: pop from empty list
>>> list().pop(4)-------------------------IndexError: pop from empty list
------------------------------------------------------------------------------------------------
6. pop()
------------------------------------------------------------------------------------------------
Syntax: listobj.pop()
=>This Function is used for Removing always Last Element of List object.
=>If we call pop() on empty list object then we get IndexError
Examples:
>>> lst=[10,"Rossum",34.56,"Python"]
>>> print(lst)----------------------------[10, 'Rossum', 34.56, 'Python']
>>> lst.pop()--------------------------'Python'
>>> print(lst)---------------------------[10, 'Rossum', 34.56]
>>> lst.pop()-------------------------34.56
>>> print(lst)-------------------------[10, 'Rossum']
>>> lst.pop()--------------------------'Rossum'
>>> print(lst)-------------------------[10]
>>> lst.pop()--------------------------10
>>> print(lst)-------------------------[]
>>> lst.pop()--------------------------IndexError: pop from empty list
---------------------------------------------------------------------------
>>> list().pop()----------------IndexError: pop from empty list
>>> [].pop()---------------------IndexError: pop from empty list
------------------------------------------------------------------------------------------------
NOTE: del operator--Most Imp
Syntax1: del objname[Index]---->Removing the element based on Index
Syntax2: del objname[Begin:End:Step]--->Removing the Elements Based in
Slicing Operations
Syntax3: del objname--------->Removing the entire object
Examples
>>> lst=[10,"Sagatika",66.66,"OUCET","HYD"]
>>> print(lst,type(lst),id(lst))-----------[10, 'Sagatika', 66.66, 'OUCET', 'HYD']
<class 'list'> 2302481486656
>>> del lst[-2]
>>> print(lst,type(lst),id(lst))----------[10, 'Sagatika', 66.66, 'HYD'] <class 'list'>
2302481486656
>>> del lst[0:2]
>>> print(lst,type(lst),id(lst))---------[66.66, 'HYD'] <class 'list'>
2302481486656
>>> lst=[10,"Sagatika",66.66,"OUCET","HYD"]
>>> del lst[::2]
>>> print(lst,type(lst),id(lst))--------['Sagatika', 'OUCET'] <class 'list'>
2302485776384
>>> del lst
>>> print(lst,type(lst),id(lst))-----NameError: name 'lst' is not defined.
------------------------------------------------------------------------------------------------
7) index()
------------------------------------------------------------------------------------------------
Syntax: listobj.index(Value)
=>This Function is used for Finding Index of First Occurence of Specified
Element in List object.
=>if the Specified Element not present in List object then we get ValueError.
Examples:
>>> lst=[10,20,30,40,10,60,70,10,30]
>>> print(lst)-------------[10, 20, 30, 40, 10, 60, 70, 10, 30]
>>> lst.index(10)---------0
>>> lst.index(20)---------1
>>> lst.index(30)----------2
>>> lst.index(300)----------ValueError: 300 is not in list
>>> list().index(10)---------ValueError: 10 is not in list
>>> [].index(-12)-------------ValueError: -12 is not in list
------------------------------------------------------------------------------------------------
8) copy()----Shallow Copy
------------------------------------------------------------------------------------------------
=>This Function is Used for Copying the content of One Object into another
Object ( Implements Shallow Copy).
=>Syntax: Listobject2=listobj1.copy()
Examples:
>>> lst1=[10,"Rossum",34.56]
>>> print(lst1,type(lst1),id(lst1))------[10, 'Rossum', 34.56] <class 'list'>
2302481827584
>>> lst2=lst1.copy() # Shallow Copy
>>> print(lst2,type(lst2),id(lst2))----[10, 'Rossum', 34.56] <class 'list'>
2302481486656
>>> lst1.append("PYTHON")
>>> print(lst1,type(lst1),id(lst1))---[10, 'Rossum', 34.56, 'PYTHON'] <class
'list'> 2302481827584
>>> print(lst2,type(lst2),id(lst2))---[10, 'Rossum', 34.56] <class 'list'>
2302481486656
>>> lst2.append("NL")
>>> print(lst2,type(lst2),id(lst2))--[10, 'Rossum', 34.56, 'NL'] <class 'list'>
2302481486656
>>> print(lst1,type(lst1),id(lst1))--[10, 'Rossum', 34.56, 'PYTHON'] <class
'list'> 2302481827584
Deep Copy--Examples
>>> lst1=[10,"Rossum",34.56]
>>> print(lst1,type(lst1),id(lst1))---------[10, 'Rossum', 34.56] <class 'list'>
2302481517760
>>> lst2=lst1 # Deep Copy
>>> print(lst2,type(lst2),id(lst2))---[10, 'Rossum', 34.56] <class 'list'>
2302481517760
>>> lst1.append("HYD")
>>> print(lst1,type(lst1),id(lst1))---[10, 'Rossum', 34.56, 'HYD'] <class 'list'>
2302481517760
>>> print(lst2,type(lst2),id(lst2))---[10, 'Rossum', 34.56, 'HYD'] <class 'list'>
2302481517760
>>> lst2.remove("Rossum")
>>> print(lst1,type(lst1),id(lst1))---[10, 34.56, 'HYD'] <class 'list'>
2302481517760
>>> print(lst2,type(lst2),id(lst2))---[10, 34.56, 'HYD'] <class 'list'>
2302481517760
------------------------------------------------------------------------------------------------
9. count()
------------------------------------------------------------------------------------------------
=>Syntax: listobj1.count(Value)
=>This Function is used for finding / Counting Number of occurrences of
Specified Value of List object.
=>If the Specified Value does not exist in list object, then we get 0 as a Result.
Examples
>>> lst=[10,20,30,10,20,50,60,70,10]
>>> print(lst)-----------[10, 20, 30, 10, 20, 50, 60, 70, 10]
>>> lst.count(10)-------3
>>> lst.count(20)-------2
>>> lst.count(30)-------1
>>> lst.count(40)-------0
>>> lst.count("PYTHON" )-----0
>>> list().count(10)----------0
>>> [].count(10)-------------0
------------------------------------------------------------------------------------------------
10. reverse()
------------------------------------------------------------------------------------------------
Syntax: listobj.reverse()
=>This Function is used for reversing(Front elements to back and back elements
to Front) the elements of list object in same list itself.
------------------------------------------------------------------------------------------------
Examples:
>>> lst1=[10,"Rossum",34.56,"PYTHON"]
>>> print(lst1,id(lst1))-----------[10, 'Rossum', 34.56, 'PYTHON']
2302485781376
>>> lst1.reverse()
>>> print(lst1,id(lst1))-----------['PYTHON', 34.56, 'Rossum', 10]
2302485781376
-----------------------
>>> lst2=[10,20,30,100,200,300]
>>> print(lst2,id(lst2))--------[10, 20, 30, 100, 200, 300] 2302485776384
>>> lst2.reverse()
>>> print(lst2,id(lst2))-------[300, 200, 100, 30, 20, 10] 2302485776384
---------------------------------------
>>> lst2=[10,20,30,100,200,300]
>>> lst3=lst2.reverse()
>>> print(lst2,id(lst2))---------[300, 200, 100, 30, 20, 10] 2302481517760
>>> print(lst3)--------------None
>>> list().reverse() # No Output
(OR)
>>> print(list().reverse())--------None
------------------------------------------------------------------------------------------------
11) extend()
------------------------------------------------------------------------------------------------
Syntax: listobj1.extend(listobj2)
=>This Function is used for Merging / Combining The values of listobj2 with
ListObj1. Hence Listobj1 contains Its Own Elements and Elements of listobj2.
Syntax: listobj1=listobj1+ listobj2+ ........+listobj-n
=>By using + Operator also we can Merge OR Combine Multiple elements of
list objects
Examples:
>>> lst1=[10,20,30,40]
>>> lst2=["Python","Java"]
>>> lst3=["Rossum","Gosling"]
>>> lst1.extend(lst2,lst3)------------TypeError: list.extend() takes exactly one
argument (2 given)
#### TO Solve the above Error
>>> lst1.extend(lst2)
>>> lst1.extend(lst3)
>>> print(lst1)-------[10, 20, 30, 40, 'Python', 'Java', 'Rossum', 'Gosling']
------------------------------
OR
------------------------------
>>> lst1=[10,20,30,40]
>>> lst2=["Python","Java"]
>>> lst3=["Rossum","Gosling"]
>>> lst1=lst1+lst2+lst3 # Used + Operartor for Merging
>>> print(lst1)-----[10, 20, 30, 40, 'Python', 'Java', 'Rossum', 'Gosling']
------------------------------------------------------------------------------------------------
12) sort()
------------------------------------------------------------------------------------------------
Syntax1: listobj.sort()----->Sorts the given List data in Ascending Order
Syntax2: listobj.sort(revserse=False)----->Sorts the given List data in
Ascending Order
Syntax3: listobj.sort(reverse=True)--->Sort the given List data in Descending
Order
Examples:
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))--------[10, -2, 12, 56, 13, -7, 45, 6] 2302485781696
>>> lst.sort()
>>> print(lst,id(lst))------[-7, -2, 6, 10, 12, 13, 45, 56] 2302485781696
>>> #------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))------------[10, -2, 12, 56, 13, -7, 45, 6] 2302481486656
>>> lst.sort()
>>> print(lst,id(lst))---------[-7, -2, 6, 10, 12, 13, 45, 56] 2302481486656
>>> lst.reverse()
>>> print(lst,id(lst))----------[56, 45, 13, 12, 10, 6, -2, -7] 2302481486656
>>> #-----------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))----------[10, -2, 12, 56, 13, -7, 45, 6] 2302485781696
>>> lst.sort(reverse=True)
>>> print(lst,id(lst))--------[56, 45, 13, 12, 10, 6, -2, -7] 2302485781696
>>> #------------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))----------[10, -2, 12, 56, 13, -7, 45, 6] 2302481486656
>>> lst.sort(reverse=False)
>>> print(lst,id(lst))-----------[-7, -2, 6, 10, 12, 13, 45, 56] 2302481486656
>>> #-----------------------------------------------
>>> lst=["Trump","Zaki","Biden","Putin","Rossum","Alen"]
>>> print(lst)----------['Trump', 'Zaki', 'Biden', 'Putin', 'Rossum', 'Alen']
>>> lst.sort(reverse=True)
>>> print(lst)------------['Zaki', 'Trump', 'Rossum', 'Putin', 'Biden', 'Alen']
>>> #-------------------------------------------------
>>> lst=["Trump","Zaki","Biden","Putin","Rossum","Alen"]
>>> print(lst)----------['Trump', 'Zaki', 'Biden', 'Putin', 'Rossum', 'Alen']
>>> lst.sort()
>>> print(lst)-------['Alen', 'Biden', 'Putin', 'Rossum', 'Trump', 'Zaki']
>>> #---------------------------------------------------
>>> lst=[10,"Trump",33.33,2+3j,True]
>>> print(lst)---------[10, 'Trump', 33.33, (2+3j), True]
>>> lst.sort()----------TypeError: '<' not supported between instances of 'str'
and 'int'
==================================x======================
===============================================
Inner or Nested List
===============================================
=>The Process of Defining one list inside of another list is called Inner or
Nested List
=>Syntax:- listobj=[ Val1, Val2....[Val11,Val12...Val1n],
[Val21,Val22..Val2n...], Val-n ]
=>Here Val1,Val2...Val-n are called Values of Outer List
=>Here Val11,Val12...Val-1n are called Values of one Inner List
=>Here Val21,Val22...Val-2n are called Values of another Inner List
=>In inner list we can perform Both Indexing and Slicing Operations
=>On Inner List, we can apply all pre-defined function of list.
Examples:
>>> lst=[100,"Karthik",[18,16,20],[76,75,66],"OUCET"]
>>> print(lst)-----------------[100, 'Karthik', [18, 16, 20], [76, 75, 66], 'OUCET']
>>> lst[-1]--------------------'OUCET'
>>> lst[-2]------------------------[76, 75, 66]
>>> lst[-3]--------------------------[18, 16, 20]
>>> lst[1]-----------------------------------'Karthik'
>>> lst[0]-----------------------------------100
>>> print(lst[2],type(lst[2]))----------------[18, 16, 20] <class 'list'>
>>> print(lst[-2],type(lst[-2]))-------------[76, 75, 66] <class 'list'>
>>> lst[2][0]------------------------------18
>>> lst[2][1]------------------------------16
>>> lst[2][1]=17
>>> print(lst)-----------------[100, 'Karthik', [18, 17, 20], [76, 75, 66], 'OUCET']
>>> lst[2].append(16)
>>> print(lst)---------------[100, 'Karthik', [18, 17, 20, 16], [76, 75, 66],
'OUCET']
>>> lst[-2].insert(-2,80)
>>> print(lst)--------------[100, 'Karthik', [18, 17, 20, 16], [76, 80, 75, 66],
'OUCET']
>>> del lst[-2]
>>> print(lst)--------------[100, 'Karthik', [18, 17, 20, 16], 'OUCET']
>>> lst[2].clear()
>>> print(lst)----------------[100, 'Karthik', [], 'OUCET']
>>> del lst[2]
>>> print(lst)---------------------[100, 'Karthik', 'OUCET']
>>> lst.insert(2,[16,15,18,17])
>>> print(lst)--------------------------[100, 'Karthik', [16, 15, 18, 17], 'OUCET']
>>> lst.insert(-1,[77,66,78,55])
>>> print(lst)-----------------[100, 'Karthik', [16, 15, 18, 17], [77, 66, 78, 55],
'OUCET']
>>> lst[2].sort()
>>> print(lst)-------------------[100, 'Karthik', [15, 16, 17, 18], [77, 66, 78, 55],
'OUCET']
>>> lst[-2].sort(reverse=True)
>>> print(lst)--------------------[100, 'Karthik', [15, 16, 17, 18], [78, 77, 66, 55],
'OUCET']
Examples:
>>> studlist=[100,"DLPrince",[18,16,19],[78,66,79],"OUCET"]
>>> print(studlist,type(studlist))--[100, 'DLPrince', [18, 16, 19], [78, 66, 79],
'OUCET'] <class 'list'>
>>> print(studlist[2],type(studlist[2]))---[18, 16, 19] <class 'list'>
>>> print(studlist[-2],type(studlist[-2]))---[78, 66, 79] <class 'list'>
>>> studlist[-3].append(17)
>>> print(studlist,type(studlist))--[100, 'DLPrince', [18, 16, 19, 17], [78, 66,
79],] <class 'list'>
>>> studlist[3].append(68)
>>> print(studlist,type(studlist))---[100, 'DLPrince', [18, 16, 19, 17], [78, 66,
79, 68], 'OUCET'] <class 'list'>
>>> studlist[2].sort()
>>> print(studlist,type(studlist))---[100, 'DLPrince', [16, 17, 18, 19], [78, 66,
79, 68], 'OUCET'] <class 'list'>
>>> studlist[3].sort(reverse=True)
>>> print(studlist,type(studlist))----[100, 'DLPrince', [16, 17, 18, 19], [79, 78,
68, 66],'OUCET'] <class 'list'>
>>> studlist[2].pop(-2)----18
>>> studlist[-2].remove(68)
>>> print(studlist,type(studlist))--[100, 'DLPrince', [16, 17, 19], [79, 78, 66],
'OUCET'] <class 'list'>
>>> studlist[2].clear()
>>> print(studlist,type(studlist))--[100, 'DLPrince', [], [79, 78, 66], 'OUCET']
<class 'list'>
>>> studlist[2].append(14)
>>> print(studlist,type(studlist))---[100, 'DLPrince', [14], [79, 78, 66],
'OUCET'] <class 'list'>
>>> del studlist[2]
>>> del studlist[-2]
>>> print(studlist,type(studlist))---[100, 'DLPrince', 'OUCET'] <class 'list'>
>>> studlist.insert(2,[17,15,14,19])
>>> print(studlist,type(studlist))--[100, 'DLPrince', [17, 15, 14, 19], 'OUCET']
<class 'list'>
>>> studlist.append([66,78,65,76])
>>> print(studlist,type(studlist))---[100, 'DLPrince', [17, 15, 14, 19], 'OUCET',
[66, 78, 65, 76]] <class 'list'>
>>> studlist[-2]---'OUCET'
>>> len(studlist)----5
>>> len(studlist[2])---4
>>> len(studlist[-1])---4
===========================X==========================
=====================================================
II. TUPLE ()
=====================================================
Properties
=>'tuple' is one of the pre-defined class and treated as list data type
=>The purpose of tuple data type is that " To store Multiple Values either of
same Type OR Different Type OR Both the Types in Single Object with Unique
and Duplicate Values".
=>The Values / Elements of tuple must be stored / Organized with Braces ( )
and Values must be separated by comma.
=>An object of tuple maintains Insertion order.
=>On object of tuple, we can perform Both Indexing and Slicing Operations.
=>An object of tuple belongs to Immutable
=>W.r.t tuple class, we can create 2 types of tuple objects. They are
a) Empty tuple
b) Non-Empty tuple
a) Empty tuple:
=>An empty tuple is one, which does not contain any Elements and whose
length is 0.
=>Syntax: varname=()
(OR)
varname=tuple()
b) Non-Empty tuple:
=>A non-empty tuple is one, which contains Elements and whose length is >0.
=>Syntax: varname=(Val1,Val2.....Val-n)
(OR)
=>Syntax: varname=Val1,Val2.....Val-n
(OR)
varname=tuple(object)
------------------------------------------------------------------------------------------------
NOTE: The Functionality of tuple is exactly similar to Functionality of list but
list object belongs to Mutable and tuple object belongs to Immutable.
------------------------------------------------------------------------------------------------
Examples:
>>> t1= (10,20,30,10,50,60,70)
>>> print (t1, type(t1)) ----------------(10, 20, 30, 10, 50, 60, 70) <class 'tuple'>
>>> t2=(100,"Travis",33.33,"HYD")
>>> print (t2, type(t2)) -----------(100, 'Travis', 33.33, 'HYD') <class 'tuple'>
>>> len(t1) -----------7
>>> len(t2) ----------4
>>> t3=100,"Rossum",44.44,"PYTHON"
>>> print (t3, type(t3)) -----(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
-----------------------------------------------------------------------------------------------
>>> t1= ()
>>> print (t1, type(t1)) ------------() <class 'tuple'>
>>> len(t1) -------------------------0
>>> t2=tuple ()
>>> print (t2, type(t2)) -----------() <class 'tuple'>
>>> len(t2) ------------------------0
-----------------------------------------------------------------------------------------------
>>> t=(100,"Rossum",44.44,"PYTHON")
>>> print(type(t),id(t))--------(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
2605398318096
>>> t[0]--------------100
>>> t[1]--------------'Rossum'
>>> t[-1]------------'PYTHON'
>>> t[0]=200-----------Type Error: 'tuple' object does not support item
assignment
------------------------------------------------------------------------------------------------
>>> l1=[100,"Rossum",44.44,"PYTHON"]
>>> print(l1,type(l1))-----------[100, 'Rossum', 44.44, 'PYTHON'] <class 'list'>
>>> t1=tuple(l1)
>>> print(t1,type(t1))----------(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
>>> l2=list(t1)
>>> print(l2,type(l2))----------[100, 'Rossum', 44.44, 'PYTHON'] <class 'list'>
-----------------------------------------------------------------------------------------------
>>> t=(100,"Rossum",44.44,"PYTHON")
>>> print(t,type(t))-----------(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
>>> t[0:3]-----------(100, 'Rossum', 44.44)
>>> t[::-1]-------------('PYTHON', 44.44, 'Rossum', 100)
Special Points
(special Conversions)
>>> a=10
>>> t=tuple(a)--------------TypeError: 'int' object is not iterable
>>> t=tuple(a,)-------------TypeError: 'int' object is not iterable
>>> t=tuple((a))-------------TypeError: 'int' object is not iterable
>>> t=tuple([a])
>>> print(t,type(t))--------------(10,) <class 'tuple'>
--------OR-------------
>>> b=100
>>> t=(b,)
>>> print(t,type(t))-----------(100,) <class 'tuple'>
-----------------------------------------------------------------------------------------------
>>> b=12.34
>>> t=(b)
>>> print(t,type(t))------------12.34 <class 'float'>
>>> t=(b,)
>>> print(t,type(t))------------(12.34,) <class 'tuple'>
*----------------------------------------------X---------------------------------------------*
When we should go for tuple?
To maintain the immutable for given list
=========================================================
Pre-defined Function in tuple
=========================================================
=>We know that on the object of tuple we can perform Both Indexing and
Slicing Operations.
=>Along with these operations, we can also perform other operations by using
the following pre-defined Functions.
1)index ()
2)count ()
Examples:
>>> t1=(10,"RS",45.67)
>>> print(t1,type(t1))------------(10, 'RS', 45.67) <class 'tuple'>
>>> t1.index(10)---------0
>>> t1.index("RS")------1
>>> t1=(10,"RS",45.67)
>>> print(t1,type(t1))-------(10, 'RS', 45.67) <class 'tuple'>
>>> t1.count(10)-------1
>>> t1.count(100)------0
>>> t1=(10,0,10,10,20,0,10)
>>> print(t1,type(t1))---------(10, 0, 10, 10, 20, 0, 10) <class 'tuple'>
>>> t1.count(10)---------------4
>>> t1.count(0)-----------------2
>>> t1.count(100)--------------0
------------------------------------------------------------------------------------------------
MOST IMP:
sorted(): This Function is used for Sorting the data of immutable object tuple
and gives the sorted data in the form of list.
=>Syntax: listobj=sorted(tuple object)
------------------------------------------------------------------------------------------------
Examples:
>>> t1=(10,23,-56,-1,13,15,6,-2)
>>> print(t1,type(t1))------------(10, 23, -56, -1, 13, 15, 6, -2) <class 'tuple'>
>>> t1.sort()----------------------AttributeError: 'tuple' object has no attribute
'sort'
>>> x=sorted(t1)
>>> print(x,type(x))-----------[-56, -2, -1, 6, 10, 13, 15, 23] <class 'list'>
>>> print(t1,type(t1))----------(10, 23, -56, -1, 13, 15, 6, -2) <class 'tuple'>
>>> t1=tuple(x) # Converted sorted list into tuple
>>> print(t1,type(t1))---------(-56, -2, -1, 6, 10, 13, 15, 23) <class 'tuple'>
>>> t2=t1[::-1]
>>> print(t2,type(t2))------(23, 15, 13, 10, 6, -1, -2, -56) <class 'tuple'>
OR
>>> t1=(10,-4,12,34,16,-6,0,15)
>>> print(t1,type(t1))---------------------(10, -4, 12, 34, 16, -6, 0, 15) <class
'tuple'>
>>> l1=list(t1)
>>> print(l1,type(l1))-----------------[10, -4, 12, 34, 16, -6, 0, 15] <class 'list'>
>>> l1.sort()
>>> print(l1,type(l1))-------------------[-6, -4, 0, 10, 12, 15, 16, 34] <class 'list'>
>>> t1=tuple(l1)
>>> print(t1,type(t1))---------------(-6, -4, 0, 10, 12, 15, 16, 34) <class 'tuple'>
>>>t1=t1[::-1]
>>> print(t1,type(t1))----------------(34, 16, 15, 12, 10, 0, -4, -6) <class 'tuple'>
============================ x =======================
==================================================
Inner (OR) Nested tuple
==================================================
=>The Process of Defining One tuple in another tuple is called Inner or Nested
tuple
=>Syntax:- tplobj1=( Val1,Val2....(Val11,Val12....Val1n).....
(Val21,Val22...Val2n)..........Val-n )
=>Here (Val11,Val12....Val1n) is called One Inner OR Nested tuple
(Val21,Val22...Val2n) is called another Inner OR Nested tuple
=> ( Val1,Val2....(Val11,Val12....Val1n).....(Val21,Val22...Val2n)....Val-n ) is
called Outer tuple
=>All the pre-defined Functions of tuple can be applied on Inner or Nested
tuple.
=>On Inner or Nested tuple we can perform Index and Slicing Operations.
------------------------------------------------------------------------------------------------
Examples:
>>> sf=(10,"RS",(17,18,16),(78,66,79),"OUCET")
>>> print(sf,type(sf))------------(10, 'RS', (17, 18, 16), (78, 66, 79), 'OUCET')
<class 'tuple'>
>>> print(sf[0])-----10
>>> print(sf[2],type(sf[2]),type(sf))---------(17, 18, 16) <class 'tuple'> <class
'tuple'>
>>> print(sf[0:3])------------(10, 'RS', (17, 18, 16))
------------------------------------------------------------------------------------------------
>>> sf=(10,"RS",[17,18,16],(78,66,79),"OUCET")
>>> print(sf,type(sf))----------------(10, 'RS', [17, 18, 16], (78, 66, 79), 'OUCET')
<class 'tuple'>
>>> print(sf[2],type(sf[2]),type(sf))------------[17, 18, 16] <class 'list'> <class
'tuple'>
>>> sf[2].append(12)
>>> print(sf[2],type(sf[2]),type(sf))------------[17, 18, 16, 12] <class 'list'>
<class 'tuple'>
>>> print(sf,type(sf))---------(10, 'RS', [17, 18, 16, 12], (78, 66, 79), 'OUCET')
<class 'tuple'>
>>> sf[3].append(12)---AttributeError: 'tuple' object has no attribute 'append'
------------------------------------------------------------------------------------------------
>>> sf=[10,"RS",[17,18,16],(78,66,79),"OUCET"]
>>> print(sf,type(sf))-------------[10, 'RS', [17, 18, 16], (78, 66, 79), 'OUCET']
<class 'list'>
>>> print(sf[2],type(sf[2]),type(sf))--------[17, 18, 16] <class 'list'> <class 'list'>
>>> print(sf[3],type(sf[3]),type(sf))-------(78, 66, 79) <class 'tuple'> <class
'list'>
------------------------------------------------------------------------------------------------
NOTE:
=>One can define One List in another List
=>One can define One Tuple in another Tuple
=>One can define One List in another Tuple ( tuple of lists)
=>One can define One tuple in another List (list of tuples)
------------------------------------------------------------------------------------------------
>>> print(t1,type(t1))
(10, 'Rossum', [16, 18, 17], ('CSE', 'AI', 'DS'), 'OUCET') <class 'tuple'>
>>> print(t1[2],type(t1[2]))-------[16, 18, 17] <class 'list'>
>>> print(t1[3],type(t1[3]))------('CSE', 'AI', 'DS') <class 'tuple'>
>>> t1[2].append("KVR")
>>> print(t1,type(t1))--(10, 'Rossum', [16, 18, 17, 'KVR'], ('CSE', 'AI', 'DS'),
'OUCET') <class 'tuple'>
>>> t1[3].append("Python")-----AttributeError: 'tuple' object has no attribute
'append'
>>> t1[2][1]--------18
>>> t1[2][-2]------17
>>> t1[2][-3]------18
>>> k=sorted(t1[-2])
>>> k------------['AI', 'CSE', 'DS']
>>> t1[3]=k---------TypeError: 'tuple' object does not support item assignment
>>> l1=list(t1)----
>>> l1----------[10, 'Rossum', [16, 18, 17, 'KVR'], ('CSE', 'AI', 'DS'), 'OUCET']
>>> l1[3]=k
>>> l1-----[10, 'Rossum', [16, 18, 17, 'KVR'], ['AI', 'CSE', 'DS'], 'OUCET']
>>> y=tuple(l1[3])
>>> l1[3]=y
>>> l1-----[10, 'Rossum', [16, 18, 17, 'KVR'], ('AI', 'CSE', 'DS'), 'OUCET']
>>> t2=tuple(l1)
>>> t2-----(10, 'Rossum', [16, 18, 17, 'KVR'], ('AI', 'CSE', 'DS'), 'OUCET')
------------------------------------------------------------------------------------------------
>>> t1=('AI', 'cSE', 'DS')
>>> k=sorted(t1)
>>> k-------['AI', 'DS', 'cSE']
------------------------------------------------------------------------------------------------