-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patharrayPractice.py
83 lines (63 loc) · 1.78 KB
/
arrayPractice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from array import *
# 1. Create an array and traverse.
my_array = array('i',[1,2,3,4,5])
for i in my_array:
print(i)
# 2. Access individual elements through indexes
print("Step 2")
print(my_array[3])
# 3. Append any value to the array using append() method
print("Step 3")
my_array.append(6)
print(my_array)
# 4. Insert value in an array using insert() method
print("Step 4")
my_array.insert(3, 11)
print(my_array)
# 5. Extend python array using extend() method
print("Step 5")
my_array1 = array('i', [10,11,12])
my_array.extend(my_array1)
print(my_array)
# 6. Add items from list into array using fromlist() method
print("Step 6")
tempList = [20,21,22]
my_array.fromlist(tempList)
print(my_array)
# 7. Remove any array element using remove() method
print("Step 7")
my_array.remove(11)
print(my_array)
# 8. Remove last array element using pop() method
print("Step 8")
my_array.pop()
print(my_array)
# 9. Fetch any element through its index using index() method
print("Step 9")
print(my_array.index(21))
# 10. Reverse a python array using reverse() method
print("Step 10")
my_array.reverse()
print(my_array)
# 11. Get array buffer information through buffer_info() method
print("Step 11")
print(my_array.buffer_info())
# 12. Check for number of occurrences of an element using count() method
print("Step 12")
my_array.append(11)
print(my_array.count(11))
print(my_array)
# 13. Convert array to string using tostring() method
print("Step 13")
strTemp = my_array.tostring()
print(strTemp)
ints = array('i')
ints.fromstring(strTemp)
print(ints)
# 14. Convert array to a python list with same elements using tolist() method
print("Step 14")
# print(my_array.tolist())
# 15. Append a string to char array using fromstring() method
# 16. Slice Elements from an array
print("Step 16")
print(my_array[:])