runtric-logo
Python Basic 2 - DataLists: indexing, slicing, appending (1/5)
Chapter 1
Python Basic 2 - Data

Lists: indexing, slicing, appending

5 minutes read

Beginner friendly

Includes practice

Imagine a row of labeled boxes where you can drop anything: numbers, words, even other boxes.
That row is a list in Python—a flexible, ordered collection you can grow, shrink, and reorganize at will.

Why Lists Matter

  • Store many values in one variable instead of creating item1, item2, item3
  • Keep the original order (first in list is still first when you read it back).
  • Easily loop over all items to do work (print them, add them up, etc.).

Runtic's Learning Tips

  • Struggling at first is normal—each list operation you try is a mini-win that rewires your brain for coding.

  • Tinker actively: change values, rerun, break things, fix them. Hands-on beats passive reading every time.

  • When errors pop up, read them aloud and guess the cause—it trains your debugging muscle.

Creating Your First List

A list is written with square brackets [] and commas separating items.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Text between card placements to avoid clustering.

Quick Practice

3 minutes

Check your understanding of list creation.

Which line correctly creates an empty list?

What is len([3, 4, 5]) ?

Accessing Elements (Indexing)

Lists start counting from zero.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Quick Summary

  • Indexing uses square brackets: list_name[index]

  • Positive indices start at 0 from the left; negative indices start at -1 from the right

  • An out-of-range index triggers IndexError

Slicing: Grabbing a Sub-List

Want just a part of a list? Use start:stop (stop is exclusive).

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Some slice tricks:

  • Omit start: numbers[:3] → first three
  • Omit stop: numbers[2:] → everything from index 2 onward
  • Step value: numbers[::2] → every other number

Modifying Lists (Add, Change, Remove)

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Quick Practice

3 minutes

Test yourself on list modification.

Which method adds an item to the *end* of a list?

What is the result of lst = ['a','b']; lst.insert(1,'X') ?

Looping Through Lists

The for loop is your power drill—quickly processes each item.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Mini-Challenge

Practice Challenge

Interactive Code Editor
📝 Your Task:

Write a program that calculates the sum of all numbers in the list nums. Start total at 0, then loop through each number in nums and add it to total. Finally, print the total.

python

Use a for-loop to add each number in the list to total.

Quick Summary

  • for item in list: lets you read or modify every element

  • Combine loops with list methods for powerful data tasks

  • Practice builds fluency; tiny programs like this add up to big skills

Common Pitfalls to Watch For

  • Forgetting zero-based indexing → off-by-one bugs
  • Mixing data types unintentionally (e.g., integers and strings)
  • Using = instead of == in conditions involving list items
  • Assuming slicing is inclusive at both ends—remember, the stop index is exclusive
Lesson Complete!
100%
Progress
What You've Learned:
  • You can now create, read, slice, and modify Python lists confidently
  • Indexing & slicing rules (start at 0, stop is exclusive) are the key to error-free access
  • Loops plus list methods unlock real-world data tasks—keep experimenting to solidify knowledge

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.