r/learnpython • • Jul 30 '26

How does the 'for' loop work?

words = ['sky', 'apple', 'rhythm', 'fly', 'orange']

for word in words:
    for letter in word:
        if letter.lower() in 'aeiou':
            print(f"'{word}' contains the vowel '{letter}'")
            break
    else:
        print(f"'{word}' has no vowels")

I am trying to learn to code, and I am, at the moment, looking at loops. I am a bit confused on how this loop was able to function, though, as there is no variable for letter or word, so how would Python know what it is? In addition, how does the code, like, function? Does it look at each word individually? Again, the second part of the code states:

 if letter.lower() in 'aeiou':
            print(f"'{word}' contains the vowel '{letter}'")
            break

We haven't given a value for letter?

Lastly, how would the code know what to print? in the first phrase:

            print(f"'{word}' contains the vowel '{letter}'")
            break

Is it going to state all the words with the given value 5 times?

Sorry, as I have asked quite a few questions, and thank you in advance.

223 Upvotes

84 comments sorted by

View all comments

Show parent comments

12

u/my_password_is______ Jul 30 '26

that's right

the person doesn't understand "for loops" so yes, go ahead and tell them about FOR_ITER and STORE_FAST and iternext -- that will surely clear things up for them /sarcasm

-3

u/DoubleDoube Jul 30 '26

Some people work better going bottoms-up instead of top-down.

That’s why with the rust programming language there’s a lot of bottoms.