+91 81900 98289

Python Arrays

This Python Arrays lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Most Python code uses a list where other languages say array. The array module holds only one numeric type and is less common in tutorials.

A list can mix types. An array from the array module cannot.

Example 1: Start here

Reach for a list first. Use array or NumPy only when you need their extra rules.

marks = [71, 82, 90] # a list; the values stay in this order print(sum(marks) / len(marks)) # len counts the items

Example 2: A list used as an array

For most beginner code, a list is the array.

scores = [70, 80, 90] # a list; the values stay in this order print(scores[1])

Example 3: Fill with zeros

Multiplying a one-item list builds a row of starting values.

slots = [0] * 4 # a list; the values stay in this order slots[2] = 5 # change this one field print(slots)

Example 4: The array module

array.array stores one kind of number. The i means integers.

import array # load a tool that comes with Python scores = array.array("i", [70, 80, 90]) # the value at this position print(scores[0])
Reach for a list first. Use array or NumPy only when you need their extra rules.
Call Now 81900 98289