Python Arrays

Arrays are one of the fundamental data structures in programming, and Python offers several ways to work with them. When I first started working with Python more than a decade ago, understanding arrays was a game-changer for handling collections of data efficiently.

In this tutorial, I’ll walk you through everything you need to know about arrays in Python – from creating them to performing various operations. Whether you’re analyzing stock market data or processing customer information for your US-based business, arrays will make your life easier.

Let’s dive in and explore how to harness the power of arrays in Python!

What Are Arrays in Python?

Arrays in Python are ordered collections of items that can store elements of the same data type. Unlike lists (which are more flexible), true arrays in Python are more memory-efficient and faster for numerical operations.

Python offers different ways to work with arrays:

  1. Python’s built-in array module
  2. NumPy arrays (the most commonly used)
  3. Lists (Python’s default sequence type)

Let’s explore each option and see when to use them.

Check out the Python Operators page to learn about Operators

Method 1: Using Python’s Built-in Array Module

The array module provides an array object that is more compact than lists for storing basic numeric types.

Creating Arrays with the Array Module

import array as arr

# Creating an integer array
# 'i' represents signed integer
numbers = arr.array('i', [1, 2, 3, 4, 5])

print(numbers)  # Output: array('i', [1, 2, 3, 4, 5])
print(type(numbers))  # Output: <class 'array.array'>

Common Array Type Codes

  • ‘b’: signed char (integer between -128 to 127)
  • ‘i’: signed int
  • ‘f’: float
  • ‘d’: double (float with higher precision)

Basic Operations with Array Module

import array as arr

# Create an array of integers
sales_figures = arr.array('i', [10500, 15200, 8700, 19300, 12600])

# Accessing elements
print(f"January sales: ${sales_figures[0]}")  # Output: January sales: $10500

# Updating values
sales_figures[1] = 16000
print(f"Updated February sales: ${sales_figures[1]}")  # Output: Updated February sales: $16000

# Adding elements
sales_figures.append(14800)
print(f"Added June sales: ${sales_figures[-1]}")  # Output: Added June sales: $14800

# Removing elements
removed_value = sales_figures.pop(2)
print(f"Removed March sales (${removed_value}) due to data error")

Method 2: Using NumPy Arrays (Recommended)

NumPy arrays are the most widely used arrays in Python, especially for data science and numerical computing.

Setting Up NumPy

# Install NumPy if you haven't already
# pip install numpy

import numpy as np

Creating NumPy Arrays

# 1D array
temperatures = np.array([68, 71, 73, 69, 75, 83, 79])
print(temperatures)  # Output: [68 71 73 69 75 83 79]

# 2D array (matrix)
monthly_sales = np.array([
    [5000, 6000, 7000],  # Q1 (Jan, Feb, Mar)
    [7500, 8200, 9000],  # Q2 (Apr, May, Jun)
    [9500, 9800, 10000], # Q3 (Jul, Aug, Sep)
    [12000, 15000, 13000] # Q4 (Oct, Nov, Dec)
])

print(monthly_sales)

Creating Special NumPy Arrays

# Array of zeros
zero_array = np.zeros(5)
print(f"Zeros array: {zero_array}")

# Array of ones
ones_array = np.ones(5)
print(f"Ones array: {ones_array}")

# Range array
range_array = np.arange(0, 10, 2)  # Start, stop, step
print(f"Range array: {range_array}")  # Output: [0 2 4 6 8]

# Evenly spaced array
linspace_array = np.linspace(0, 1, 5)  # Start, stop, number of elements
print(f"Linspace array: {linspace_array}")  # Output: [0.   0.25 0.5  0.75 1.  ]

NumPy Array Operations

import numpy as np

# Create arrays of US city temperatures (°F)
new_york = np.array([45, 48, 52, 58, 65])
los_angeles = np.array([68, 70, 72, 74, 76])

# Addition
temperature_sum = new_york + los_angeles
print(f"Sum of temperatures: {temperature_sum}")

# Multiplication (element-wise)
temperature_product = new_york * 2  # Double all temperatures
print(f"New York temperatures doubled: {temperature_product}")

