In Python, arrays allow you to store multiple values in a single variable. Once an array is created, you often need to access individual items to read or use the data.
In this tutorial, you will learn how to access array items in Python using index numbers, negative indexing, and looping techniques.
What is Array Indexing?
Array indexing means accessing elements using their position number.
Example Concept:
Index: 0 1 2 3
Array: 10 20 30 40- Index starts from 0
- First element is at index 0
- Last element is at index (n-1)
Creating an Array
We use Python’s array module.
import array as arr
numbers = arr.array('i', [10, 20, 30, 40, 50])1. Access Array Items Using Index
You can access elements using square brackets [].
Example
print(numbers[0])
print(numbers[2])
print(numbers[4])Output
10
30
50Explanation
numbers[0]→ first elementnumbers[2]→ third elementnumbers[4]→ fifth element
2. Negative Indexing
Python allows accessing elements from the end using negative index.
Example
print(numbers[-1])
print(numbers[-2])Output
50
40Explanation
| Index | Value |
|---|---|
| -1 | Last element |
| -2 | Second last |
3. Access Array Items Using Loop
You can access all elements using a loop.
Example
for item in numbers:
print(item)Output
10
20
30
40
504. Access Array Items Using Index Loop
Example
for i in range(len(numbers)):
print(numbers[i])Output
10
20
30
40
505. Access Array Item by Condition
You can access items based on conditions.
Example: Print values greater than 25
for item in numbers:
if item > 25:
print(item)Output
30
40
506. Access Specific Range (Slicing Concept with List Conversion)
Python arrays do not support slicing directly like lists, but you can convert them.
Example
print(numbers[1:4])Output
array('i', [20, 30, 40])7. Access First and Last Elements
Example
print("First:", numbers[0])
print("Last:", numbers[-1])Output
First: 10
Last: 50Real-World Example: Student Marks
import array as arr
marks = arr.array('i', [85, 90, 78, 88, 92])
print("First Mark:", marks[0])
print("Highest Index Mark:", marks[-1])Real-World Example: Product Prices
import array as arr
prices = arr.array('f', [99.99, 149.50, 200.75])
for price in prices:
print("Price:", price)Common Mistakes
Mistake 1: Using Out-of-Range Index
❌ Wrong
print(numbers[10])✔ Result: IndexError
Mistake 2: Forgetting Index Starts at 0
❌ Wrong assumption:
First element = 1✔ Correct:
First element = 0 indexMistake 3: Mixing Array Types
arr.array('i', [10, "hello", 20])✔ Only same data type is allowed
Array Access Methods Summary
| Method | Description |
index [0] | Access first element |
index [-1] | Access last element |
| loop | Access all elements |
| range loop | Access by index |
| condition | Filter values |
| slicing | Access range of items |
Practice Exercise 1
Print the second and fourth element of an array.
Practice Exercise 2
Print all elements greater than 50.
Conclusion
Accessing array items in Python is simple and powerful. You can use:
- Positive indexing
- Negative indexing
- Loops
- Conditions
- Range-based access
Understanding array access is essential for working with data processing, scientific computing, and real-world applications.


0 Comments