0% found this document useful (0 votes)
3 views

# Arrays in Python

The document explains arrays in Python, highlighting the use of built-in lists, the array module, and NumPy arrays for different types of data storage. It details the characteristics of arrays, such as indexing, mutability, and common operations like slicing and searching. NumPy arrays are recommended for numerical computing due to their efficiency and capabilities.

Uploaded by

MUKUL CHAUHAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

# Arrays in Python

The document explains arrays in Python, highlighting the use of built-in lists, the array module, and NumPy arrays for different types of data storage. It details the characteristics of arrays, such as indexing, mutability, and common operations like slicing and searching. NumPy arrays are recommended for numerical computing due to their efficiency and capabilities.

Uploaded by

MUKUL CHAUHAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# Arrays in Python

In Python, arrays are data structures that store elements of the same type in contiguous
memory locations. While Python has a built-in `list` type that can store heterogeneous elements,
true arrays (for homogeneous data) are available through the `array` module or third-party
libraries like NumPy.

## 1. Built-in Lists (Often Used as Arrays)

Python lists are flexible and can hold different data types, but they're commonly used as arrays:

```python
# Creating a list
my_list = [1, 2, 3, 4, 5]

# Accessing elements
print(my_list[0]) # Output: 1

# Modifying elements
my_list[1] = 20

# Length of list
print(len(my_list)) # Output: 5

# Adding elements
my_list.append(6)
```

## 2. Array Module

For more memory-efficient arrays of uniform type:

```python
import array

# Create an array of integers


int_array = array.array('i', [1, 2, 3, 4, 5])

# Create an array of floats


float_array = array.array('f', [1.0, 2.5, 3.7])

# Common type codes:


# 'i' - signed integer
# 'f' - floating point
# 'd' - double precision float
```

## 3. NumPy Arrays (Most Powerful)

For numerical computing, NumPy arrays are preferred:

```python
import numpy as np

# Create a NumPy array


np_array = np.array([1, 2, 3, 4, 5])

# Operations are vectorized


print(np_array * 2) # Output: [ 2 4 6 8 10]

# Multi-dimensional arrays
matrix = np.array([[1, 2, 3], [4, 5, 6]])
```

## Key Characteristics of Arrays in Python:

1. **Indexing**: Starts at 0 (like most programming languages)


2. **Mutable**: Elements can be changed after creation
3. **Ordered**: Elements maintain their order
4. **Iterable**: Can be looped through with `for` loops

## Common Operations:

```python
arr = [10, 20, 30, 40, 50] # Using list as example

# Slicing
print(arr[1:4]) # Output: [20, 30, 40]

# Searching
print(30 in arr) # Output: True

# Concatenation
new_arr = arr + [60, 70]

# Length
print(len(arr)) # Output: 5
```
For most numerical work, NumPy arrays are recommended due to their performance and
functionality. For general-purpose use, Python lists are typically sufficient.

You might also like