DATATYPES ========== SEQUENTIAL DATATYPES ======================= 3) BYTE DATATYPE ================= ==> IS ORDERED ==> INDEX SUPPORTED ==> CLASSIFIED INTO TWO TYPES: 1) bytes Datatype 2) bytearray Datatype Byte ==> 8-bits to store 2^8 possible values 256 possible values ==> 0 to 255 Note: ==== Byte datatype categoury is always accepted the range of values from 0 to 255 1) bytes Datatype ============== ==> pre-defined method: bytes() using bytes() we can convert any collection data into bytes But, that collection values must be from the range ==> 0 to 255 Syntax: bytes(collection_data) Note: bytes datatype is not possible to made with strings. Features of Bytes data: =================== 1) Bytes data is allowed to define with only homogeneous elements. Note: no float, no complex, no string as bytes elements. ==> type error 2) Bytes datatype is ordered. 3) bytes datatype is index supported. 4) slicing is also possible. 5) Duplication is allowed. 6) Immutable datatype. a = [1,3,4,5,7,9,7,9,7,9] b = (2,4,6,8,10) # c = [1.2,2.3,4.5] c = (True,False,True,False) print(type(a),type(b)) p = bytes(a) # list ==> bytes q = bytes(b) # tuple ==> bytes r = bytes(c) print(type(p),type(q),type(r)) print(p) print(q) print(r) # positive indexing print(p[0],p[1],p[2],p[3],p[4]) # negative indexing print(q[-1],q[-2],q[-3]) # slicing print(p[::-1]) print(q[::-1]) print(r[::-1]) # p[1] = 97 bytearray datatype =============== ==> using bytearray(), we can create a bytearray data. Syntax: bytearray(collection data) Features: ======= 1) possible to define with only homogeneous elements 2) ordered. 3) index based 4) slicing is also possible 5) duplication is allowed. 6) mutable datatype a = bytearray([1,3,5,7,9,10,8,6,4,2]) b = bytearray((99,98,97,96,95,94,93,92,91)) print(type(a),type(b)) print(a[0],a[1],a[2],a[3]) print(b[-1],b[-2],b[-3]) print(a[::-1]) print(b[::1]) print(a) print(b) print(id(a)) a[0] = 200 print(a) print(id(a)) ================================================================ NON-SEQUENTIAL DATATYPES =========================== ==> ARE NOT INDEX BASED ==> BUT MAY OR MAY NOT BE ORDERED. ==> TWO TYPES: 1) SET TYPE 2) MAP TYPE 1) SET TYPE ========== ==> TWO TYPES: 1) SET 2) FROZENSET 1) SET DATATYPE =============== ==> DEFINE WITH {} AND THE ELEMENTS IN {} MUST BE SEPARATED WITH COMMA ==> inbuilt datatype pre-defined class ==> set Features: ======= 1) Sets are allowed to define with homogeneous elements and/or with heterogeneous elements. 2) Sets are not ordered. 3) Index is not supported 4) Duplication is not allowed by the sets 5) Mutable. a = {1,2,3,4,5,6,7,8,9} b = {111,'a',1-2j,True} c = {1,2,3,3,2,1,1,2,3} print(type(a)) print(type(b)) print(a) print(b) print(c) # print(a[0]) print(id(a)) a.add(100) print(a) print(id(a)) ========================================= Frozenset Datatype ================ ==> using frozenset(), we can able to define the frozenset ==> frozenset(), will convert any collection data to frozenset Syntax: ====== frozenset(collection data) Features: ======= 1) Like sets: possible with homogeneous elements and/or with heterogeneous elements. 2) Unordered 3) not index supported 4) duplication is also not allowed. 5) Immutable. a = frozenset('abcde') b = frozenset([1,3,5,7,9]) c = frozenset((1.2,111,True,10-20j)) print(type(a)) print(type(b)) print(type(c)) print(a) print(b) print(c) # print(a[0]) # a.add(100) ============================================= Map datatype =========== ==> the data can be represented with key and value pair format. ex: key : value 'a' : 100, 1 : 'a' etc. ==> Dictionary ==-> pre-defined class ==> dict ==> also allowed to define with {} within the {}, key:value (element) as member must be separated with comma. ex:{key1:value1,key2:value2,key3:value3,...} Rules: ==== 1) keys are of any datatype 2) values are of any datatype 3) keys of dictionary must be unique (no duplicatiion is allowed) 4) values are possible to define with duplication ==> Dictionaries are ordered. ==> not index based ==> the values of the dictionary can be allowed to access with the keys. Syntax: dictionary-name[key] ==> Mutable. a = {11:10,12:11,13:12} b = {'a':11,12:'b',True:12.23} c = {'a' : 11, 'b' : 111,'a' : 222} d = {'a' : 11,'b':11,'c':11} print(type(a)) print(a) print(b) print(c) print(d) # print(a[0]) # print(b[-1]) print(b[12]) print(c['b']) print(id(a)) a[11] = 1212 print(a) print(id(a))