PYTHON CORE · LESSON 27

Piece of list

Slices · about 11 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Take a piece.

The slice takes several elements. «конец» is no longer included.

pets = ["кот", "пёс", "хомяк"]
print(pets[0:2])

✦ It's a cat and a пёс.

STEP 2

We can start from scratch.

Numbers in brackets are seat numbers, not the values themselves.

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

✦ It's 20 and 30.

IN-CLASS PRACTICE

Type the code yourself

Reprint the list and slice from the sample.

nums = [2, 4, 6, 8]
print(nums[1:3])
Open the editor for class work
HOMEWORK

Solve it yourself

Create nums = [1, 2, 3, 4]. Output the cut with the second and third element.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework