Regarding strings in Python, we know some_string[0] is the index of the first character of string some_string. Why is some_string[-1] the index of the last character instead of some_string[-0]? Would python evaluate -0 to 0 anyway?
Intuitively, 0 is the first element in the string. So -1 is the one before it, or in other words, the last element, cause it's wrapping. In other words, the negative sign doesn't signify subtraction. it just signifies the value of the number inside the brackets. so -0 = 0 and -1 = -1 and 1=1.
I think you got confused cause you thought the -0 minus one less than 0. but it doesn't, hope that helps.
to do that, i believe you'd use the actual subtract operator, like --. not just one -. Make sense?
\(\color{blue}{\text{Originally Posted by}}\) @ratinthecat Would python evaluate -0 to 0 anyway? \(\color{blue}{\text{End of Quote}}\) This is one reason, but there is another reason though. Here is one way to think of python indexing for negative values: `some_string[i] == some_string[len(some_string) + i]` Since the `len(...) - 1` gives the last index, the `-1` gives the last element.
These explanations really help. Thanks Wio and Curry!
Join our real-time social learning platform and learn together with your friends!