CodeGym /Java Course /Python SELF EN /Fixed-Size Arrays

Fixed-Size Arrays

Python SELF EN
Level 52 , Lesson 2
Available

9.1 Features of Fixed-Size Arrays

Fixed-size arrays are arrays whose size is determined when they're created and cannot be changed during program execution. In Python, you can create fixed-size arrays using the array library or use lists. Even though lists in Python can change size, they're used similarly to arrays in other languages.

Features of fixed-size arrays:

  • Fixed Size: The size of the array is set when it's created and cannot be changed.
  • Homogeneity: All elements in the array must be of the same type.
  • Fast Index Access: Access to array elements is constant time O(1).
  • Fixed Size: The number of elements in the array remains constant, and adding new elements is not possible.

Example of Creating a Fixed-Size Array in Python:

Using the array library:


import array

# Create an array of integers (type 'i' for int)
arr = array.array('i', [1, 2, 3, 4, 5])

# Access elements
print(arr[2])  # Output: 3

# Change element value
arr[2] = 10
print(arr)  # Output: array('i', [1, 2, 10, 4, 5])

Using a list (mimicking a fixed-size array):


# Create a fixed-size list
arr = [0] * 5

# Initialize elements
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

# Access elements
print(arr[2])  # Output: 3

# Change element value
arr[2] = 10
print(arr)  # Output: [1, 2, 10, 4, 5]

9.2 Advantages and Disadvantages of Fixed-Size Arrays

Everything has its pros and cons, and so do fixed-size arrays.

Advantages:

  • Fast Index Access: Access to any element of the array is constant time O(1), making arrays very efficient for reading data.
  • Simplicity: Arrays are simple to understand and use; they're easy to implement and apply to various tasks.
  • Memory Efficiency: Since the array size is fixed, memory is allocated immediately upon creation, avoiding the cost of memory reallocation.
  • Predictability: The fixed size of the array simplifies memory management and resource use predictability.

Disadvantages:

  • Fixed Size: The size of the array is set when it's created and cannot be changed. This means you need to know the necessary size in advance or risk over-allocating memory.
  • Costly Inserts and Deletes: Inserting and deleting elements can be time-consuming as they require shifting elements. In the worst case, inserting or deleting an element in the middle of an array takes O(n) time.
  • Inefficient Memory Use: If the array isn't fully used, the remaining memory cells stay unfilled, leading to inefficient memory use.
  • Limited Flexibility: Arrays don't allow dynamic resizing, making them less flexible compared to dynamic data structures like lists.

9.3 Examples of Usage and Application

Here are a few examples of usage and application of fixed-size arrays.

Example 1: Tables and Matrices

Fixed-size arrays are often used to represent tables and matrices where the size is known in advance.


import numpy as np

# Create a 3x3 matrix with a fixed size
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

# Access matrix elements
print(matrix[1][2])  # Output: 6

# Modify matrix element
matrix[1][2] = 10
print(matrix)
# Output:
# [[ 1  2  3]
#  [ 4  5 10]
#  [ 7  8  9]]

Example 2: Buffers and Caches

Fixed-size arrays are used to create buffers and caches where the buffer size is known in advance and doesn't change.


# Fixed-size buffer for reading data
buffer_size = 1024
buffer = bytearray(buffer_size)

# Fill buffer with data
data = b"Hello, World!"
buffer[:len(data)] = data

print(buffer[:len(data)])  # Output: b'Hello, World!'

Example 3: Storing Date and Time Data

Fixed-size arrays can be used to store date and time data, like the number of days in each month.

In this example, we mimic fixed array behavior using the list class:


# Number of days in each month (non-leap year)
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# Access data
month = 2  # February
print(f"February has {days_in_month[month - 1]} days")  # Output: February has 28 days
2
Task
Python SELF EN, level 52, lesson 2
Locked
Creating Your Own Array
Creating Your Own Array
2
Task
Python SELF EN, level 52, lesson 2
Locked
Create a two-dimensional array.
Create a two-dimensional array.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION