Posts

Showing posts with the label indexing

Explain slicing and indexing operations.

Slicing and Indexing operations : Indexing operation allows to access a particular item in the sequence directly. Slicing operation allows to retrieve a part of the sequence. In slicing : S[ StartNo : StopNo : Step ] If the start no is omitted, Python will start at the beginning of the sequence. If the stop no is omitted, Python will stop at the end of the sequence. >>> a = [1,2,3,4] >>> a[0:2] # take items 0-2, upper bound noninclusive [1, 2] >>> a[0:-1] #take all but the last [1, 2, 3] >>> a[1:4] [2, 3, 4] >>> a[::-1] # reverse the list [4, 3, 2, 1] >>> a[::2] # skip 2 [1, 3]