adspace


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

How would you display a file’s contents in reversed order?

937


list some of the data science libraries in python

947


Is there a way to remove the last object from a list?

1007


What is the use of assertions in python?

1176


How to read a 10gb (or larger) file size in python?

893


How do you write if else in python?

1046


How do I list all files of a directory?

937


How do I download a file over http using python?

925


What is the biggest challenge facing your current job right now? What is your biggest failure?

873


What is the best notepad?

890


Explain the inheritance in python with an example?

985


What is the process to get the home directory using ‘~' in python?

1015


Tell me what are different methods to copy an object in python?

1023


How do you check if a list is empty in python?

955


What is the length of your largest python code? Can you please describe the project?

1133