YOUR SAVED CODE-ALONG
Code along: Access items and slices
Keep the lesson open on your other screen. Your edits stay in this workspace, separate from the practice activity. Sign in to save and reload a private account draft across devices, or share a snapshot when you need help.
CODE-ALONG / THEATER MODE
Access items and slices
ORIGINAL WALKTHROUGH · 11:26
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Video settings & progress
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.
Start the video to enable typing controls.
YOUR TURN
Code along: Access items and slices
Compare positive and negative indexes. Trace included starts and excluded slice stops. Try a step of 2 and a reversed slice; inspect the unchanged original. Pause the original recording to type and run your examples. This open workspace saves separately from scored practice and has no fixed-answer checks.
Load an example, edit it and run it. Word wrap is enabled.
Your check results
Lesson reference
Examples load into this same code-along only after you confirm replacing your code.
Compare the code and its result
Choose an example. The highlighted code and its checked output change together. This demonstration does not change your saved practice.
books = ["Sky", "River", "Forest", "Ocean"]
print(books[0])Sky
Index 0 selects the first item. The result is one string, not another list.
books = ["Sky", "River", "Forest", "Ocean"]
print(books[-1])Ocean
Negative one selects the last item without calculating its positive index.
books = ["Sky", "River", "Forest", "Ocean"]
print(books[1:3])['River', 'Forest']
The slice includes positions 1 and 2, but excludes position 3. Its result is a new list.
The first position is zero
With four items, valid nonnegative indexes are 0 through 3. Negative indexes count backward: -1 selects the last item and -4 selects the first. There is no special last-item index -0; it is just 0.
An out-of-range single index raises IndexError. An empty list has no first or last item.
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-1]) # date
print(fruits[-4]) # appleStart is included; stop is excluded
fruits[1:3] selects indexes 1 and 2. The stop marks where selection ends, not the final selected index. Slicing returns a new, shallow list; it does not remove those items from the original.
Omit the start to begin at zero and omit the stop to continue to the end. Slice bounds beyond the list are clipped instead of raising the index error shown by single-item access.
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[1:3])
print(fruits[:2])
print(fruits[2:])
print(fruits[99:]) # []A step describes the distance between selected indexes
numbers[::2] selects indexes 0, 2 and 4; it does not mean select the items whose values are even. A step of -1 reverses traversal in a new list. A step of zero raises ValueError.
The source asks for three middle numbers from ten values. Ten has no single middle item; this course explicitly chooses indexes 3, 4 and 5, giving 40, 50 and 60.
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[::2]) # [10, 30, 50]
print(numbers[::-1])
print(numbers) # unchangedBuild it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)5 steps · Saved work · Instant feedback
See the attached lesson’s comic
