Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Array

An array can store multiple items of the same type together in contiguous memory locations. Arrays in Python can be created using the array module by specifying the data type and values. Elements can be added to or removed from an array using methods like insert(), append(), remove(), and pop(). Individual elements can be accessed using their index within square brackets.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Array

An array can store multiple items of the same type together in contiguous memory locations. Arrays in Python can be created using the array module by specifying the data type and values. Elements can be added to or removed from an array using methods like insert(), append(), remove(), and pop(). Individual elements can be accessed using their index within square brackets.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

array

• An array is to store multiple items of the same


type together.
• It is a collection of items stored at contiguous
memory locations.
Creating a Array:Array in Python can be created by
importing array module.
array(data_type, value_list) is used to create an
array with data type and value list specified in its
arguments.
Note: typecode must be b, B, u, h, H, i, I, l, L, q, Q, f
or d
• import array as arr
• # creating an array with integer type
• a = arr.array('i', [1, 2, 3])
• # printing original array
• print("The new created array is :")
• for i in range(0, 3):
• print(a[i])

The new created array is :


1
2
3
• Example:
import array as arr
a = arr.array('d', [1.0, 2.5, 3.6])
print("The new created array is :",end=" ")
for i in range(0, 3):
print(a[i],end=" ")
print()

The new created array is : 1.0 2.5 3.6


• Adding Elements to a Array
• Elements can be added to the Array by using
built-in insert() function. Insert is used to
insert one or more data elements into an
array. Based on the requirement, a new
element can be added at the beginning, end,
or any given index of array. append() is also
used to add the value mentioned in its
arguments at the end of the array.
Adding element using append():
• import array as arr
• b = arr.array('d', [2.5, 3.2, 3.3])
• print("Array before insertion : ", end=" ")
• for i in range(0, 3): Array before insertion : 2.5 3.2 3.3
Array after insertion : 2.5 3.2 3.3 4.4
• print(b[i], end=" ")
• print()
• # adding an element using append()
• b.append(4.4)
• print("Array after insertion : ", end=" ")
• for i in (b):
• print(i, end=" ")
• print()
Adding element using insert():
• import array as arr
• a = arr.array('i', [1, 2, 3])
• print("Array before insertion : ", end=" ")
• for i in range(0, 3): Array before insertion : 1 2 3
• print(a[i], end=" ") Array after insertion : 1 4 2 3

• print()
• a.insert(1, 4)
• print("Array after insertion : ", end=" ")
• for i in (a):
• print(i, end=" ")
• print()
• Accessing elements from the Array
• In order to access the array items refer to the
index number. Use the index operator [ ] to access
an item in a array. The index must be an integer.
• import array as arr
• a = arr.array('i', [1, 2, 3, 4, 5, 6])
• print("Access element is: ", a[0])
• print("Access element is: ", a[3])
• b = arr.array('d', [2.5, 3.2, 3.3])
• print("Access element is: ", b[1])
• print("Access element is: ", b[2])
Removing Elements from the Array:Elements can be
removed from the array by using built-in remove()
function but an Error arises if element doesn’t exist
in the set. Remove() method only removes one
element at a time, to remove range of elements,
iterator is used. pop() function can also be used to
remove and return an element from the array, but by
default it removes only the last element of the array,
to remove element from a specific position of the
array, index of the element is passed as an argument
to the pop() method.
• Note – Remove method in List will only remove the
first occurrence of the searched element.
Removing Element using remove() function:
import array as arr
a = arr.array('i', [1, 2, 3, 4, 5,1, 6])
a.remove(1)
for i in (a):
print(i,end=" ") 234516
print()

You might also like