Header Ads Widget

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

Python - Arrays (Complete Guide for Beginners)

 In Python, an array is a collection of items stored at contiguous memory locations. Arrays are used to store multiple values in a single variable, similar to lists, but with some important differences.

Python does not have built-in array support like C or Java, but it provides arrays through the array module and also uses lists as a flexible alternative.

In this tutorial, you will learn Python arrays, how they work, and how to use them effectively.


What is an Array?

An array is a data structure that stores:

  • Multiple values
  • Same data type items (in traditional arrays)

Example Concept:

Index:   0   1   2   3
Array:  10  20  30  40

Creating Arrays in Python

To use arrays in Python, we import the array module.

Syntax:

import array

Example 1: Creating an Integer Array

import array as arr

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

print(numbers)

Output:

array('i', [10, 20, 30, 40, 50])

Array Type Codes

Python arrays require a type code to define data type.

Type CodeTypeDescription
'i'IntegerSigned integer
'f'FloatFloating point
'd'DoubleFloating point
'u'UnicodeCharacter

Example 2: Float Array

import array as arr

prices = arr.array('f', [10.5, 20.3, 30.7])

print(prices)

Output:

array('f', [10.5, 20.299999237060547, 30.700000762939453])

Accessing Array Elements

You can access elements using index numbers.

Example:

import array as arr

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

print(numbers[0])
print(numbers[2])

Output:

10
30

Modifying Array Elements

Example:

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

numbers[1] = 99

print(numbers)

Output:

array('i', [10, 99, 30, 40])

Looping Through Arrays

Example:

for num in numbers:
    print(num)

Output:

10
99
30
40

Adding Elements to Array

Use append() to add one item.

Example:

numbers.append(50)

print(numbers)

Inserting Elements in Array

Use insert() to add at a specific position.

Example:

numbers.insert(1, 15)

print(numbers)

Removing Elements from Array

Use remove() to delete a value.

Example:

numbers.remove(99)

print(numbers)

Pop Method in Arrays

Removes an element by index.

Example:

numbers.pop(2)

print(numbers)

Array Length

Use len() to get the number of elements.

Example:

print(len(numbers))

Real-World Example: Student Marks

import array as arr

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

total = 0

for mark in marks:
    total += mark

print("Total Marks:", total)

Real-World Example: Product Prices

import array as arr

prices = arr.array('f', [99.99, 149.50, 200.75])

for price in prices:
    print(price)

Array vs List in Python

FeatureArrayList
Data TypeSame type onlyMixed types
PerformanceFasterSlower
Module RequiredYesNo
FlexibilityLessMore

When to Use Arrays?

Use arrays when:

  • You need fast processing
  • You work with large numeric data
  • You need memory efficiency
  • Data type is the same

Common Mistakes

Mistake 1: Mixing Data Types

❌ Wrong:

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

✔ Correct:

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

Mistake 2: Forgetting Type Code

❌ Wrong:

arr.array([10, 20, 30])

✔ Correct:

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

Array Methods Summary

MethodDescription
append()Add item at end
insert()Add item at index
remove()Remove value
pop()Remove index
index()Find index
reverse()Reverse array

Practice Exercise 1

Create an array of 5 integers and print all values.


Practice Exercise 2

Insert a number at index 2 and remove the last element.


Conclusion

Python arrays are powerful for handling structured numeric data efficiently.

You learned:

  • How to create arrays using array module
  • How to access and modify elements
  • How to add and remove items
  • Differences between arrays and lists
  • Real-world usage examples

Arrays are very useful in data processing, scientific computing, and performance-critical applications.




Post a Comment

0 Comments