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

How can I merge two python dictionaries in a single expression?

1 Answers  


What is the difference between methods & constructors?

1 Answers  


Difference between __str__ and __repr__?

1 Answers  


Is python good for microservices?

1 Answers  


How do you open an already existing file and add content to it?

1 Answers  


What is difference between list and set in python?

1 Answers  


What does time time () do in python?

1 Answers  


What are python libraries? Name a few of them.

1 Answers  


Explain join() and split() in python.

1 Answers  


What is the use of assertions in python?

0 Answers  


Why is python good for beginners?

1 Answers  


Finally, tell us about bitwise operators in python?

1 Answers  


Categories