|
1 | 1 | # Array: |
2 | | -# An array is a type of linear data structure |
3 | | -# that is defined as a collection of elements with same or different data types. |
4 | | -# They exist in both single dimension and multiple dimensions. |
5 | | -# These data structures come into picture when there is a necessity |
| 2 | +# An array is a type of linear data structure |
| 3 | +# that is defined as a collection of elements with same or different data types. |
| 4 | +# They exist in both single dimension and multiple dimensions. |
| 5 | +# These data structures come into picture when there is a necessity |
6 | 6 | # to store multiple elements of similar nature together(data type) at one place. |
7 | 7 |
|
8 | | -# The difference between an array index and a memory address |
9 | | -# array index acts like a key value to label the elements in the array. |
| 8 | +# The difference between an array index and a memory address |
| 9 | +# array index acts like a key value to label the elements in the array. |
10 | 10 | # a memory address is the starting address of free memory available. |
11 | 11 |
|
12 | 12 | # Following are the important terms to understand the concept of Array. |
|
15 | 15 | # Index − Each location of an element in an array has a numerical index, which is used to identify the element. |
16 | 16 |
|
17 | 17 | # An array is a collection of items stored at contiguous memory locations. |
18 | | -# The idea is to store multiple items of the same type together. |
19 | | -# This makes it easier to calculate the position of each element by simply adding an offset to a base value, |
| 18 | +# The idea is to store multiple items of the same type together. |
| 19 | +# This makes it easier to calculate the position of each element by simply adding an offset to a base value, |
20 | 20 | # i.e., the memory location of the first element of the array (generally denoted by the name of the array). |
21 | 21 |
|
22 | 22 | # Creating an Array |
23 | | -# Array in Python can be created by importing an array module. |
24 | | -# array(data_type, value_list) is used to create an array with data type and value list specified in its arguments. |
| 23 | +# Array in Python can be created by importing an array module. |
| 24 | +# array(data_type, value_list) is used to create an array with data type and value list specified in its arguments. |
25 | 25 |
|
26 | 26 | # There are thre ways to import array module: |
27 | 27 | # Using import (import array) |
28 | 28 | # Using import and call alias name (import array as arr) |
29 | 29 | # from modules import all methods ( from array import * ) |
30 | 30 |
|
31 | | -# Some of the data types are mentioned below which will help in creating an array of different data types. |
| 31 | +# Some of the data types are mentioned below which will help in creating an array of different data types. |
32 | 32 | # i - signed Int - allow positive and negative values |
33 | 33 | # I - unsigned Int - allow only positive values |
34 | 34 | # d - double - allow double values |
|
40 | 40 |
|
41 | 41 | from array import * |
42 | 42 |
|
43 | | -a=array('i',[-1,2,3,4,5]) |
| 43 | +a = array("i", [-1, 2, 3, 4, 5]) |
44 | 44 |
|
45 | | -print(a) # array('i', [1, 2, 3, 4, 5]) |
| 45 | +print(a) # array('i', [1, 2, 3, 4, 5]) |
46 | 46 |
|
47 | | -a=array('I',[1,2,3,4,5]) |
| 47 | +a = array("I", [1, 2, 3, 4, 5]) |
48 | 48 |
|
49 | | -print(a) # array('I', [1, 2, 3, 4, 5]) |
| 49 | +print(a) # array('I', [1, 2, 3, 4, 5]) |
50 | 50 |
|
51 | 51 | # a=array('i',[1,2,3,4,5,'a']) |
52 | 52 |
|
|
56 | 56 |
|
57 | 57 | # print(a) OverflowError: can't convert negative value to unsigned int |
58 | 58 |
|
59 | | -a=array('u',['a','b','c']) |
| 59 | +a = array("u", ["a", "b", "c"]) |
60 | 60 |
|
61 | | -print(a) # array('u', 'abc') |
| 61 | +print(a) # array('u', 'abc') |
62 | 62 |
|
63 | | -a=array('b',[-1,2,3,3]) # array('b', [-1, 2, 3, 3]) |
| 63 | +a = array("b", [-1, 2, 3, 3]) # array('b', [-1, 2, 3, 3]) |
64 | 64 |
|
65 | | -print(a) #TypeError: 'str' object cannot be interpreted as an integer |
| 65 | +print(a) # TypeError: 'str' object cannot be interpreted as an integer |
66 | 66 |
|
67 | | -a=array('f',[-1,2,3,3.0]) # array('f', [-1.0, 2.0, 3.0, 3.0]) |
| 67 | +a = array("f", [-1, 2, 3, 3.0]) # array('f', [-1.0, 2.0, 3.0, 3.0]) |
68 | 68 |
|
69 | | -print(a) # convert int to float |
| 69 | +print(a) # convert int to float |
70 | 70 |
|
71 | | -a=array('d',[-1,2,3,3.00500050]) # array('d', [-1.0, 2.0, 3.0, 3.0050005]) |
| 71 | +a = array("d", [-1, 2, 3, 3.00500050]) # array('d', [-1.0, 2.0, 3.0, 3.0050005]) |
72 | 72 |
|
73 | | -print(a) # convert int to double |
| 73 | +print(a) # convert int to double |
74 | 74 |
|
75 | 75 | # a=array('B',[-1,2,3]) |
76 | 76 |
|
|
84 | 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 | 85 |
|
86 | 86 |
|
87 | | -print("------------------------------------------ Adding Element to a array ------------------------------------------") |
| 87 | +print( |
| 88 | + "------------------------------------------ Adding Element to a array ------------------------------------------" |
| 89 | +) |
88 | 90 | # Adding Elements to a Array |
89 | | -# Elements can be added to the Array by using built-in insert() function. |
| 91 | +# Elements can be added to the Array by using built-in insert() function. |
90 | 92 |
|
91 | 93 | # 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 | +# Insert is used to insert one or more data elements into an array. |
| 95 | +# a new element can be added at the beginning, end, or any given index of array. |
94 | 96 |
|
95 | | -# append(): |
96 | | -# append() is also used to add the value mentioned in its arguments at the end of the array. |
| 97 | +# append(): |
| 98 | +# append() is also used to add the value mentioned in its arguments at the end of the array. |
97 | 99 |
|
98 | 100 |
|
99 | 101 | import array as arr |
100 | | -a = arr.array('i', [1, 2, 3]) |
| 102 | + |
| 103 | +a = arr.array("i", [1, 2, 3]) |
101 | 104 | print("Array before insertion : ", end=" ") |
102 | 105 | for i in range(0, 3): |
103 | | - print(a[i], end=" ") |
| 106 | + print(a[i], end=" ") |
104 | 107 | print() |
105 | 108 |
|
106 | 109 | a.insert(1, 4) |
107 | 110 | print("Array after insertion : ", end=" ") |
108 | | -for i in (a): |
109 | | - print(i, end=" ") |
| 111 | +for i in a: |
| 112 | + print(i, end=" ") |
110 | 113 | print() |
111 | 114 |
|
112 | | -b = arr.array('d', [2.5, 3.2, 3.3]) |
| 115 | +b = arr.array("d", [2.5, 3.2, 3.3]) |
113 | 116 | print("Array before insertion : ", end=" ") |
114 | 117 | for i in range(0, 3): |
115 | | - print(b[i], end=" ") |
| 118 | + print(b[i], end=" ") |
116 | 119 | print() |
117 | 120 |
|
118 | 121 | b.append(4.4) |
119 | 122 | print("Array after insertion : ", end=" ") |
120 | | -for i in (b): |
121 | | - print(i, end=" ") |
| 123 | +for i in b: |
| 124 | + print(i, end=" ") |
122 | 125 | print() |
123 | | - |
|
0 commit comments