Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m