|
36 | 36 | # u - unicode character - allow unicode character values
|
37 | 37 | # b - signed byte integer - allow positive and negative byte integer values
|
38 | 38 | # B - unsigned byte integer - allow positive byte integer values only
|
| 39 | + |
| 40 | + |
| 41 | +from array import * |
| 42 | + |
| 43 | +a=array('i',[-1,2,3,4,5]) |
| 44 | + |
| 45 | +print(a) # array('i', [1, 2, 3, 4, 5]) |
| 46 | + |
| 47 | +a=array('I',[1,2,3,4,5]) |
| 48 | + |
| 49 | +print(a) # array('I', [1, 2, 3, 4, 5]) |
| 50 | + |
| 51 | +# a=array('i',[1,2,3,4,5,'a']) |
| 52 | + |
| 53 | +# print(a) # TypeError: 'str' object cannot be interpreted as an integer |
| 54 | + |
| 55 | +# a=array('I',[-1,2,3,4,5]) |
| 56 | + |
| 57 | +# print(a) OverflowError: can't convert negative value to unsigned int |
| 58 | + |
| 59 | +a=array('u',['a','b','c']) |
| 60 | + |
| 61 | +print(a) # array('u', 'abc') |
| 62 | + |
| 63 | +a=array('b',[-1,2,3,3]) # array('b', [-1, 2, 3, 3]) |
| 64 | + |
| 65 | +print(a) #TypeError: 'str' object cannot be interpreted as an integer |
| 66 | + |
| 67 | +a=array('f',[-1,2,3,3.0]) # array('f', [-1.0, 2.0, 3.0, 3.0]) |
| 68 | + |
| 69 | +print(a) # convert int to float |
| 70 | + |
| 71 | +a=array('d',[-1,2,3,3.00500050]) # array('d', [-1.0, 2.0, 3.0, 3.0050005]) |
| 72 | + |
| 73 | +print(a) # convert int to double |
| 74 | + |
| 75 | +# a=array('B',[-1,2,3]) |
| 76 | + |
| 77 | +# print(a) # OverflowError: unsigned byte integer is less than minimum |
| 78 | + |
| 79 | +# Array Operations and its Time complexity |
| 80 | +# n - length of array, i - index |
| 81 | +# Accessing - O(1) |
| 82 | +# Inserting - Best O(1) Inserting at last, Worst O(n-i) Insert at any position |
| 83 | +# Deleting - Best O(1) Deleting at last, Worst O(n-i) Delete at any position |
| 84 | +# Seaching - Best O(1) got in first position, Average O(n-i) may got in middle ,Worst O(n) got at last postion |
| 85 | + |
| 86 | + |
| 87 | +print("------------------------------------------ Adding Element to a array ------------------------------------------") |
| 88 | +# Adding Elements to a Array |
| 89 | +# Elements can be added to the Array by using built-in insert() function. |
| 90 | + |
| 91 | +# Insert(): |
| 92 | +# Insert is used to insert one or more data elements into an array. |
| 93 | +# a new element can be added at the beginning, end, or any given index of array. |
| 94 | + |
| 95 | +# append(): |
| 96 | +# append() is also used to add the value mentioned in its arguments at the end of the array. |
| 97 | + |
| 98 | + |
| 99 | +import array as arr |
| 100 | +a = arr.array('i', [1, 2, 3]) |
| 101 | +print("Array before insertion : ", end=" ") |
| 102 | +for i in range(0, 3): |
| 103 | + print(a[i], end=" ") |
| 104 | +print() |
| 105 | + |
| 106 | +a.insert(1, 4) |
| 107 | +print("Array after insertion : ", end=" ") |
| 108 | +for i in (a): |
| 109 | + print(i, end=" ") |
| 110 | +print() |
| 111 | + |
| 112 | +b = arr.array('d', [2.5, 3.2, 3.3]) |
| 113 | +print("Array before insertion : ", end=" ") |
| 114 | +for i in range(0, 3): |
| 115 | + print(b[i], end=" ") |
| 116 | +print() |
| 117 | + |
| 118 | +b.append(4.4) |
| 119 | +print("Array after insertion : ", end=" ") |
| 120 | +for i in (b): |
| 121 | + print(i, end=" ") |
| 122 | +print() |
| 123 | + |
0 commit comments