Explain indexing and slicing operation in sequences

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What are all the operating system that python can run on?

470


Why is python good for beginners?

435


Write program to find the common elements from two sets?

458


How do you copy an object in python?

499


Why lambda forms in python does not have statements?

557






What is the syntax for exponentiation and give example?

456


What is python package manager (pypm)?

521


What is python good for?

522


What is means by string python?

436


How to avoid having class data shared among instances in python?

463


How do I delete a file?

503


What is subclass in python?

423


How to send a object and its value to the garbage collection?

464


Can we overload constructor in python?

467


Is html harder than python?

440