0

please explain how this code works

words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!")

3rd Apr 2018, 11:38 AM
R Sha
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!
3rd Apr 2018, 12:16 PM
Mitali
Mitali - avatar
+ 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
3rd Apr 2018, 11:42 AM
cyk
cyk - avatar
+ 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(!).
3rd Apr 2018, 11:47 AM
Bishal Sarang
Bishal Sarang - avatar
+ 1
Thanks friends
3rd Apr 2018, 12:13 PM
R Sha