Question on loops

I am quite new here even though I have been on Codecademy for a while now. for this particular code:

#Write your function here
def delete_starting_evens(lst):
  while (len(lst) > 0 and lst[0] % 2 == 0):
    lst = lst[1:]
  return lst

#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

I understand the code basically but what I do not understand is why the loop kept going after it found that the value at index 0 of lst is even. there are two conditions for the loop, and I am sure it met the second condition when it found that the value at lst[0] is even… I thought it should have given the output [8, 10, 11, 12, 15] but it made sure it went over the lst, removed all the even numbers in front of the first odd number… please how did the while loop achieve this??

that is exactly what this condition is for:

lst[0] % 2 == 0

even the number even, the remainder will equal zero, thus the condition is true (assuming length greater then zero)

loops run as long as the conditions evaluate to true.

you can run your code through a visualizer/debugger to help you understand it:

http://www.pythontutor.com/visualize.html#mode=edit

Okay dear. thanks a lot for your response. I appreciate

I just do not know why this thing still confuses me. loop keep running while the conditions are met but the action in the loop i.e “lst = lst[1:]” should have sliced from index 1, how was the loop able to leave it and get to slice from index 3. Or maybe I should ask it this way : what specific nature of the while loop made it check for other indices of lst since the loop says “while (len(lst) > 0 and lst[0] % 2 == 0):” ? obviously the conditions were met so I thought it should have done this “lst = lst[1:]” that is slice from index 1…I am so confused.

for example:

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

in the first iteration of the loop, lst[0] will be 4.

then the list is sliced, resulting in [8, 10, 11, 12, 15].

so, now lst[0] will be 8, given there is a new first element.

So what the first value of the list is, keeps changing until an odd/uneven number is encountered.

If you repeatedly walk 1 step forward you will have walked not a total of 1 step but as many steps as you made repetitions

Maybe slicing doesn’t do what you think it does? Or maybe something else doesn’t behave like think it does. You can use prints or a debugger to get information out of what your code is doing. It’s not a black box, you can look at it as it happens.

It seems like you want to attribute the unexpected behaviour to while-loops, but keep in mind that while-loops are very simple. They run their bodies for as long as the condition is satisfied. You can rule out that the while-loop is the difference by removing it and repeating your code as many times as there should be iterations, it would still do the same thing.

oh oh oh my… thanks. I totally forgot that loops generally through a file, list or something. Thanks again.

thamks man… I GOT THIS.

hello guys

please, is there anything wrong with this code:
def get_destination_index(destination):
destination_index = destinations.index(destination)
return destination_index

get_destination_index(‘Los Angeles, USA’)

I typed “python3 script.py” in the git bash section and the index wasn’t printed. please what is the reason for this? the file where the code has been written is “script.py”

printing requires calling the print() function, which i don’t see anywhere?

this is the image of what I am trying to do.
but I was expecting “2” to show up but it didnt

why did you expect 2 to show? You don’t use print() anywhere.

oh… I thought taht using “return” is enough. that way, the function already holds the answer… it is just for you to call it

return and print() do very different things. Return hands back data to the caller/function call. I don’t think i need to explain what print does

hahahaha… thanks dear. codes sometimes can just turn me to a naughty boy. yes indeed, print() is needed. I was thinking of something entirely different. thanks for your responses . I am grateful.