no no need notes with example and syntax
in python
Here are Python-only notes on arrays with syntax and examples.
Array in Python
In Python, arrays are usually represented by lists. Python also has an array module for
storing same-type values more efficiently.
Syntax for list as array
arr = [10, 20, 30, 40, 50]
Syntax for array module
from array import array
arr = array('i', [10, 20, 30, 40, 50])
'i' means integer type.
All elements must be of the same type.
Representation
An array stores elements in a sequence and each element has an index.
Example
arr = [5, 10, 15, 20]
print(arr[0]) # 5
print(arr[2]) # 15
Indexing
Indexing means accessing an element using its position.
Syntax
arr[index]
Example
arr = [100, 200, 300]
print(arr[1]) # 200
print(arr[-1]) # 300
Traversing an array
Traversal means visiting each element one by one.
Syntax
for x in arr:
print(x)
Example
arr = [1, 2, 3, 4]
for x in arr:
print(x)
Updating elements
You can change an element using its index.
Syntax
arr[index] = value
Example
arr = [10, 20, 30]
arr[1] = 99
print(arr) # [10, 99, 30]
Adding elements
Python lists are dynamic, so elements can be added easily.
1. append()
Adds an element at the end.
Syntax
[Link](value)
Example
arr = [1, 2, 3]
[Link](4)
print(arr) # [1, 2, 3, 4]
2. insert()
Adds an element at a specific position.
Syntax
[Link](index, value)
Example
arr = [1, 2, 4]
[Link](2, 3)
print(arr) # [1, 2, 3, 4]
3. extend()
Adds multiple elements at the end.
Syntax
[Link](iterable)
Example
arr = [1, 2]
[Link]([3, 4, 5])
print(arr) # [1, 2, 3, 4, 5]
Deleting elements
Python provides several ways to remove elements.
1. remove()
Removes the first matching value.
Syntax
[Link](value)
Example
arr = [10, 20, 30, 20]
[Link](20)
print(arr) # [10, 30, 20]
2. pop()
Removes an element by index. If no index is given, it removes the last element.
Syntax
[Link]()
[Link](index)
Example
arr = [10, 20, 30]
[Link]()
print(arr) # [10, 20]
[Link](0)
print(arr) # [20]
3. del
Deletes an element at a given index.
Syntax
del arr[index]
Example
arr = [10, 20, 30]
del arr[1]
print(arr) # [10, 30]
Searching in array
To check whether an element is present, use in.
Syntax
value in arr
Example
arr = [10, 20, 30]
print(20 in arr) # True
print(40 in arr) # False
To find the index of an element, use index().
Syntax
[Link](value)
Example
arr = [5, 10, 15]
print([Link](10)) # 1
Sorting and reversing
Sorting
[Link]()
Example:
arr = [30, 10, 20]
[Link]()
print(arr) # [10, 20, 30]
Reversing
[Link]()
Example:
arr = [1, 2, 3, 4]
[Link]()
print(arr) # [4, 3, 2, 1]
Slicing
Slicing means taking a part of the array.
Syntax
arr[start:end:step]
Example
arr = [10, 20, 30, 40, 50]
print(arr[1:4]) # [20, 30, 40]
print(arr[:3]) # [10, 20, 30]
print(arr[::2]) # [10, 30, 50]
2D array in Python
A 2D array is usually made using nested lists.
Syntax
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Example
matrix = [[1, 2], [3, 4], [5, 6]]
print(matrix[0][1]) # 2
print(matrix[2][0]) # 5
Traversing 2D array
for row in matrix:
for value in row:
print(value)
Common properties
Arrays store elements in order.
Python lists can store different data types.
array module stores only same-type values.
Lists are mutable, so they can be changed.
Python lists are dynamic, so size can grow or shrink.
Simple program using array-like list
arr = [10, 20, 30, 40, 50]
[Link](60)
[Link](2, 25)
[Link](40)
for x in arr:
print(x)
Short definition
An array in Python is a collection of elements stored in indexed order, usually
represented by a list.
Very short exam answer
Python arrays are represented mainly using lists. They store elements in indexed order,
support traversal, insertion, deletion, searching, sorting, and slicing.
If you want, I can turn this into:
1. 2-mark answer
2. 5-mark answer
3. 10-mark detailed notes