Computer Science Grade 8 20 min

Arrays and Lists

Arrays and Lists

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what an array or list is and explain its purpose. Create a new array/list and populate it with initial data. Retrieve a specific element from an array/list using its index. Add a new element to the end of an existing array/list. Determine the total number of elements in an array/list (its length). Iterate through all elements of an array/list using a loop. Identify and correct common 'index out of bounds' and 'off-by-one' errors. How does your phone store your contacts or your favorite playlist? 📱 It uses a special container to keep everything in order, just like a list! In this lesson, you'll learn about one of the most fundamental data structures in computer science: arrays (often called lists). We'll explore how...
2

Key Concepts & Vocabulary

TermDefinitionExample Array / ListAn ordered collection of items stored in a single variable. Think of it as a container that holds multiple values in a specific sequence.A list of weekend chores: `chores = ["Wash Dishes", "Walk Dog", "Do Homework"]` ElementA single item or value within an array/list.In the list `[10, 20, 30]`, the number `20` is an element. IndexThe position of an element in an array/list. Indexing almost always starts at 0 for the first element.In `chores = ["Wash Dishes", "Walk Dog", "Do Homework"]`, the element `"Walk Dog"` is at index `1`. Length (or Size)The total number of elements in an array/list.The list `[10, 20, 30]` has a length of 3. Zero-Based IndexingThe concept that the first element of...
3

Core Syntax & Patterns

Creating a List list_name = [element1, element2, element3] Use square brackets `[]` to define a list. Separate each element with a comma. Accessing an Element list_name[index] Use the list's name followed by the index of the element you want in square brackets. Remember that the first element is at index 0. Adding an Element (Append) list_name.append(new_element) Use the `.append()` method to add a new element to the very end of the list. Finding the Length len(list_name) Use the `len()` function to get the total count of elements in the list.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A program is meant to print every guest from `guests = ["Eve", "Frank", "Grace"]`. The code is: `i = 0; while i <= len(guests): print(guests[i]); i = i + 1;`. What is the critical error in this code?
A.The loop will be infinite because `i` is never changed.
B.It will cause an 'index out of bounds' error on the last iteration.
C.It will skip the first guest, "Eve".
D.The code has no errors and will work perfectly.
Challenging
You have a list `scores = [10, 5, 8]`. You want to double each score and store it in a new list. Which logic correctly accomplishes this?
A.Create a new list, then iterate through `scores`. For each score, append `score * 2` to the new list.
B.Create a new list, then just append `scores * 2` to it one time.
C.Iterate through `scores`. For each score, run `scores.append(score * 2)`.
D.Simply write `new_scores = scores * 2`.
Challenging
A student runs the following sequence of commands: `subjects = ["Math", "Science"]; subjects.append("History"); subjects.append("Art"); second_subject = subjects[len(subjects) - 3];`. What is the value of `second_subject`?
A."Math"
B."Science"
C."History"
D.An error

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Data Structures

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.