Skip to the content

Slices

Python · Unit 5: Lists

items[a:b] takes a piece: from index a up to just before b. As in range, the end is left out.

The piece is a new list.

nums = [10, 20, 30, 40, 50]
print(nums[1:4])

Prints

[20, 30, 40]

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Multiple choice

    What is the difference between nums[2] and nums[2:3]?

  3. 3. Complete the code

    Complete it to keep 20, 30 and 40.

  4. 4. Predict the output

    The tournament points run from highest to lowest. What does it print?

  5. 5. Complete the code

    Complete it to print the last three temperatures.

  6. 6. Multiple choice

    After this code, what does row hold?

  7. 7. Predict the output

    What does this code print?

  8. 8. Put the lines in order

    Put the lines in order to print the countdown 3, 2, 1.

  9. 9. Find the bug

    The race times run from fastest to slowest. The program should print the top three. Which line has the error?

  10. 10. Predict the output

    Ava is already at the register. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →