+ 1
# please explain me the working of 3rd line in the code, cant we use "b=len(a))"
a=["vim","#?","skk","jsj"] b=len(a)-1 c=0 while c<=b: d=a[c] print(d) c=c+1 https://code.sololearn.com/cpwKJRvWI3LQ/?ref=app
4 odpowiedzi
+ 1
No we can't. This is because array indices start from 0
So
a[0] = "vim", a[1] = "#?", a[2] = "skk" and a[3] = "jsj"
As you can see, the last index is 3. So if we take b = len(c), b will be 4 and a[4] does not exist.
So we have to take
b = len(c) - 1
+ 2
I suppose that your code should read the elements of list "a" and output them. Just for your information, there is a very pythonic way to do this task with a for loop. By using a for loop, you don't need to know the length of the list. This can do similar output as your code:
a=["vim","#?","skk","jsj"]
for element in a:
print(element)
"element" is a regular variable that gets exactly one element from the list per each iteration. So at the end the of iteration of the for loop, all elements are printed.
+ 1
Tham you sir
0
Thank you sir