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

Array in Python

The document provides various examples of using NumPy and the array module in Python for creating and manipulating arrays. It covers operations such as element-wise multiplication, appending, inserting, iterating, slicing, updating, removing elements, and reversing arrays. Each example demonstrates specific functionalities of the array structures in Python.

Uploaded by

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

Array in Python

The document provides various examples of using NumPy and the array module in Python for creating and manipulating arrays. It covers operations such as element-wise multiplication, appending, inserting, iterating, slicing, updating, removing elements, and reversing arrays. Each example demonstrates specific functionalities of the array structures in Python.

Uploaded by

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

1.

import numpy as np

# Creating a NumPy array


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

# Element-wise operations
print(arr)
print(arr * 2)

# Multi-dimensional array
arr2d = np.array([[1, 2], [3, 4]])
print(arr2d * 2)

2. import array as arr

# creating array of integers


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

# accessing First Araay


print(a[0])

# Adding element to array


a.append(4)
print(a)

3. import array as arr

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

# iterating and printing each item


for i in range(0, 3):
print(a[i], end=" ")

4. import array as arr

# Integer array example


a = arr.array('i', [1, 3, 4])
print("Integer Array before insertion:", *a)

a.insert(1, 2) # Insert 4 at index 1


print("Integer Array after insertion:", *a)

5. import array as arr


a = arr.array('i', [1, 2, 3, 4, 5, 6])

print(a[0])
print(a[3])

b = arr.array('d', [2.5, 3.2, 4.3])


print(b[1])
print(b[2])

6. import array
arr = array.array('i', [1, 2, 3, 4, 5])

# using remove() method to remove first occurance of 1


arr.remove(1)
print(arr)

# pop() method - remove item at index 2


arr.pop(2)
print(arr)

7. import array as arr


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

a = arr.array('i', l)

Sliced_array = a[3:8]
print(Sliced_array)

Sliced_array = a[5:]
print(Sliced_array)

Sliced_array = a[:]
print(Sliced_array)

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

# update item at index 2


arr[2] = 6
print(arr)

# update item at index 4


arr[4] = 8
print(arr)

9. import array
arr = array.array('i', [1, 2, 3, 4, 5])

arr.reverse()
print("Reversed array:", *arr)

10. import array as arr


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

# using extend() method


a.extend([6,7,8,9,10])
print(a)

You might also like