Arrays in Python allow you to store multiple values in a single variable. Learn how to create arrays using lists, the array module, and NumPy.
๐ฌWhat is an Array?
An
array is a linear data structure that stores elements in contiguous
memory locations.
Each element is accessed using an index, and all elements are of the same
data type.
๐ฌKey Features:
- Fixed
size
(in most languages like C/C++)
- Index-based
access
(starts from 0)
- Efficient
for reading elements
- Stored
in continuous memory
๐ฌTypes of Arrays:
- One-Dimensional
Array:
Simple list of elements
- Two-Dimensional
Array:
Like a matrix (rows & columns)
- Multi-Dimensional
Array:
3D or more dimensions
๐ฌArray Syntax in Different Languages:
๐ขPython:
arr
= [10, 20, 30, 40]
print(arr[2]) # Output: 30
๐ขC:
int
arr[4] = {10, 20, 30, 40};
printf("%d",
arr[2]); // Output: 30
๐ขC++:
int
arr[] = {10, 20, 30, 40};
cout
<< arr[2]; // Output: 30
๐ขJava:
int[]
arr = {10, 20, 30, 40};
System.out.println(arr[2]);
// Output: 30
๐ฌExample Program (Python):
arr = [1, 2, 3, 4, 5]
๐# Insert
arr.insert(2, 99) # at index 2
print(arr) # [1, 2, 99, 3, 4, 5]
๐# Delete
arr.remove(99)
print(arr) # [1, 2, 3, 4, 5]
๐# Search
print(3 in arr) # True
๐# Update
arr[1] = 20
print(arr) # [1, 20, 3, 4, 5
๐ฌWhen to Use Arrays?
- When the size is known in advance.
- When you need fast access using index.
- For storing elements of same type.


No comments:
Post a Comment