0% found this document useful (0 votes)
2 views2 pages

Python Slicing Notes

Slicing in Python allows for extracting a subset of sequences such as lists, strings, tuples, or NumPy arrays using a specified range of indices. The syntax for slicing is sequence[start:stop:step], where 'start' is inclusive, 'stop' is exclusive, and 'step' defines the interval between elements. Slicing is a powerful feature that enables efficient data access without the need for loops.

Uploaded by

12biog1jaiharij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Slicing Notes

Slicing in Python allows for extracting a subset of sequences such as lists, strings, tuples, or NumPy arrays using a specified range of indices. The syntax for slicing is sequence[start:stop:step], where 'start' is inclusive, 'stop' is exclusive, and 'step' defines the interval between elements. Slicing is a powerful feature that enables efficient data access without the need for loops.

Uploaded by

12biog1jaiharij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Slicing in Python

Python Slicing Concepts

What is Slicing in Python?

Slicing in Python means extracting a portion (subset) of a sequence like a list, string, tuple, or NumPy array

using a range of indices.

Syntax:

sequence[start : stop : step]

- start: Index to start from (inclusive)

- stop: Index to stop at (exclusive)

- step: Interval between elements

Examples with a List:

numbers = [10, 20, 30, 40, 50, 60]

1. numbers[1:4] => [20, 30, 40]

2. numbers[:3] => [10, 20, 30]

3. numbers[3:] => [40, 50, 60]

4. numbers[::2] => [10, 30, 50]

5. numbers[::-1] => [60, 50, 40, 30, 20, 10] (Reverses list)

Slicing Strings:

text = "PYTHON"

1. text[1:4] => 'YTH'

2. text[::-1] => 'NOHTYP'

Slicing NumPy Arrays:

import numpy as np
Slicing in Python

a = np.array([[1, 2, 3],

[4, 5, 6]])

a[0, :] => [1 2 3] # First row

a[:, 1] => [2 5] # Second column

Slicing is a powerful feature in Python that helps access data quickly and efficiently without loops.

You might also like