COLLECTION DATATYPES: ===================== primitive datatypes: integer float complex Boolean string a = 0b11001 ==> int b = 1123.234 c = 12-23j d = True e = 'ravi' facebook signup: {full name, dob, user name, mobile, mail, age,...} calculator: 10 + 20 = 30 to define the variable with more than one value (group of values), we can use "collection datatypes". ==> collection datatypes are classified into: 1) Sequential datatypes ========================= like strings, we can access these using an index are: list, tuple, bytes, bytearray 2) Non-Sequential datatypes ============================ are not an index supported. are: sets, frozensets 3) Mapping Datatypes ===================== key and value Syntax: key : value ex: dictionary LIST DATA OPERATIONS ===================== About List: =========== 1) list can be a sequential data collection. 2) list can always define with '[]'. 3) list is the collection of elements in [] with comma separation. Syntax: list-object-name = [ele1,ele2,ele3,....] Note: if only one element in [] ==> list more than one element in [] ==> list 4) list is an inbuilt datatype. an inbuilt class ==> 'list' 5) list is a collection of homogeneous elements and also with heterogeneous elements. homogeneous elements ==> the values with same type heterogeneous elements ==> the values with different type 6) list can index based. if it is index based: we can access the data with positive index values, with negative index values and even slicing is also possible. Syntax: list-object[index] 7) List is ordered datatype. 8) Can be nested. list within list other collections within list 9) List can be mutable. # Features of the list el = [] ls = [11] lm = [1,2,3,4,5] # homogeneous list lh = [0b101010,123,123.345,True,100-200j,'string'] # heterogeneous list nl = [[1,2,3],[4,5,6],[7,8,9]] print(type(el)) print(type(ls)) print(type(lm)) print(type(lh)) print(type(nl)) # positive indexing print(lm[0]);print(lm[1]);print(lm[2]);print(lm[3]);print(lm[4]) # negative indexing print(lm[-1]);print(lm[-2]);print(lm[-3]);print(lm[-4]);print(lm[-5]) # slicing print(lh[1:4]) print(lh[-1:-5:-1]) print(lm[::-1]) print(lm);print(lh) print(nl[0]);print(nl[1]);print(nl[2]) print(nl[-1]);print(nl[-2]);print(nl[-3]) # sub-indexing print(nl[0][0]);print(nl[0][1]);print(nl[0][2]) print(id(nl)) print(nl) nl[1] = [11,15,17,19] print(id(nl)) print(nl) ================================================ How to define the list: ======================= 3-ways: 1) compile time definition =========================== Syntax: list-object-name = [e1,e2,e3,e4] 2) run time definition ======================= ==> to define the list with dynamically change, run time definition is used. an inbuilt method: eval() Syntax: eval(input()) 3) using list() =============== list() is an inbuilt method, which we can use to convert any collection data into list. Syntax: list-object-name = list(collection-data) # list definitions lc = [1,10,100,1000,10000] # compile time lr = eval(input("Enter a list:")) # run time ls = list('Python') lt = list((1,3,5,7,9,10,8,6,4,2,0)) print(type(lc));print(type(lr)) print(lc);print(lr) print(ls);print(lt)