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 is distutils?

418


What is @property in python?

456


Will the do-while loop work if you don’t end it with a semicolon?

462


Should I learn html before python?

454


Why cannot lambda forms in Python contain statements?

562






Differentiate *args and **kwargs?

470


What is difference between tuple and list?

411


How will you check in a string that all characters are digits?

466


What is the best interpreter for python?

466


What does netflix use python for?

466


Which python memory profiler is recommended?

435


Why is numpy so fast?

552


Which programming language is a good choice between java and python?

465


How do I sort a list in python 3?

586


What is self in oop of python?

466