Python Arrays

🐍 Python Arrays — Full Tutorial

In Python, arrays can be implemented in two ways:

  1. Using lists (most common, flexible)

  2. Using the array module (more like traditional arrays, type-specific)


🔹 1. Using Python Lists as Arrays

Python lists can store multiple elements of different data types:

# List as array
numbers = [1, 2, 3, 4, 5]
fruits = ["Apple", "Banana", "Cherry"]

print(numbers[0]) # 1
print(fruits[1]) # Banana

  • Lists are dynamic — you can add or remove elements.

  • Lists support slicing, indexing, loops.


List Operations

numbers = [1, 2, 3]

# Add
numbers.append(4)
numbers.insert(1, 10)

# Remove
numbers.remove(2)
numbers.pop() # removes last item

# Access
print(numbers[0])
print(numbers[-1])

# Length
print(len(numbers))


🔹 2. Using the array Module

If you want a true array with same data type elements, use the array module.

import array

# Create an integer array
numbers = array.array('i', [1, 2, 3, 4, 5])

print(numbers)
print(numbers[0])

  • 'i' → type code for signed integer

  • 'f' → type code for float

  • Common type codes:

Type Code
int 'i'
float 'f'
double 'd'
char 'u'

Array Operations

import array

arr = array.array('i', [1, 2, 3])

# Append
arr.append(4)

# Insert
arr.insert(1, 10)

# Remove
arr.remove(2)

# Pop last element
arr.pop()

# Iterate
for x in arr:
print(x)


🔹 3. Accessing Array Elements

arr = [10, 20, 30, 40]

print(arr[0]) # 10
print(arr[-1]) # 40
print(arr[1:3]) # [20, 30]

  • Works for lists and array.array.


🔹 4. Looping Through Arrays

numbers = [1, 2, 3, 4]

# Using for loop
for num in numbers:
print(num)

# Using while loop
i = 0
while i < len(numbers):
print(numbers[i])
i += 1


🔹 5. Multidimensional Arrays

Using Nested Lists

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

print(matrix[0][1]) # 2

Using numpy (Recommended for large arrays)

import numpy as np

arr = np.array([[1,2,3],[4,5,6]])
print(arr)
print(arr[1,2]) # 6


🔹 6. Array Operations with NumPy

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# Addition
print(arr1 + arr2) # [5 7 9]

# Multiplication
print(arr1 * 2) # [2 4 6]

# Sum
print(np.sum(arr1)) # 6


🔹 7. Summary

Feature List array module NumPy Array
Type-specific ❌ No ✅ Yes ✅ Yes
Dynamic Size ✅ Yes ✅ Yes ✅ Yes
Supports Loops ✅ Yes ✅ Yes ✅ Yes
Mathematical Operations ❌ No Limited ✅ Yes

🧠 Practice Exercises

  1. Create a list of 10 numbers and find their sum.

  2. Create an array of integers using the array module and remove an element.

  3. Create a 3×3 matrix using nested lists and print the diagonal elements.

  4. Using NumPy, create an array of 5 numbers and multiply each by 10.

  5. Loop through a list and print only even numbers.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *