Explain indexing and slicing operation in sequences



Explain indexing and slicing operation in sequences..

Answer / chaitanya

Different types of sequences in python are strings, Unicode strings, lists, tuples, buffers, and xrange objects

Slicing & indexing operations are salient features of sequence.

indexing operation allows to access a particular item in the sequence directly ( similar to the array/list indexing) and the slicing operation allows to retrieve a part of the sequence.

The slicing operation is used by specifying the name of the sequence followed by an optional pair of numbers separated by a colon within square brackets say S[startno.:stopno].

The startno in the slicing operation indicates the position from where the slice starts and the stopno indicates where the slice will stop at.

If the startno is ommited, Python will start at the beginning of the sequence. If the stopno is ommited, Python will stop at the end of the sequence..

Following code will further explain indexing & slicing operation:

>>> cosmeticList =[‘lipsstick’,’facepowder’,eyeliner’,’blusher’,kajal’]

>>> print “Slicing operation :”,cosmeticList[2:]

Slicing operation :[‘eyeliner’,’blusher’,kajal’]

>>>print “Indexing operation :”,cosmeticList[0]

“Indexing operation :lipsstick

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

Explain about the use of python for web programming?

0 Answers  


What is operator overloading in python?

0 Answers  


What are class methods?

0 Answers  


How to add an index, row or column to a pandas dataframe?

0 Answers  


What is linspace in python?

0 Answers  






What is the difference between runtime and compile time?

0 Answers  


Differentiate between the append() and extend() methods of a list?

0 Answers  


What is a counter in python?

0 Answers  


What are the possible ways to load an array from a text data file in python? How can the efficiency of the code to load data file be improved?

0 Answers  


What’s the difference between a for loop and a while loop?

0 Answers  


What does nan mean in python?

0 Answers  


Why does comparing strings in python using either ‘==’ or ‘is’ sometimes produce a different result?

0 Answers  


Categories