๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

Slicing

๐Ÿ“š What is Slicing? Slicing extracts a portion of a list or string using the syntax [start:end:step]. It returns a new list or string - the original is unchanged. Slicing is one of Python's most expressive features and is used constantly in data processing.

5 min 10 XP Lesson 16 of 21
Slicing
๐ŸŒ

Appy Saysโ€ฆ

Slicing is one of Python's most elegant features. Grab the first 10 items of a list, every other element, or reverse a string โ€” all in one expression. Once you understand slices, you'll use them in almost every program you write.

๐Ÿ“–

What are Slices?

A slice extracts a portion of a sequence (list, string, or tuple) using the [start:stop:step] syntax. Python returns a new sequence โ€” the original is unchanged.

  • โ€ขitems[2:5] โ€” elements at index 2, 3, 4 (stop is exclusive)
  • โ€ขitems[:3] โ€” first three elements (start defaults to 0)
  • โ€ขitems[3:] โ€” from index 3 to the end
  • โ€ขitems[::2] โ€” every other element (step of 2)
  • โ€ขitems[::-1] โ€” reverse the entire sequence
  • โ€ขitems[-3:] โ€” last three elements (negative indices count from the end)
๐ŸŽฎ

Think of it like a playlist clip

Imagine your Spotify playlist has 100 songs. A slice is like saying 'play songs 5 to 15' or 'play the last 3 songs' โ€” you're not deleting anything from the playlist, just picking a section to work with.

โš™๏ธ

How It Works

  • โ€ข1. seq[start:stop:step] โ€” all three parts are optional
  • โ€ข2. start defaults to 0, stop defaults to end, step defaults to 1
  • โ€ข3. Indices can be negative: -1 is the last element, -2 is second-to-last
  • โ€ข4. stop is always exclusive โ€” [0:3] gives indices 0, 1, 2
  • โ€ข5. A negative step reverses direction: [::-1] reverses the sequence
  • โ€ข6. Out-of-range indices are silently clipped โ€” no IndexError with slices
๐ŸŒ

Real-World Examples

  • โ€ขPaginate results: results[page*10 : (page+1)*10] โ€” 10 results per page
  • โ€ขGet file extension: filename[-4:] โ†’ '.png'
  • โ€ขReverse a string to check if it's a palindrome: word[::-1] == word
  • โ€ขShow preview of a long message: message[:100] + 'โ€ฆ'
  • โ€ขProcess every other frame in a video: frames[::2]
๐Ÿ’ก

Key Facts

  • โ€ขSlices work on strings, lists, tuples and any custom sequence type
  • โ€ขlist[:] creates a shallow copy of the entire list โ€” useful for avoiding mutation bugs
  • โ€ขNumPy arrays (used in data science and AI) heavily rely on slice syntax
  • โ€ขPython internally uses a slice object โ€” you can create one with slice(start, stop, step)
โš ๏ธ

Watch Out!

Slices return a shallow copy โ€” inner objects (like nested lists) are still shared with the original. If you slice a list of lists and modify an inner list, the change shows in both the slice and the original. Use copy.deepcopy() if you need full independence.

๐Ÿ“Œ

Remember

seq[start:stop:step] โ€” stop is exclusive, negatives count from the end, [::-1] reverses. Slicing never raises an IndexError.

โœ…

What You Learned

  • โ€ขSlices extract portions of sequences: seq[start:stop:step]
  • โ€ขNegative indices count from the end; [::-1] reverses any sequence
  • โ€ขUnlocks: pagination, text previews, data sampling, palindrome checks, and clean list manipulation

Key Facts

  • โ†’Slices work on strings, lists, tuples and any custom sequence type
  • โ†’list[:] creates a shallow copy of the entire list โ€” useful for avoiding mutation bugs
  • โ†’NumPy arrays (used in data science and AI) heavily rely on slice syntax
  • โ†’Python internally uses a slice object โ€” you can create one with slice(start, stop, step)

Real-World Examples

โ€ข Paginate results: <code>results[page*10 : (page+1)*10]</code> โ€” 10 results per page โ€ข Get file extension: <code>filename[-4:]</code> โ†’ <code>'.png'</code> โ€ข Reverse a string to check if it's a palindrome: <code>word[::-1] == word</code> โ€ข Show preview of a long message: <code>message[:100] + 'โ€ฆ'</code> โ€ข Process every other frame in a video: <code>frames[::2]</code>

Remember

seq[start:stop:step] โ€” stop is exclusive, negatives count from the end, [::-1] reverses. Slicing never raises an IndexError.

Quick Quiz

1 / 2

s[1:4] includes index 4?