# Statistical operations
print(f"Average NY temperature: {np.mean(new_york)}°F")
print(f"Maximum LA temperature: {np.max(los_angeles)}°F")
print(f"Minimum NY temperature: {np.min(new_york)}°F")
print(f"Standard deviation of NY temperatures: {np.std(new_york):.2f}°F")

Array Reshaping and Slicing

# Creating a 1D array
quarterly_sales = np.array([55000, 68000, 72000, 98000, 62000, 75000, 83000, 91000])

# Reshaping to 2D (2 rows, 4 columns) - 2 years of quarterly data
sales_by_year = quarterly_sales.reshape(2, 4)
print("Sales by year (quarterly):")
print(sales_by_year)

# Slicing arrays
print("\nFirst year quarterly sales:")
print(sales_by_year[0])  # First row

print("\nQ2 sales for both years:")
print(sales_by_year[:, 1])  # Second column

You can also learn about Python Lists

Method 3: Using Python Lists as Arrays

While not true arrays, Python lists can serve as array-like structures for many purposes.

# Creating a list as an array
us_cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

# Adding elements
us_cities.append("Philadelphia")

# Accessing elements
print(f"Third largest city: {us_cities[2]}")  # Output: Chicago

# Slicing
east_coast_cities = us_cities[0:2]
print(f"East coast cities: {east_coast_cities}")  # Output: ['New York', 'Los Angeles']

When to Use Each Type of Array

  1. Use Python’s array module when:
    • You need a simple collection of numerical data of the same type
    • Memory efficiency is important
    • You don’t need complex mathematical operations
  2. Use NumPy arrays when:
    • You need to perform mathematical operations on arrays
    • You’re working with large datasets
    • You need broadcasting and vectorized operations
    • You need multi-dimensional arrays
  3. Use Python lists when:
    • You need a collection of mixed data types
    • You’re frequently adding/removing elements
    • You don’t need high-performance numerical operations

Practical Example: Analyzing US Stock Data

Let’s put arrays to work with a practical example of analyzing US stock returns:

import numpy as np
import matplotlib.pyplot as plt

# Monthly stock returns (%) for 5 major US companies in 2023 (hypothetical)
stock_returns = np.array([
    [2.1, 1.5, -0.8, 3.2, 1.7],  # Company A
    [1.8, -1.2, 0.9, 2.8, 3.3],  # Company B
    [0.5, 0.8, 1.2, -0.3, 2.1],  # Company C
    [3.2, 2.8, 1.9, 0.7, -1.2],  # Company D
    [1.1, 1.5, 2.0, 2.2, 1.8]    # Company E
])

# Calculate average return for each company
avg_returns = np.mean(stock_returns, axis=1)
print("Average returns by company:")
for i, avg in enumerate(avg_returns):
    print(f"Company {chr(65+i)}: {avg:.2f}%")

# Find the best-performing company
best_company_index = np.argmax(avg_returns)
print(f"\nBest performing company: Company {chr(65+best_company_index)} with {avg_returns[best_company_index]:.2f}% average return")

# Calculate cumulative returns 
cumulative_returns = np.cumprod(1 + stock_returns/100, axis=1) * 100 - 100

# Plot the results
plt.figure(figsize=(10, 6))
for i, company_returns in enumerate(cumulative_returns):
    plt.plot(company_returns, label=f'Company {chr(65+i)}')

plt.title('Cumulative Stock Returns')
plt.xlabel('Month')
plt.ylabel('Cumulative Return (%)')
plt.legend()
plt.grid(True)
# plt.show()  # Uncomment to display the plot

Array-related tutorial:

Conclusion

Arrays in Python provide powerful ways to work with collections of data. Whether you’re using the built-in array module, NumPy arrays, or Python lists, understanding how to manipulate these data structures is essential for any Python developer.

For most data analysis and numerical computing tasks, I recommend using NumPy arrays due to their flexibility and performance. However, for simpler tasks, Python’s built-in modules might be sufficient.

I hope this tutorial has given you a solid foundation in working with arrays in Python. As you continue your Python journey, you’ll find arrays indispensable for data manipulation and analysis tasks.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

Let’s be friends

Be the first to know about sales and special discounts.