Python Array Tutorial

Introduction

Arrays in Python are a collection of items stored at contiguous memory locations. They are used to store multiple values of the same type, making it easier to manage large sets of data. In Python, arrays are provided by the array module.

Table of Contents

  1. Define Array and Key Points
  2. Create Array
  3. Access Array Items
  4. Change Array Items
  5. Add Array Items
  6. Remove Array Items
  7. Loop Arrays
  8. Array Methods
  9. Conclusion

Key Points

An array is a collection of items of the same type. 

Key points about arrays:

  • Ordered: Items have a defined order.
  • Mutable: Items can be changed.
  • Indexed: Each item is accessible using its index.
  • Homogeneous: All items are of the same type.

1. Create Array

To create an array, you need to import the array module. The array module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers.

Example

import array

# Creating an array of integers
int_array = array.array('i', [1, 2, 3, 4, 5])
print(int_array)  # Output: array('i', [1, 2, 3, 4, 5])

# Creating an array of floating point numbers
float_array = array.array('d', [1.1, 2.2, 3.3, 4.4])
print(float_array)  # Output: array('d', [1.1, 2.2, 3.3, 4.4])

2. Access Array Items

Array items can be accessed using their index. Indexing starts at 0.

Example

# Accessing array items
print(int_array[0])  # Output: 1
print(int_array[3])  # Output: 4

# Negative indexing
print(int_array[-1])  # Output: 5
print(int_array[-2])  # Output: 4

3. Change Array Items

Array items can be changed by assigning a new value to a specific index.

Example

# Changing array items
int_array[1] = 20
print(int_array)  # Output: array('i', [1, 20, 3, 4, 5])

4. Add Array Items

New items can be added to an array using methods like append(), insert(), and extend().

Example

# Using append() to add a single item
int_array.append(6)
print(int_array)  # Output: array('i', [1, 20, 3, 4, 5, 6])

# Using insert() to add an item at a specific position
int_array.insert(1, 10)
print(int_array)  # Output: array('i', [1, 10, 20, 3, 4, 5, 6])

# Using extend() to add multiple items
int_array.extend([7, 8, 9])
print(int_array)  # Output: array('i', [1, 10, 20, 3, 4, 5, 6, 7, 8, 9])

5. Remove Array Items

Items can be removed from an array using methods like remove(), pop(), and del.

Example

# Using remove() to remove a specific item
int_array.remove(10)
print(int_array)  # Output: array('i', [1, 20, 3, 4, 5, 6, 7, 8, 9])

# Using pop() to remove an item at a specific index
popped_item = int_array.pop(1)
print(popped_item)  # Output: 20
print(int_array)  # Output: array('i', [1, 3, 4, 5, 6, 7, 8, 9])

# Using del to remove an item at a specific index
del int_array[2]
print(int_array)  # Output: array('i', [1, 3, 5, 6, 7, 8, 9])

6. Loop Arrays

You can loop through the items in an array using a for loop.

Example

# Looping through an array
for item in int_array:
    print(item)

# Output:
# 1
# 3
# 5
# 6
# 7
# 8
# 9

7. Array Methods

Python arrays have several built-in methods, including append(), extend(), insert(), remove(), pop(), index(), reverse(), and buffer_info().

Example

# Using various array methods
example_array = array.array('i', [1, 2, 3, 4, 5])

# append() method
example_array.append(6)
print(example_array)  # Output: array('i', [1, 2, 3, 4, 5, 6])

# extend() method
example_array.extend([7, 8, 9])
print(example_array)  # Output: array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])

# insert() method
example_array.insert(0, 0)
print(example_array)  # Output: array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# remove() method
example_array.remove(5)
print(example_array)  # Output: array('i', [0, 1, 2, 3, 4, 6, 7, 8, 9])

# pop() method
popped_item = example_array.pop()
print(popped_item)  # Output: 9
print(example_array)  # Output: array('i', [0, 1, 2, 3, 4, 6, 7, 8])

# index() method
index = example_array.index(4)
print(index)  # Output: 4

# reverse() method
example_array.reverse()
print(example_array)  # Output: array('i', [8, 7, 6, 4, 3, 2, 1, 0])

# buffer_info() method
buffer_info = example_array.buffer_info()
print(buffer_info)  # Output: (address, length)

Conclusion

Python arrays are useful for storing collections of items of the same type. Understanding how to create, access, modify, and perform operations on arrays is essential for effective programming. This tutorial covered various aspects of arrays, including creating, accessing, changing, and removing items, as well as advanced topics like looping and using built-in methods. By mastering these concepts, you can efficiently work with arrays in your Python programs.

Comments