77 [
88 Python,
99 Array in Python,
10- Introduction of python,
11- Python Syntax,
12- Variables,
13- Operators,
14- Type Casting,
15- String
10+ array module,
11+ python data structures,
12+ array methods,
13+ type codes,
1614 ]
17-
15+ description : Learn how to use arrays in Python using the built-in 'array' module. This guide covers array creation, indexing, slicing, methods, and type codes.
1816---
1917
2018# Python Arrays
2119
22- An ** Array** in Python is a data structure that stores multiple elements of the ** same data type** in contiguous memory locations.
20+ An ** Array** in Python is a data structure that stores multiple elements of the ** same data type** in contiguous memory locations.
2321Arrays are ** ordered** , ** mutable** , and ** type-restricted** , making them more memory-efficient than lists for large numeric data.
2422
2523In Python, arrays are provided by the built-in ** ` array ` ** module, which must be imported before use.
@@ -43,229 +41,3 @@ numbers = array.array('i', [1, 2, 3, 4, 5])
4341floats = array.array(' f' , [1.1 , 2.2 , 3.3 ])
4442
4543print (numbers) # array('i', [1, 2, 3, 4, 5])
46- ```
47-
48- ---
49-
50- ## Type Codes
51-
52- Arrays in Python require a ** type code** to specify the element type:
53-
54- | Type Code | C Type | Python Type | Size (bytes) |
55- | --------- | --------------- | ----------- | ------------ |
56- | ` 'i' ` | signed int | int | 2 or 4 |
57- | ` 'I' ` | unsigned int | int | 2 or 4 |
58- | ` 'f' ` | float | float | 4 |
59- | ` 'd' ` | double | float | 8 |
60- | ` 'b' ` | signed char | int | 1 |
61- | ` 'B' ` | unsigned char | int | 1 |
62- | ` 'u' ` | Py_UNICODE | Unicode | 2 |
63-
64- ---
65-
66- ## Indexing
67-
68- Just like lists, arrays use ** zero-based indexing** .
69-
70- ``` python
71- nums = array.array(' i' , [10 , 20 , 30 , 40 , 50 ])
72-
73- print (nums[0 ]) # 10
74- print (nums[2 ]) # 30
75- print (nums[- 1 ]) # 50
76- ```
77-
78- ---
79-
80- ## Slicing
81-
82- You can slice arrays to get sub-arrays.
83-
84- ``` python
85- print (nums[1 :4 ]) # array('i', [20, 30, 40])
86- print (nums[:3 ]) # array('i', [10, 20, 30])
87- print (nums[::2 ]) # array('i', [10, 30, 50])
88- ```
89-
90- ** Syntax:**
91-
92- ```
93- array[start:stop:step]
94- ```
95-
96- ---
97-
98- ## Modifying Elements
99-
100- Arrays are ** mutable** , so you can change elements:
101-
102- ``` python
103- nums[1 ] = 99
104- print (nums) # array('i', [10, 99, 30, 40, 50])
105- ```
106-
107- ---
108-
109- ## Array Methods
110-
111- Python's ` array ` module provides several useful methods:
112-
113- | Method | Description |
114- | ------------------ | ----------------------------------------------------- |
115- | ` append(x) ` | Adds an element to the end |
116- | ` insert(i, x) ` | Inserts an element at index ` i ` |
117- | ` extend(iterable) ` | Adds elements from another iterable |
118- | ` remove(x) ` | Removes the first occurrence of the item |
119- | ` pop([i]) ` | Removes and returns the item at index ` i ` |
120- | ` index(x) ` | Returns the index of the first occurrence of the item |
121- | ` count(x) ` | Counts how many times the item appears |
122- | ` reverse() ` | Reverses the array |
123- | ` buffer_info() ` | Returns a tuple (memory address, length) |
124- | ` tobytes() ` | Converts the array to a bytes object |
125- | ` frombytes(b) ` | Appends items from a bytes object |
126-
127- ---
128-
129- ### Examples
130-
131- #### append()
132-
133- ``` python
134- nums = array.array(' i' , [1 , 2 , 3 ])
135- nums.append(4 )
136- print (nums) # array('i', [1, 2, 3, 4])
137- ```
138-
139- #### insert()
140-
141- ``` python
142- nums.insert(1 , 100 )
143- print (nums) # array('i', [1, 100, 2, 3, 4])
144- ```
145-
146- #### extend()
147-
148- ``` python
149- nums.extend([5 , 6 ])
150- print (nums) # array('i', [1, 100, 2, 3, 4, 5, 6])
151- ```
152-
153- #### remove() and pop()
154-
155- ``` python
156- nums.remove(100 )
157- print (nums) # array('i', [1, 2, 3, 4, 5, 6])
158-
159- nums.pop() # Removes last element
160- print (nums) # array('i', [1, 2, 3, 4, 5])
161-
162- nums.pop(2 ) # Removes index 2
163- print (nums) # array('i', [1, 2, 4, 5])
164- ```
165-
166- ---
167-
168- ## Iterating Through an Array
169-
170- ** Using a for loop:**
171-
172- ``` python
173- for num in nums:
174- print (num)
175- ```
176-
177- ** Using indices:**
178-
179- ``` python
180- for i in range (len (nums)):
181- print (i, nums[i])
182- ```
183-
184- ---
185-
186- ## Membership Test
187-
188- Check if an element exists in an array:
189-
190- ``` python
191- print (10 in nums) # True or False
192- print (100 not in nums) # True or False
193- ```
194-
195- ---
196-
197- ## Array from List
198-
199- ``` python
200- list_data = [1 , 2 , 3 , 4 ]
201- arr = array.array(' i' , list_data)
202- print (arr)
203- ```
204-
205- ---
206-
207- ## Copying Arrays
208-
209- Assigning directly creates a reference:
210-
211- ``` python
212- a = array.array(' i' , [1 , 2 , 3 ])
213- b = a
214- b.append(4 )
215- print (a) # array('i', [1, 2, 3, 4])
216- ```
217-
218- To make an independent copy:
219-
220- ``` python
221- c = array.array(a.typecode, a)
222- c.append(5 )
223-
224- print (a) # array('i', [1, 2, 3, 4])
225- print (c) # array('i', [1, 2, 3, 4, 5])
226- ```
227-
228- ---
229-
230- ### ** Practice Questions**
231-
232- 1 . ** Basic Traversal**
233-
234- ** Q1** : Write a Python program to traverse an array and print each element on a new line.
235-
236- 2 . ** Maximum Element**
237-
238- ** Q2:** Write a Python program to find the maximum and minimum elements in an array without using built-in functions.
239-
240- 3 . ** Array Reversal**
241-
242- ** Q3:**** Write a Python program to reverse an array without using slicing or the reverse() method.
243-
244-
245- 4 . ** Insertion Operation**
246-
247- ** Q4:** Write a Python program to insert an element at a specific index in an array.
248-
249-
250- 5 . ** Deletion Operation**
251-
252- ** Q5:** Write a Python program to delete an element from a given index in an array.
253-
254- 6 . ** Search Element**
255-
256- ** Q6:** Write a Python program to search for a given element in an array and print its index if found, otherwise print "Not Found".
257-
258- 7 . ** Sum of Elements**
259-
260- ** Q7:** Write a Python program to find the sum of all elements in an array without using the sum() function.
261-
262- 8 . ** Second Largest Element**
263-
264- ** Q8:** Write a Python program to find the second largest element in an array.
265-
266- ---
267-
268- ## Conclusion
269-
270- Python Arrays are useful when you need to store large amounts of ** numeric data** of the same type efficiently.
271- They provide faster performance and smaller memory footprint compared to lists for numerical operations.
0 commit comments