In Python, arrays are not as common as lists, which are often used in place of arrays due to their
flexibility. However, Python does support arrays through the `array` module. This guide will cover how to
create, access, and manipulate arrays in Python.
Introduction to Arrays
An array is a collection of items stored at contiguous memory locations. Python's `array` module provides a
space-efficient way to store data of the same type. Arrays are similar to lists but are restricted to
storing elements of a single data type.
Creating an Array
To use arrays in Python, you need to import the `array` module. Here’s how you can create an array:
The `array` module is imported, and we create an array of integers using `array.array("i", [1, 2, 3, 4, 5])`.
The `"i"` specifies that the array will contain integers. The array is then printed to show its contents.
Accessing Array Elements
You can access individual elements in an array using indexing, similar to lists. Here’s an example:
Array elements can be accessed by their index, where indexing starts at 0. Negative indexing is also
supported, allowing access to elements from the end of the array.
Modifying Array Elements
You can modify elements in an array by assigning new values to specific indices:
In this example, the element at index 2 is modified from 3 to 10. The updated array is then printed.
Array Operations
Arrays support various operations such as appending, inserting, and removing elements:
Python
import array
arr = array.array("i", [1, 2, 3])
# Append an element
arr.append(4)
# Insert an element at a specific index
arr.insert(1, 5)
# Remove an element
arr.remove(2)
print(arr) # Output: array("i", [1, 5, 3, 4])
Explanation:
We use `append()` to add an element at the end, `insert()` to add an element at a specific index, and
`remove()` to remove an element by value. The updated array is printed after these operations.
Array Length and Traversal
You can determine the length of an array and traverse through its elements using loops:
Python
import array
arr = array.array("i", [10, 20, 30, 40])
# Get the length of the array
length = len(arr)
print("Length:", length) # Output: Length: 4
# Traverse the array
for element in arr:
print(element)
Explanation:
The `len()` function returns the number of elements in the array. A loop is used to traverse and print each
element in the array.
Key Points to Remember
Arrays in Python are created using the `array` module and are more space-efficient for storing elements
of the same type compared to lists.
Arrays support indexing, modification, and various operations like appending, inserting, and removing
elements.
Arrays have a fixed data type and are less flexible than lists but can be more efficient in certain
cases.
Conclusion
Arrays provide a way to handle collections of items efficiently when the type of elements is known and fixed.
Understanding how to create, access, modify, and operate on arrays in Python will help you manage data more
effectively in your programs.
Working with Arrays in Python
Python mein arrays itne common nahi hote jitne lists, jo apni flexibility ke karan zyada use kiye jaate hain.
Lekin Python arrays ko support karta hai `array` module ke zariye. Is guide mein hum dekhenge kaise arrays
create, access, aur manipulate kiye jaate hain Python mein.
Introduction to Arrays
Array ek aisi collection hai jo contiguous memory locations mein stored hoti hai. Python ka `array` module ek
space-efficient tareeka provide karta hai jisme same type ke data ko store kiya jaa sakta hai. Arrays lists
ke jaise hote hain lekin ye sirf ek hi data type ke elements ko store kar sakte hain.
Creating an Array
Python mein arrays ka use karne ke liye, aapko `array` module ko import karna padta hai. Yahan kaise aap ek
array create kar sakte hain:
Array ke elements ko unke index ke zariye access kiya ja sakta hai, jahan indexing 0 se shuru hoti hai.
Negative indexing bhi supported hoti hai, jisse aap array ke end se elements ko access kar sakte hain.
Modifying Array Elements
Aap array ke elements ko nayi values assign karke modify kar sakte hain:
Is example mein, element ko index 2 par modify karke 3 se 10 kiya jata hai. Updated array ko phir print kiya
jata hai.
Array Operations
Arrays support karte hain various operations jaise append karna, insert karna, aur remove karna:
Python
import array
arr = array.array("i", [1, 2, 3])
# Ek element ko append karein
arr.append(4)
# Ek element ko specific index par insert karein
arr.insert(1, 5)
# Ek element ko remove karein
arr.remove(2)
print(arr) # Output: array("i", [1, 5, 3, 4])
Explanation:
Hum `append()` ka use karte hain ek element ko end mein add karne ke liye, `insert()` ka use specific index
par element ko add karne ke liye, aur `remove()` ka use element ko value ke zariye remove karne ke liye. In
operations ke baad updated array ko print kiya jata hai.
Array Length and Traversal
Aap array ki length ko determine kar sakte hain aur loop ke zariye uske elements ko traverse kar sakte hain:
Python
import array
arr = array.array("i", [10, 20, 30, 40])
# Array ki length paayein
length = len(arr)
print("Length:", length) # Output: Length: 4
# Array ko traverse karein
for element in arr:
print(element)
Explanation:
`len()` function array mein number of elements return karta hai. Ek loop ka use karke array ke har element ko
traverse karke print kiya jata hai.
Key Points to Remember
Python mein arrays `array` module ka use karke create kiye jaate hain aur ye lists ke comparison mein
zyada space-efficient hote hain jab same type ke elements ko store karna ho.
Arrays support karte hain indexing, modification, aur various operations jaise append karna, insert
karna, aur remove karna.
Arrays ka fixed data type hota hai aur ye lists ke comparison mein less flexible hote hain lekin kuch
cases mein zyada efficient hote hain.
Conclusion
Arrays ek efficient tareeka provide karte hain items ko handle karne ka jab elements ka type pata ho aur
fixed ho. Samajhna kaise arrays create, access, modify, aur operate kiye jaate hain Python mein aapko data
ko efficiently manage karne mein madad karega aapke programs mein.
Import Links
Here are some useful import links for further reading: