anonymous
  • anonymous
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?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Curry
  • Curry
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.
Curry
  • Curry
I think you got confused cause you thought the -0 minus one less than 0. but it doesn't, hope that helps.
Curry
  • Curry
to do that, i believe you'd use the actual subtract operator, like --. not just one -. Make sense?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
\(\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.
anonymous
  • anonymous
These explanations really help. Thanks Wio and Curry!

Looking for something else?

Not the answer you are looking for? Search for more explanations.