16

Phase 4 - Level 1: Basic Arrays

Chapter 16 • Beginner

60 min

Phase 4 - Level 1: Basic Arrays

Introduction to Arrays

Arrays (lists in Python) allow you to store multiple values in a single variable. They are essential for working with collections of data.

Array Basics

Creating Arrays

python.js
arr = [1, 2, 3, 4, 5]  # List of numbers
arr = []  # Empty list

Accessing Elements

  • Index starts at 0
  • arr[0] - first element
  • arr[-1] - last element

Common Operations

  • len(arr) - length
  • arr.append(x) - add element
  • arr[i] - access element at index i

Key Concepts

  1. Indexing: Access elements by position (0-based)
  2. Iteration: Loop through all elements
  3. Accumulation: Sum, count, find max/min
  4. Input/Output: Read and display arrays

Common Patterns

  • Input n elements into array
  • Sum all elements
  • Find maximum/minimum
  • Count elements meeting condition

Hands-on Examples

Input and Print Array

# Take n as input
n = int(input("Enter number of elements: "))

# Input array
arr = []
print("Enter elements:")
for i in range(n):
    element = int(input())
    arr.append(element)

# Print array
print("Array elements:")
for element in arr:
    print(element, end=" ")
print()

Use a loop to input n elements and append to list. Then iterate through list to print all elements.