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👆🏻👆🏻
2 ответов
+ 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)
+ 1
thanks David Ashton!!