Python Array Overview

Aapne array ke concept ko bhut si programming languages me padha hoga aur usi tarah python me bhi array ka concept hai, sirf difference itna hi hai ki python ke case me array ek built-in feature nhi hai. Isko use karne ke liye iske module me call karna hota hai. 

Array same type ke data items ko contiguous(ek ke baad ek) memory location me store karta hai. Array me indexing ke through uske items ko access kiya jata hai aur indexing ke through hi usme operations bhi kiye jate hain.

Example se samajhte hain:

import array as arr

# integer type ka array creation
x = arr.array(‘i’, [5,6,7])

print (“Integered Array is : “, end =” “)
for i in range (0, 3):
    print (x[i], end =” “)

float type ka array creation
y = arr.array(‘d’, [2.7 5.2, 6.3])

print (“Floating array is : “, end =” “)
for d in range (0, 3):
    print (y[d], end =” “)

Diye gaye example me “arr” array ka alias naam hai, alias means koi bhi naam jo user apne samajh ke anusar de sakta hai, jiski sari properties uss module ki rahegi. Iska matlab yah ki array module ki sari properties “arr” me rahegi.

arr.array(‘j’,[5,6,7]) equivalent to arraymodule.array(‘data_type’,value_list)
jaha ‘i’ data types aur [5,6,7] value_list ko represent kar rahe hain.

Python Array me Elements add karen

List ki tarah array me bhi insert() aur append() ka use karke aap elements ko add kar sakte hain. Agar user apni choice ke according kisi particular place me element add karna chahta hai to usse insert() function ka use karna hoga aur agar wah append() function ka use karega to elements by default array ke end me add honge.

import array as arr

# integer type ka array creation
x = arr.array(‘i’, [5,6,7])

print (“Insert() function ke pahle ka output: “, end =” “)
for i in range (0, 3):
    print (x[i], end =” “) 

x.insert(2,6)                 #2nd index me 6 ka insertion

print (“Insert() function ke baad ka output: “, end =” “) 
for i in range (0, 4): 
    print (x[i], end =” “) 

import array as arr

# integer type ka array creation
x = arr.array(‘i’, [5,6,7])

print (“append() function ke pahle ka output: “, end =” “)
for i in range (0, 3):
    print (x[i], end =” “) 

x.append(7)                 

print (“append() function ke baad ka output: “, end =” “) 
for i in range (0, 5): 
    print (x[i], end =” “) 

Python Array me Elements remove karne ke methods

Python Array me elements ko pop() aur remove() function ka use karke kiya jata hai. pop() function me jis value ko delete karna hota hai uske index ko pass kiya jata hai jo execute hone ke baad deleted value ko return karega. Wahi remove() function me uss value ko direct dalna hota hai jise aap delete karna chahte hain.

#Example for pop() function
import array as arr
x = arr.array(‘i’, [1, 2, 3, 1, 5])
y= x.pop(2)
print(x)

#Example for remove() function
x = arr.array(‘i’, [1, 2, 3, 1, 5])
x.remove(3)
print(x)

Python Array me Slicing ka concept

List ki tarah array me bhi slicing ka concept use kiya jata hai. Behtar hai aap example se samjhe:

#Example for slicing
x = arr.array(‘i’, [1, 2, 3, 1, 5])
x.remove(3)
print(x)
print(x[:])
print(x[1:3])
print(x[:-2])
print(x[1:5:2])