Why does this assign a number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does this assign a number?

Why does this code assign numbers? letters = ['p', 'q', 'r', 's', 'p', 'u'] print(letters.index('r')) print(letters.index('p')) print(letters.index('q')) print(letters.index('r')) The code up top👆🏻👆🏻

3rd Sep 2018, 1:40 AM
Potato Hacker
Potato Hacker - avatar
2 Answers
+ 5
The index() method returns the lowest index position of the item in the parentheses in an iterable like a string or a list. so: str = "foo bar" print(str.index("f")) # output: 0 because "f" is the first item (i.e. at index position 0) lst = ["a", "b", "c"] print(lst.index("b")) # output: 1 because "b" is the second item (i.e. index position 1)
3rd Sep 2018, 2:30 AM
David Ashton
David Ashton - avatar
+ 1
thanks David Ashton!!
3rd Sep 2018, 1:30 PM
Potato Hacker
Potato Hacker - avatar