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 to convert a string into datetime?

0 Answers  


Can you list down some of the pdb commands for debugging python programs?

0 Answers  


How to change a string in list ?

0 Answers  


What is the sleeping time of giraffe?

0 Answers  


What is the purpose of #!/usr/bin/pythonon the first line in the above code? Is there any advantage?

0 Answers  






What makes python object-oriented?

0 Answers  


Can we use else block with for loop? Answer with one example.

0 Answers  


How to import a module given the full path?

0 Answers  


Write a one-liner that will count the number of capital letters in a file. Your code should work even if the file is too big to fit in memory.

1 Answers  


How do you remove duplicates from a list whilst preserving order?

0 Answers  


What does * args mean in python?

0 Answers  


Name some commonly used built-in modules in python?

0 Answers  


Categories