0
please explain how this code works
words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!")
4 Réponses
+ 3
The first line intializes a list with for elements.
In second line "word" is a user defined variable that is used to iterate through every element of the list.
In first iteration word="hello",in second word="world"...and so on.
After entering the loop it prints the word concatenated with exclamtion
After frst iteration it will print...
hello!
+ 1
line 2 and 3 of the code basically say: go through the list "words" and take each element, put it in the variable "word", add ! to "word" and print the result... Line 2 goes through the list and put the element in "word" and line 3 does the adding and the printing
+ 1
First Line :words = ["hello", "world", "spam", "eggs"] simply initailzes the list with the items separated my comma.
Second line: It is simply a for each loop i.e it iterates through all the items in list until it's empty. It goes through every item in the list.
Third Line: It prints each item i.e word in words concatenating with the exclamation sign(!).
+ 1
Thanks friends