PYTHON ARRAY

Array can be handled in python by module named “array“. They can be useful when we have to manipulate only a specific data type values.


Program 1

array(),append(),insert()

# array(), append(), insert()

# importing "array" for array operations
import array

# initializing array with array values
# initializes array with signed integers
#Here i is signed integers
arr = array.array('i', [1, 2, 3])
print ("The new created array is : ",end=" ")
for i in range (0, 3):
    print (arr[i], end=" ")
print("")

# using append() to insert new value at end
arr.append(4);
print("The appended array is : ", end="")
for i in range (0, 4):
    print (arr[i], end=" ")
    
# using insert() to insert value at specific position
# inserts 3 at 1st position
arr.insert(1, 3)

print("\r")

# printing array after insertion
print ("The array after insertion is : ", end="")
for i in range (0, 5):
    print (arr[i], end=" ")

Output:
The new created array is :  1 2 3
The appended array is : 1 2 3 4
The array after insertion is : 1 3 2 3 4

Program 2

pop(),remove()


# pop() , remove() ,

# importing "array" for array operations
import array

# initializing array with array values
# initializes array with signed integers
arr= array.array('i',[1, 2, 3, 4, 5])

# printing original array
print ("The new created array is : ",end="")
for i in range (0,5):
    print (arr[i],end=" ")

print ("\r")

# using pop() to remove element at 2nd position
print ("The popped element is : ",end="")
print (arr.pop(2));

print ("The array after popping is : ",end="")
for i in range (0,4):
    print (arr[i],end=" ")

print("\r")

# using remove() to remove 1st occurrence of 1
arr.remove(1)
print ("The array after removing is : ",end="")
for i in range (0,3):
    print (arr[i],end=" ")


Output:
The new created array is : 1 2 3 4 5
The popped element is : 3
The array after popping is : 1 2 4 5 
The array after removing is : 2 4 5

Program 3

index(),reverse()


# index() and reverse()

# importing "array" for array operations
import array

# initializing array with array values
# initializes array with signed integers
arr= array.array('i',[1, 2, 3, 4, 5, 6])

# printing original array
print ("The new created array is : ",end="")
for i in range (0,6):
    print (arr[i],end=" ")

print ("\r")

# using index() to print index of 1st occurrence of 2
print ("The index of 1st occurrence of 2 is : ",end="")
print (arr.index(2))

#using reverse() to reverse the array
arr.reverse()

# printing array after reversing
print ("The array after reversing is : ",end="")
for i in range (0,6):
    print (arr[i],end=" ")


Output:
The new created array is : 1 2 3 4 5 6
The index of 1st occurrence of 2 is : 1
The array after reversing is : 6 5 4 3 2 1

Program 4
del()


# initializing list
list = [1, 2, 3, 4, 5, 6, 7]

# using del to delete elements from 2 to 5
del list[2 : 5]

# displaying list after deleting
print ("List elements after deleting are : ",end="")
for i in range(0, len(list)):
    print(list[i], end=" ")
    
print("\r")


Output:
List elements after deleting are : 1 2 6 7

Comments

Popular posts from this blog

Python Conditions

Basic Operators in Python