Python: Array Exercises, Practice, Solution
Python Array [24 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
Python array module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained.
1. Write a Python program to create an array of 5 integers and display the array items. Access individual elements through indexes.
Sample Output:
1
3
5
7
9
Access first three items individually
1
3
5
Click me to see the sample solution
2. Write a Python program to append a new item to the end of the array.
Sample Output:
Original array: array('i', [1, 3, 5, 7, 9])
Append 11 at the end of the array:
New array: array('i', [1, 3, 5, 7, 9, 11])
Click me to see the sample solution
3. Write a Python program to reverse the order of the items in the array.
Sample Output
Original array: array('i', [1, 3, 5, 3, 7, 1, 9, 3])
Reverse the order of the items:
array('i', [3, 9, 1, 7, 3, 5, 3, 1])
Click me to see the sample solution
4. Write a Python program to get the length in bytes of one array item in the internal representation.
Sample Output:
Original array: array('i', [1, 3, 5, 7, 9])
Length in bytes of one array item: 4
Click me to see the sample solution
5. Write a Python program to get the current memory address and the length in elements of the buffer used to hold an array's contents. Also, find the size of the memory buffer in bytes.
Sample Output:
Original array: array('i', [1, 3, 5, 7, 9])
Current memory address and the length in elements of the buffer: (139741883429512, 5)
The size of the memory buffer in bytes: 20
Click me to see the sample solution
6. Write a Python program to get the number of occurrences of a specified element in an array.
Sample Output:
Original array: array('i', [1, 3, 5, 3, 7, 9, 3])
Number of occurrences of the number 3 in the said array: 3
Click me to see the sample solution
7. Write a Python program to append items from inerrable to the end of the array.
Sample Output:
Original array: array('i', [1, 3, 5, 7, 9])
Extended array: array('i', [1, 3, 5, 7, 9, 1, 3, 5, 7, 9])
Click me to see the sample solution
8. Write a Python program to convert an array to an array of machine values and return the bytes representation.
Sample Output:
Bytes to String:
b'w3resource'
Click me to see the sample solution
9. Write a Python program to append items to a specified list.
Sample Output:
Items in the list: [1, 2, 6, -8]
Append items from the list:
Items in the array: array('i', [1, 2, 6, -8])
Click me to see the sample solution
10. Write a Python program to insert a newly created item before the second element in an existing array.
Sample Output:
Original array: array('i', [1, 3, 5, 7, 9])
Insert new value 4 before 3:
New array: array('i', [1, 4, 3, 5, 7, 9])
Click me to see the sample solution
11. Write a Python program to remove a specified item using the index of an array.
Sample Output:
Original array: array('i', [1, 3, 5, 7, 9])
Remove the third item form the array:
New array: array('i', [1, 3, 7, 9])
Click me to see the sample solution
12. Write a Python program to remove the first occurrence of a specified element from an array.
Sample Output:
Original array: array('i', [1, 3, 5, 3, 7, 1, 9, 3])
Remove the first occurrence of 3 from the said array:
New array: array('i', [1, 5, 3, 7, 1, 9, 3])
Click me to see the sample solution
13. Write a Python program to convert an array to an ordinary list with the same items.
Original array: array('i', [1, 3, 5, 3, 7, 1, 9, 3])
Convert the said array to an ordinary list with the same items:
[1, 3, 5, 3, 7, 1, 9, 3]
Click me to see the sample solution
14. Write a Python program to find out if a given array of integers contains any duplicate elements. Return true if any value appears at least twice in the array, and return false if every element is distinct.
Sample Output:
False
True
True
Click me to see the sample solution
15. Write a Python program to find the first duplicate element in a given array of integers. Return -1 if there are no such elements.
Sample Output:
4
-1
1
Click me to see the sample solution
16. Write a Python program to check whether it follows the sequence given in the patterns array.
Pattern example:
For color1 = ["red", "green", "green"] and patterns = ["a", "b", "b"]
the output should be samePatterns(color1, patterns) = true;
For color2 = ["red", "green", "greenn"] and patterns = ["a", "b", "b"]
the output should be samePatterns (strings, color2) = false.
Click me to see the sample solution
17. Write a Python program to find a pair with the highest product from a given array of integers.
Original array: [1, 2, 3, 4, 7, 0, 8, 4]
Maximum product pair is: (7, 8)
Original array: [0, -1, -2, -4, 5, 0, -6]
Maximum product pair is: (-4, -6)
Click me to see the sample solution
18. Write a Python program to create an array of six integers. Print all members of the array.
Sample Output:
10
20
30
40
50
60
Click me to see the sample solution
19. Write a Python program to get array buffer information.
Sample Output:
Array buffer start address in memory and number of elements.
(140023105054240, 2)
Click me to see the sample solution
20. Write a Python program to get the length of an array.
Sample Output:
Length of the array is:
5
Click me to see the sample solution
21. Write a Python program to get the array size of types unsigned integer and float.
Sample Output:
4
4
Click me to see the sample solution
22. Write a Python program that reads a string and interprets it as an array of machine values.
Sample Output:
array1: array('i', [7, 8, 9, 10])
Bytes: b'0700000008000000090000000a000000'
array2: array('i', [7, 8, 9, 10])
Click me to see the sample solution
23. Write a Python program that removes all duplicate elements from an array and returns a new array.
Sample Output:
Original array:
1 3 5 1 3 7 9
After removing duplicate elements from the said array:
1 3 5 7 9
Original array:
2 4 2 6 4 8
After removing duplicate elements from the said array:
2 4 6 8
Click me to see the sample solution
24. Write a Python program to find the missing number in a given array of numbers between 10 and 20.
Sample Output:
Original array:
10 11 12 13 14 16 17 18 19 20
Missing number in the said array (10-20): 15
Original array:
10 11 12 13 14 15 16 17 18 19
Missing number in the said array (10-20): 20
Click me to see the sample solution
Python Code Editor:
More to Come!
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/array/
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics