new to python. Learn indexing and slicing. what does it mean.
for i in range (len(number)):
if(number[-i-1]=='0'):
i don't understand this [-i-1]
new to python. Learn indexing and slicing. what does it mean.
for i in range (len(number)):
if(number[-i-1]=='0'):
i don't understand this [-i-1]
the number I'm assuming is a list of numbers eg. [0,1,2,3,4]
the number[-i-1] refers to the number at the index of -i-1 (indexing)
for example when i = 0, -i-1 = -1, which in python means the last index or 1 from the back
which in my example would be 4
Slicing would be like numbers[2:4], which will return a portion of the array of [2,3]
What you are showing is indexing say your array is
number=[2,4,6,8,10]
number[0] is 2
number[-1] is 10
So, If you want to get the ith number from front the indexing is
number[i]
If you want to get the ith number from rear,which is your case then the indexing looks like
number[-i-1]
Just for your information slicing is used to get a part of the sequence in the array
number[1:4] gives [4,6,8]
number[:4] gives [2,4,6,8]
number[4:] gives [10]
Hope this helps