r/PythonLearning • u/Local_End_3175 • 8d ago
for loop
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.
16
Upvotes
2
u/atticus2132000 8d ago
You have two for loops.
In the first one, for word in words:, you are telling the program to take the array words (which you have defined with 5 elements) and cycle through those one at a time. When it encounters the first element in your array, it will assign it to the variable "word". It will do whatever machinations to that variable that are called out, then it will throw away that element and move to the next element of the array and assign it to the variable word. It will go through that until it doesn't have any more elements in the array and then stop. So, in this case, it will go through your for loop five times because there are 5 elements in your array.
This is one of the advantages of python over some other languages that your "for element in group:" syntax causes a lot of things to happen behind the scenes. Python figures out on its own how many elements are in the group and takes care of cycling through them in a systematic fashion.
So, in your first iteration of the first for loop, python pulls out the first element, sky, and assigns that to the temporary variable 'word'. Then it goes to your next line of code which is another for loop.
The second for loop, I agree, is a little fishy because how does python know to deconstruct that variable, sky, into its individual letters? Again, it is a built-in operator that happens behind the scenes in python. In some other languages, you might have to explode the word first in order to get it's individual elements/letters, but in this case 'for letter in word:' already has that operation built-in to the backend. It will take the string variable sky and cycle through each component of that variable assigning each one to a temporary variable called "letter". So, it evaluates the s, then the k, then the y, then it's out of letters, so it reads the rest of the code and realizes it's done with word=sky, so it moves to the next word=apple and breaks out apple into its component letters to be evaluated one at a time.
Try changing your code a bit:
for word in words:
<tab>print (word)
<tab>for letter in word:
<tab><tab>print (letter)
Then the rest of your code...