0% found this document useful (0 votes)
40 views8 pages

Class 11-Python Lists-Slicing

The document provides an overview of list slicing in Python, explaining how to extract subsets of lists using specific syntax. It includes examples demonstrating various slicing techniques and their outputs. Additionally, it discusses how Python handles out-of-bounds indices without raising errors, returning empty sequences instead.
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)
40 views8 pages

Class 11-Python Lists-Slicing

The document provides an overview of list slicing in Python, explaining how to extract subsets of lists using specific syntax. It includes examples demonstrating various slicing techniques and their outputs. Additionally, it discusses how Python handles out-of-bounds indices without raising errors, returning empty sequences instead.
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/ 8

CLASS XI

ARTIFICIAL INTELLIGENCE
PYTHON – SLICING IN LISTS
LIST SLICING

Slicing: A slice of a list is a subset (a part) of the list.

For example, if [5,2,3,8,9] is a list, then some of its slices can be [5,2,3], [3,8,9], [2], [2,9],
[9,8,3,2] etc.

In Python we can extract any slice of a list by specifying the list name, start index, stop
index, and step size

syntax:
listName[start:stop:step]
This will extract the elements of the list starting from index start till (but not including)
index stop in steps of step.

This is similar to the indices generated by range(start, stop, step).


SLICING - SOLVED EXAMPLE USING LISTS

College= [“IIT”, “NIT”, “College of Engg.”,”DTU”]

print(College[0]) Output => IIT


print(College[2]) Output => College of Engg.
print(College[1:3]) ['NIT', 'College of Engg.']
SLICING - SOLVED EXAMPLE USING LISTS

• List=['G','O','O','D','M','O‘,'R','N','I','N','G']
• print(List)
• print(List[:]) #prints all list elements
• print(List[5] #prints ['O', 'R', 'N', 'I', 'N', 'G']

• Sliced_List=List[3:8]
• print(Sliced_List) #['D', 'M', 'O', 'R', 'N']
• Further we way use the above general syntax in different ways to slice the strings:

i. List[start:end] Extract elements from start to end -1.


ii. List[start:] Extract elements from start to end.
Extract elements from 0 th index to end-1
iii. List[:end]
Extract elements from start to end-1
iv. List[-start:]
Extract elements from 0th index to end-1
v. List[:-end] Extract the entire List 0th index to till last index.
vi. List[:] This will print alternative element according to step size.
vii. List[ : : step]

Note:

• In the above syntax start value is included and end value is excluded.
LIST SLICING EXAMPLES
INTERESTING INFERENCE IN LIST SLICING

Giving upper limit way beyond the size of the list. But,
Python return elements from list falling in range 3
onwards <30.

Giving lower limit much lower, But, Python return


elements from list falling in range -15 onwards <4

Giving one limit is out of bound, But, Python return


elements from list falling in range 6 onwards <10

Python gives no error and returns an empty sequence


as no element of S has index falling in range of 10 to
20.
Mention the output of the following codes

a.
day= ['f','r','i','d','a','y']
print(a[-1])
print(a[-6])

b.
n_list=[“Happy”,[2,0,1,5]]
print(n_list[0][1])
print(n_list[1][3])

You might also like