Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Array Exercises (Complete Guide for Beginners)

 Practicing exercises is the best way to master Python arrays. Arrays help you store multiple values of the same data type, and they are widely used in data processing, algorithms, and real-world applications.

In this tutorial, you will find Python array exercises with solutions ranging from beginner to intermediate level.


Creating an Array

We use Python’s array module.

import array as arr

numbers = arr.array('i', [10, 20, 30, 40, 50])

Exercise 1: Create an Array

Task:

Create an array of 5 integers and print it.

Solution:

import array as arr

numbers = arr.array('i', [1, 2, 3, 4, 5])

print(numbers)

Exercise 2: Access First and Last Element

Task:

Print the first and last elements.

Solution:

print(numbers[0])
print(numbers[-1])

Exercise 3: Add an Element

Task:

Add 60 to the array.

Solution:

numbers.append(60)
print(numbers)

Exercise 4: Insert Element at Position

Task:

Insert 99 at index 2.

Solution:

numbers.insert(2, 99)
print(numbers)

Exercise 5: Remove an Element by Value

Task:

Remove value 30.

Solution:

numbers.remove(30)
print(numbers)

Exercise 6: Remove Element by Index

Task:

Remove element at index 1.

Solution:

numbers.pop(1)
print(numbers)

Exercise 7: Loop Through Array

Task:

Print all elements using a loop.

Solution:

for num in numbers:
    print(num)

Exercise 8: Reverse an Array

Task:

Reverse the array.

Solution:

numbers.reverse()
print(numbers)

Exercise 9: Find Length of Array

Task:

Find number of elements in array.

Solution:

print(len(numbers))

Exercise 10: Find Index of a Value

Task:

Find index of value 30.

Solution:

print(numbers.index(30))

Exercise 11: Sum of Array Elements

Task:

Calculate total sum of elements.

Solution:

total = 0

for num in numbers:
    total += num

print(total)

Exercise 12: Find Maximum Value

Task:

Find the largest number in array.

Solution:

print(max(numbers))

Exercise 13: Find Minimum Value

Task:

Find smallest number.

Solution:

print(min(numbers))

Exercise 14: Copy Array

Task:

Create a copy of array.

Solution:

new_array = numbers.copy()
print(new_array)

Exercise 15: Join Two Arrays

Task:

Combine two arrays.

array1 = arr.array('i', [1, 2, 3])
array2 = arr.array('i', [4, 5, 6])

Solution:

array1.extend(array2)
print(array1)

Real-World Practice: Student Marks System

import array as arr

marks = arr.array('i', [85, 90, 78, 88, 92])

marks.append(95)
marks.remove(78)
marks.reverse()

total = sum(marks)

print("Marks:", marks)
print("Total:", total)

Real-World Practice: Inventory System

import array as arr

inventory = arr.array('i', [10, 20, 30, 40])

inventory.append(50)
inventory.pop(2)

for item in inventory:
    print(item)

Common Mistakes

Mistake 1: Mixing data types

❌ Wrong:

arr.array('i', [10, "hello"])

✔ Arrays must contain same type


Mistake 2: Using index out of range

numbers[10]

✔ Causes IndexError


Mistake 3: Forgetting import array module

numbers = array('i', [1,2,3])

✔ Must import array


Conclusion

Practicing array exercises helps you build strong programming logic and real-world problem-solving skills.

You learned:

  • How to create and modify arrays
  • How to loop, sort, and manipulate data
  • Common array operations
  • Real-world applications

Keep practicing these exercises to improve your Python programming skills.




Post a Comment

0 Comments