Python – Arrays

After reading this Python arrays topic, you will understand its the theory and examples, you will understand how to create the arrays, and you will know how to accessing Items, changing Items and use arrays built-in methods in Python.

An array is a variable that collects same type of elements in a sequence.

How to create Array in python?

The array elements should be written within the square brackets (i.e. [ ]) and each item separated by a comma (,).

Example:

Program (1): To demonstrate how to create and display array in Python.

# storing items 2,3 and 4 in an array
x = [2,3,4]
# print an array
print(x)

Output(1)

[2, 3, 4]

Accessing Elements from an Array

Example:

Program (1): To demonstrate how to create array and access array items in Python.

# storing items 2,3 and 4 in an array
x = [2,3,4]
print(x[0])
print(x[1])

Output(1)

2
3

Explanation: for Program(1) and Output(1)

  • statement print(x[0]) gives output 2 describe the first item of array, associated with index number 0.
  • statement print(x[1]) gives output 3 describe the second item of array, associated with index number 1.

Changing Items from Array

Example:

Program (1): To demonstrate how to create array and access array items in Python.

# storing items 2,3 and 4 in the array
x = [2,3,4]
#print original array
print(x)
#change second item of array
x[1]=5
# print array with changed items
print(x)

Output(1)

[2, 3, 4]
[2, 5, 4]

Using loop over the Array

Using loop over the array means we move through the array and visit each item of the array.

Example:

Program (1): To demonstrate how to use loop on the array to access array items in Python.

# storing items 2,3 and 4 in the array
x = [2,3,4]
# use for loop in order to print each item of the array
for i in x:
print(i)

Output(1)

2
3
4

Array handling built-in methods

Some of commonly used array handling methods like len(), insert(), remove(), pop() and clear().

len() : This method gives the total number of items in an array.

Example:

Program (1): To demonstrate how to use len() function on the array in Python.

# storing items 2,3 and 4 in an array
x = [2,3,4]
# using len() method to calculate to the total number of items in the array.
print(len(x))

Output(1)

3

insert(): This method inserts an item at the particular position defined by the index number of the array.

Example:

Program (1): To demonstrate how to use insert() function on the array in Python.

# storing items 2,3 and 4 in an array
x = [2,3,4]
# using insert() method to add item 5 in the second position of an array (for second position index number is 1)
x.insert(1,5)
# print array
print(x)

Output(1)

[2, 5, 3, 4]

remove() : This method remove an item from an array.

Example:

Program (1): To demonstrate how to use remove() function on the array in Python.

# storing items 2,3 and 4 in an array
x = [2,3,4]
# using remove() method to remove item 3 from an array
x.remove(3)
# print an array
print(x)

Output(1)

[2, 4]

pop(): This method removes and returns an item from the particular position defined by the index number of an array. When index number is not specified then the last item of an array gets removed.

Example:

Program (1): To demonstrate how to use pop() function on the array in Python.

# storing items 2,3 and 4 in the array
x = [2,3,4]
# using pop() method to remove last item from the array.
x.pop ()
# print an array
print(x)

Output(1)

[2, 3]

clear() : This method remove all items from an array.

Example:

Program (1): To demonstrate how to use clear() function on the array in Python.

# storing items 2,3 and 4 in an array
x = [2,3,4]
# using clear() method to remove all items
x.clear()
# print an array
print(x)

Output(1)

[]

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *