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

Slicing in Python

The document explains list slicing in Python using the example of a list named 'birds' containing four elements. It covers different slicing techniques, including selecting specific ranges, omitting start or end indices, and slice assignment, demonstrating how to manipulate lists. The outputs for each slicing operation are provided, showing the resulting lists after each operation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Slicing in Python

The document explains list slicing in Python using the example of a list named 'birds' containing four elements. It covers different slicing techniques, including selecting specific ranges, omitting start or end indices, and slice assignment, demonstrating how to manipulate lists. The outputs for each slicing operation are provided, showing the resulting lists after each operation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1.

Slicing in python:
Program
birds = ['parrot', 'dove', 'duck', 'cuckoo']
print(birds[2:4])
Explanation
Line 1:
birds = ['parrot', 'dove', 'duck', 'cuckoo']
 This line creates a list named birds.
 The list contains four string elements.
 Index positions of the list are:

Index Element

0 'parrot'

1 'dove'

2 'duck'

3 'cuckoo'

Line 2:
print(birds[2:4])
 This uses list slicing.
 Syntax: list[start : end]
 start = 2 → slicing starts from index 2 (included)
 end = 4 → slicing stops before index 4 (excluded)
So it selects elements at index 2 and 3.
That means:
 birds[2] → 'duck'
 birds[3] → 'cuckoo'

Output
['duck', 'cuckoo']

1. Slicing in python.
Program:
birds = ['parrot', 'dove', 'duck', 'cuckoo']
print(birds[:3])
Explanation
Line 1:
birds = ['parrot', 'dove', 'duck', 'cuckoo']
 This line creates a list named birds.
 The list contains four elements.
 Index positions are:

Index Element
0 'parrot'
1 'dove'
2 'duck'
3 'cuckoo'

Line 2:

print(birds[:3])
This statement uses list slicing.
Syntax:
list[start : end]

 Here, the start index is not given, so Python starts from index 0 by default.

 end = 3 means slicing stops before index 3.

 So it selects elements at index 0, 1, and 2.

That means:

'parrot'

'dove'

'duck'

Output
['parrot', 'dove', 'duck']
2. Slicing in Python:
Program:
birds = ['parrot', 'dove', 'duck', 'cuckoo']

print(birds[2:])

Explanation

Line 1:

birds = ['parrot', 'dove', 'duck', 'cuckoo']

 This line creates a list named birds.

 The list contains four string elements.

 Index positions of the list are:

Index Element

0 'parrot'

1 'dove'

2 'duck'

Line 2:

print(birds[2:])

 This statement uses list slicing.

Syntax:

list[start : end]

 start = 2 → slicing starts from index 2 (included).

 end is not specified, so Python goes up to the last element of the list.

 So it selects elements from index 2 to the end.

 That means:

'duck'

'cuckoo'

Output

['duck', 'cuckoo']

3. Slicing in Python:
Program

birds = ['parrot', 'dove', 'duck', 'cuckoo']

print(birds[:])

Explanation

Line 1:

birds = ['parrot', 'dove', 'duck', 'cuckoo']

 This line creates a list named birds.

 The list contains four string elements.

 Index positions are:

Index Element

0 'parrot'

1 'dove'

2 'duck'

3 'cuckoo'

Line 2:

print(birds[:])

 This statement uses list slicing.

 Syntax: list[start : end]

 Here, both start and end indices are omitted.

 When both are omitted, Python selects all elements in the list.

So the entire list is returned.

Output

['parrot', 'dove', 'duck', 'cuckoo']

4. Slicing in python:

Program
t = ['a', 'b', 'c', 'd', 'e', 'f']

t[1:3] = ['x', 'y']

Explanation

Line 1:

t = ['a', 'b', 'c', 'd', 'e', 'f']

 This line creates a list named t.

 The list contains six elements.

 Index positions are:

Index Element

0 'a'

1 'b'

2 'c'

3 'd'

4 'e'

Line 2:

t[1:3] = ['x', 'y']

 This uses slice assignment.

 t[1:3] refers to elements from index 1 to 2 ('b' and 'c').

 These elements are replaced by the new list ['x', 'y'].

So:

 'b' → 'x'

 'c' → 'y'

 The length remains the same because two elements are replaced by two elements.

Line 3:

 This displays the updated list t.

Output
['a', 'x', 'y', 'd', 'e', 'f']

You might also like