Page 315 - AI Ver 1.0 Class 9
P. 315
print([1, 2, 6] != [1, 2, 5]) #True
print([1, 2, 6] > [1, 2, 5]) #True
Slicing a List
Slicing a list means accessing a specific portion of a list. This can be done by using a range of values with start
index number, end index number with colon in between. We can use both forward index and backward index to
specify the range of slicing a list. Some important points to consider to do slicing in a list are:
• Elements from beginning to a range - list[ : index] or [ : -index]
• Elements from specific Index till the end - list[index:]
• Elements within a range - list[start index : end index]
• Elements within range using step value - list[start index : end index : step value]
• Whole list in forward order- list[ : ]
• Whole list in reverse order - list[ : : -1].
For example,
word = ['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O', 'N']
Forward Indexing 0 1 2 3 4 5 6 7 8
E D U C A T I O N
–9 –8 –7 –6 –5 –4 –3 –2 –1 Backward Indexing
Examples Commands Output
To display the whole list word[:] ['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O', 'N']
or
word[::]
or
word[0:]
or
word[-9:]
Displays the alternate elements print(word[::2] ['E', 'U', 'A', 'I', 'N']
of the given list
)
To display ['E', 'D', 'U'] from the word[0:3] ['E', 'D', 'U']
above list
or
word[:3]
or
word[-9:-6]
or
word[:-6]
Introduction to Python 313

