- 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
35 Respostas
+ 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)]
+ 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
+ 2
šš¢š¢ššØ šš”šš²šš„ Yup thanksš
+ 2
SAADAT ALI look at the question ,you skipped not us
+ 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
+ 1
šš¢š¢ššØ šš”šš²šš„ why do you mention people name first , instead take time to write whole explanation and then comment ,
+ 1
Yup after tagging his name you edited it lol
+ 1
I can't take screenshot of output otherwise that would be a proof that answer is according to my explanation
+ 1
Thanx now understand
+ 1
[(3,1),(3,2),(4,1),(4,2)]
0
I m new and confused in nested for loops
0
uhh I am only a mighty web developer
0
Ok
0
It is c++
0
Nc
0
It is pyrhon 3.7
0
Ye
0
Yes
- 1
Thanx my account is no activated
- 1
Its give out put [(3,1),(4,2),(4,1),5,2)]