GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Array - Exercises, Practice, Solution - w3resource

  • Home
  • Python Home
  • ▼Python Exercises
  • Exercises Home
  • ▼Python Basic
  • Basic - Part-I
  • Basic - Part-II
  • Python Programming Puzzles
  • Mastering Python
  • ▼Python Advanced
  • Python Advanced Exercises
  • ▼Python Control Flow
  • Condition Statements and Loops
  • Recursion
  • ▼Python Data Types
  • String
  • JSON
  • List
  • List Advanced
  • Dictionary
  • Tuple
  • Sets
  • Collections
  • Array
  • Enum
  • ▼Python Class
  • Class
  • ▼Python Concepts
  • Python Unit test
  • Python Exception Handling
  • Python Object-Oriented Programming
  • Decorator
  • ▼Functional Programming
  • Functions
  • Lambda
  • Map
  • Itertools
  • Filter Function
  • ▼Date and Time
  • Date Time
  • Pendulum Module
  • ▼File Handling
  • File I/O
  • CSV Read, Write
  • ▼Regular Expressions
  • Regular Expression
  • ▼Data Structures and Algorithms
  • Search and Sorting
  • Linked List
  • Binary Search Tree
  • Heap queue algorithm
  • Bisect
  • ▼Advanced Python Data Types
  • Boolean Data Type
  • None Data Type
  • Bytes and Byte Arrays
  • Memory Views exercises
  • Frozenset Views exercises
  • NamedTuple exercises
  • OrderedDict exercises
  • Counter exercises
  • Ellipsis exercises
  • ▼Concurrency and Threading
  • Threading
  • Asynchronous
  • ▼Python Modules
  • Modules
  • Operating System Services
  • Math
  • Requests
  • SQLite Database
  • SQLAlchemy
  • PPrint
  • ▼Miscellaneous
  • Cyber Security
  • Generators Yield
  • ▼Python GUI Tkinter, PyQt
  • Tkinter Home
  • Tkinter Basic
  • Tkinter Layout Management
  • Tkinter Widgets
  • Tkinter Dialogs and File Handling
  • Tkinter Canvas and Graphics
  • Tkinter Events and Event Handling
  • Tkinter Custom Widgets and Themes
  • Tkinter File Operations and Integration
  • PyQt Basic
  • PyQt Widgets
  • PyQt Connecting Signals to Slots
  • PyQt Event Handling
  • ▼Python NumPy
  • Python NumPy Home
  • ▼Python urllib3
  • Python urllib3 Home
  • ▼Python Metaprogramming
  • Python Metaprogramming Home
  • ▼Python GeoPy
  • Python GeoPy Home
  • ▼BeautifulSoup
  • BeautifulSoup Home
  • ▼Arrow Module
  • Arrow Home
  • ▼Python Pandas
  • Python Pandas Home
  • ▼Pandas and NumPy Exercises
  • Pandas and NumPy Home
  • ▼Python Machine Learning
  • Machine Learning Home
  • TensorFlow Basic
  • ▼Python Web Scraping
  • Web Scraping
  • ▼Python Challenges
  • Challenges-1
  • ▼Python Mini Project
  • Python Projects
  • ▼Python Natural Language Toolkit
  • Python NLTK
  • ▼Python Project
  • Novel Coronavirus (COVID-19)
  • ..More to come..

Python: Array Exercises, Practice, Solution

Last update on July 05 2025 07:14:25 (UTC/GMT +8 hours)


This resource offers a total of 120 Python Array problems for practice. It includes 24 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

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.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Create an Array and Access Elements

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. Append a New Item to the End of an Array

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. Reverse the Order of Items in an Array

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. Get the Length in Bytes of One Array Item

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. Get Array Buffer Information

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. Count occurrences of a specified element in an array

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. Append Items from an Iterable to the End of the Array

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. Convert an array to machine values and return bytes

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. Append Items to a Specified List (from Array)

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. Insert an Item Before the Second Element in an Array

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. Remove a Specified Item Using the Index of an Array

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. Remove first occurrence of a specified element from an array

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. Convert an Array to an Ordinary List with the Same Items

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. Check if an Array Contains Any Duplicate Elements

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. Find the First Duplicate Element in an Array

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. Check Whether a List Follows a Given Pattern

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. Find a Pair with the Highest Product from an Array

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. Create an Array of Six Integers and Print All Members

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. Get Array Buffer Information

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. Get the Length of an Array

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. Get the Array Size of Unsigned Integer and Float Types

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. Read a String and Interpret It as an Array of Machine Values

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. Remove All Duplicate Elements from an Array

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. Find Missing Number in an Array of Numbers Between 10 and 20

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



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.