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
Do they know a tuple/list/dict when they see it?
Which of the languages does Python resemble in its class syntax?
Write a program to find the given string in the line?
Tell me what are different ways to create an empty numpy array in python?
Is it possible to inherit one class from another class?
How python supports encapsulation with respect to functions?
What is a for loop in python?
Why to use python numpy instead o f lists?
Why was the language called as Python?
How does python handle compile-time and run-time code checking?
Is the jupyter notebook good for python?
Tell us what is python? What are the benefits of using python?
What are object methods?
What is the best free website to learn python?
How would you convert a string into lowercase in python?