+ 2

Explain it for me, plz. Step by step

w1="welcome" w2="home" r=[] for x in w1: if x in w2: r.append(x) print(r[2]+r[1]+r[2]) ---》mom

21st Aug 2019, 9:38 AM
Gin
Gin - avatar
5 Réponses
+ 7
It checks every letter of "welcome", and adds it to r if it is in "home". So, w: not in "home" e: in "home" (r[0]) l: not in "home" c: not in "home" o: in "home" (r[1]) m: in "home" (r[2]) e: in "home" (r[3]) The list r containd these four characters: ['e', 'o', 'm', 'e'] So, r[2] + r[1] + r[2] = "mom"
21st Aug 2019, 9:48 AM
Airree
Airree - avatar
+ 3
for x in w1 will go through all the values in w1 if the present value is found in w2 put it in r, after the loop we will have "eome" printing r[2] -> m r[1] -> o r[2] -> m will now give you "mom"
21st Aug 2019, 9:49 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
the for-loop goes throw the String "welcome". if a Character in "welcome" is present in the String "home" it gets added to the list 'r'. in the end r is equal to: ["e", "o", "m" "e"] taking the second, first and second index and adding them up results in "mom" hope this helped! 😄
21st Aug 2019, 9:50 AM
Anton Böhler
Anton Böhler - avatar
+ 2
The code first creates two strings and an empty list,which can accept new values using append(). From 4th line: for x(it represents the letters in w1 ONE BY ONE so you can use anything like y to replace x) in string w1,if x(representing one letter in w1 at one time) exists in w2,add it to the list. As w1 and w2 contain e,o,m,e(repeated),list=['e','o','m','e'] Index of a list starts from 0. So 'o' is 1st value and 'm' is the second(r[2]).
21st Aug 2019, 9:55 AM
Ketchup🍅
Ketchup🍅 - avatar
+ 1
Thank you
21st Aug 2019, 10:20 AM
Gin
Gin - avatar