Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does index mean in python?

Perhaps, it sounds like a stupid question. However, I have to learn what index is because I want to learn python.

For example, in a website, I saw this:

The method find() determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given.

http://www.tutorialspoint.com/python/string_find.htm

What does "index" mean here? I would like you to explain it to me like you are explaining something to a child. Because my comprehension is somewhat bad. Anyway. You can even provide examples to explain what index is.

Many thanks.

like image 358
Rakanoth Avatar asked Nov 28 '13 14:11

Rakanoth


People also ask

What is index in Python code?

Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the index of the first occurrence.

What is the purpose for index?

An index is a list of all the names, subjects and ideas in a piece of written work, designed to help readers quickly find where they are discussed in the text. Usually found at the end of the text, an index doesn't just list the content (that's what a table of contents is for), it analyses it.

What is index and example?

Index (indices) in Maths is the power or exponent which is raised to a number or a variable. For example, in number 24, 4 is the index of 2. The plural form of index is indices.

Does Python index 0 or 1?

The list index starts with 0 in Python. So, the index value of 1 is 0, 'two' is 1 and 3 is 2.


3 Answers

An index, in your example, refers to a position within an ordered list. Python strings can be thought of as lists of characters; each character is given an index from zero (at the beginning) to the length minus one (at the end).

For the string "Python", the indexes break down like this:

P y t h o n
0 1 2 3 4 5

In addition, Python supports negative indexes, in which case it counts from the end. So the last character can be indexed with -1, the second to last with -2, etc.:

 P  y  t  h  o  n
-6 -5 -4 -3 -2 -1

Most of the time, you can freely mix positive and negative indexes. So for example, if you want to use find only from the second to second-to-last characters, you can do:

"Python".find("y", beg=1, end=-2)
like image 61
Platinum Azure Avatar answered Oct 13 '22 00:10

Platinum Azure


"index" is meant as "position".

Let's use find() as an example: find() will look for a string in another string. It will start its search at the beginning index called beg and will end its search at the end index called end. So it will only search between beg and end. Usually (by default) beg is 0 (which means it is the first character in the string) and end is the length of the string minus one (which means it is the very last character in the string). So an index is just a position (not only in a string, e.g. also in an array).

like image 43
Thomas Uhrig Avatar answered Oct 13 '22 01:10

Thomas Uhrig


Consider this string "Hello". If you wanted to point out to some of it's characters like e you would need an index, which is a position number. Indices in python start counting from zero. So the index of letter e in "Hello" is 1.

Try to run this line of code:

print "Hello".find("e");

It should return you 1.

You can further play with it an run it again what it does. Try to replace "e" with "H", next try something that isn't in "Hello".

like image 36
nio Avatar answered Oct 13 '22 01:10

nio