loop
Operations on lists

Module 3 · Operations on lists · Lesson 5 of 5

Examples

Three ideas, one section: lists are references, slices are powerful, and in answers a yes/no question. Here they are at work — read each one, predict the output, then run it.

1. Edit safely with a copy. Because assignment shares a list, take a slice copy whenever you want to change one version while keeping another intact.

Run it yourselfruns in your browser · Python
main.py

2. Slice from both ends. Positive and negative indices, omitted bounds, and a negative step give you the front, the back, the middle, and a reversal — all in one notation.

Run it yourselfruns in your browser · Python
main.py

3. Membership as a gate. in and not in decide whether to act. Run this one and type an item — try potion (already there), then something new.

Run it yourselfruns in your browser · Python
main.py

That rounds out lists: you can build them, read and change them, sort them, copy them safely, slice them every which way, and ask what's inside. That's a complete, practical toolkit — and the foundation for every collection Python has.