w3resource

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.]

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 element through indexes. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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 and also find the size of the memory buffer in bytes. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
Sample Output:
Bytes to String:
b'w3resource'
Click me to see the sample solution

9. Write a Python program to append items from a specified list. Go to the editor
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 new item before the second element in an existing array. Go to the editor
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 from an array. Go to the editor
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. Go to the editor
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. Go to the editor
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 whether a given array of integers contains any duplicate element. Return true if any value appears at least twice in the said array and return false if every element is distinct. Go to the editor
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. Go to the editor
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. Go to the editor
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 highest product from a given array of integers. Go to the editor
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 contains six integers. Also print all the members of the array. Go to the editor
Sample Output:
10
20
30
40
50
Click me to see the sample solution

19. Write a Python program to get an array buffer information. Go to the editor
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. Go to the editor
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. Go to the editor
Sample Output:
4
4
Click me to see the sample solution

22. Write a Python program to read a string and interpreting the string as an array of machine values. Go to the editor
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 to remove all duplicate elements from a given array and returns a new array. Go to the editor
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. Go to the editor
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



Python: Tips of the Day

Calling an external command from Python:

The subprocess module in the standard library:

Example:

import subprocess
subprocess.run(["ls", "-l"])

The advantage of subprocess vs. system is that it is more flexible (you can get the stdout, stderr, the "real" status code, better error handling, etc...).

The official documentation recommends the subprocess module over the alternative os.system():

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function [os.system()].

The Replacing Older Functions with the subprocess Module section in the subprocess documentation may have some helpful recipes.

For versions of Python before 3.5, use call:

import subprocess
subprocess.call(["ls", "-l"])

Ref: https://bit.ly/2ANeuPR