- 4

s1 =[3,4] s2 =[1,2] s3=list() i=0 j=0 for i in s1: for j in s2: s3.append((i,j))

Explain above step by step

31st May 2020, 7:10 PM
SAADAT ALI
27 ответов
+ 2
s1 =[3,4] s2 =[1,2] s3=list() i=0 j=0 for i in s1: for j in s2: s3.append((i,j)) i=i+1 j=j+1 print(s3) _____________________________________ Explanation: # don't mind i=0, j=0 # in line 6 and 7, _______________ |for i in [3,4]: | | for j in [1,2]: | ------------------------ | --------------------------------------------------------------- # let i = 3: if j = 1 s3.append((3,1)) => s3 = [(3,1)] i = i + 1 => i = 3+1 => i = 4 if j = 2 s3.append((4,2)) => s3 = [(3,1), (4,2)] ---------------------------------------------------------------- # let i = 4: if j = 1 s3.append((4,1)) => s3 = [(3,1), (4,2), (4,1)] i = i + 1 => i = 4+1 => i = 5 if j = 2 s3.append((5,2)) => s3 = [(3,1), (4,2), (4,1), (5,2)] ------------------------------------------------------------------------------ then output is [(3,1), (4,2), (4,1), (5,2)]
1st Jun 2020, 9:09 AM
Jenson Y
+ 2
First outer loop runs one time with i=3 Second inner Loop runs two times and appends (3,1) and (3,2) to s3 list , Now first outer loop runs second time with i=4 Second inner Loop runs two times again and appends (4,1) and (4,2) to s3 list s3 now contains =[(3,1),(3,2),(4,1),(4,2)] hopefully you understood what I wrote , In short inner Loop executes fully and after then control is thrown back to outer loop which runs one time again and then inner Loop executes fully and so on
31st May 2020, 7:19 PM
Abhay
Abhay - avatar
31st May 2020, 7:29 PM
Abhay
Abhay - avatar
+ 2
SAADAT ALI look at the question ,you skipped not us
31st May 2020, 7:32 PM
Abhay
Abhay - avatar
+ 2
I know you would ask. j = j + 1 is not necessary because after appending (i, j) in s3. j = j+1 as no use. For example, if i=3, j=1 i = i + 1 => i = 3+1 = 4 j = j + 1 => j = 1+1 = 2 In 2nd for loop i.e. for j in s2 j changes its value to 2 but i is not changed So j = j +1 is not necessary
1st Jun 2020, 12:48 PM
Jenson Y
+ 1
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 why do you mention people name first , instead take time to write whole explanation and then comment ,
31st May 2020, 7:20 PM
Abhay
Abhay - avatar
+ 1
Yup after tagging his name you edited it lol
31st May 2020, 7:22 PM
Abhay
Abhay - avatar
+ 1
I can't take screenshot of output otherwise that would be a proof that answer is according to my explanation
31st May 2020, 7:28 PM
Abhay
Abhay - avatar
+ 1
Thanx now understand
1st Jun 2020, 12:31 PM
SAADAT ALI
+ 1
[(3,1),(3,2),(4,1),(4,2)]
1st Jun 2020, 4:19 PM
Prathamesh Yadav
Prathamesh Yadav - avatar
0
I m new and confused in nested for loops
31st May 2020, 7:17 PM
SAADAT ALI
0
uhh I am only a mighty web developer
1st Jun 2020, 1:43 PM
DrNefardio
DrNefardio - avatar
0
Ok
1st Jun 2020, 2:56 PM
ferney vega
ferney vega - avatar
0
It is c++
1st Jun 2020, 4:31 PM
Shadow
Shadow - avatar
0
Nc
1st Jun 2020, 5:19 PM
ALOK “RAKHy” ROY
ALOK “RAKHy” ROY - avatar
0
It is pyrhon 3.7
1st Jun 2020, 5:21 PM
SAADAT ALI
0
Ye
2nd Jun 2020, 2:17 AM
SAADAT ALI
0
Yes
2nd Jun 2020, 4:30 AM
ferney vega
ferney vega - avatar
- 1
Thanx my account is no activated
31st May 2020, 7:24 PM
SAADAT ALI
- 1
Its give out put [(3,1),(4,2),(4,1),5,2)]
31st May 2020, 7:26 PM
SAADAT ALI