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

Data Structure and Algorithm W3

Array module 3

Uploaded by

itsmekimkatee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Data Structure and Algorithm W3

Array module 3

Uploaded by

itsmekimkatee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

ARRAYS

An array is a data
structure used to
store multiple
elements.

2
Where do Arrays
used?

3
Example Code using Python

my_array = [7, 12, 9, 4, 11]

4
Types of Arrays

1. One Dimensional
Arrays
2. Two Dimensional
Arrays

5
One Dimensional Arrays

is the simplest form of an Array


in which the elements are stored
linearly and can be accessed
individually by specifying the
index value of each element
stored in the array
6
import numpy as np

# creating the list


list = [100, 200, 300, 400]

# creating 1-d array


n = np.array(list)
print(n)

7
N=5
ar = [0]*N
print(ar)
8
2
dimensional
Arrays
9
Using 2D arrays/lists the
right way involves
understanding the
structure, accessing
elements, and efficiently
manipulating data in a two-
dimensional grid.
10
import numpy as np

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

print(arr)

print(type(arr))

11
import numpy as np

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

print(arr)

print(type(arr))

12
Array Methods

13
Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with


the specified value
14
extend() Add the elements of a list (or any iterable), to
the end of the current list
index() Returns the index of the first element with the
specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
15
Adding Array Elements

Add one more element to the cars array:

Example
cars.append("Honda")

16
Removing Array Elements
You can use the pop() method to
remove an element from the array.

Example
cars.pop(1)

17
Removing Array Elements
use the remove() method to
remove an element from the array.

Example
cars.remove("Volvo")

18

You might also like