Array
Array :
1. Array is special variable which can hold more than one value at time
2. Array is collection of common type of data structures having element with same data type
Why we use array ?
1. For numerical operation
2. Store data very compactly or more efficiently
3. List can't directly handle maths operations
4. Tuple, list having group of value similar as array but in list and tuple have various type of data (int, float, string) but in array having same type
import array as arr
a = arr. array('i',[1,2,3])
a.reverse()
a[1] = -2
print(a)
print(a[2]) # indexing
print(a[0:2]) # slicing
print(a.buffer_info()) # address and size
a.append(4) # adding new value
print(a)
# take input from user
b = arr. array('i',[ ])
n = int(input("Enter the lenght of array"))
for i in range(n):
x = int(input("Enter the next value please"))
b.append(x)
print(b)
Comments
Post a Comment
If you have any doubts, please let me know