What will be the output of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

What will be the output of this code?

t = [2,3,4] for i in t: if (i % 2): t.append(i) print(t)

2nd May 2020, 2:13 PM
Riddhi Rana
Riddhi Rana - avatar
29 Answers
+ 11
Is the code is correct
4th May 2020, 7:46 AM
Pavithra Kanmanirajah
Pavithra Kanmanirajah - avatar
+ 5
Bensen this line of code is correct. i%2 will always give either 1 or 0, which is considered as True or False.
4th May 2020, 8:01 AM
Riddhi Rana
Riddhi Rana - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥, Abhay I am new at this platform. This is the code in which I was facing problem and had a doubt which has already been clarified by ~ swim ~ . Thanks for telling me about Code Playground. But there also, this code is giving 'No Output' and no explaination.
2nd May 2020, 2:44 PM
Riddhi Rana
Riddhi Rana - avatar
+ 4
Ok wlc to sololearn but you asked what is the output of the code !!You could have said this program is giving no output !! Also click on your profile there is code bits ,click on add sign and you can access the code playground for the given languages !
2nd May 2020, 2:46 PM
Abhay
Abhay - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Thankyou for understanding. I got the logic behind this code. 🙂
2nd May 2020, 2:47 PM
Riddhi Rana
Riddhi Rana - avatar
+ 3
I accept it! Will improve it from next time.
2nd May 2020, 2:50 PM
Riddhi Rana
Riddhi Rana - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 execute this program once, it is a part of infinite loop. Similar to below code: if True: print("true")
4th May 2020, 9:20 AM
Riddhi Rana
Riddhi Rana - avatar
2nd May 2020, 2:53 PM
Riddhi Rana
Riddhi Rana - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 this is one of the question which I've answered at this platform only and that too in Python challenge.
4th May 2020, 9:23 AM
Riddhi Rana
Riddhi Rana - avatar
+ 2
Riddhi Rana If I correct this code as t = [2,3,4] for i in t: If( i % 2): t.append(i) print(t) Then at 3 if statement became true so 3 will append in t So again I became 3 then also it append and I form a infinite loop [2,3,4,3,3,3,3,3,3,.............]
4th May 2020, 10:32 AM
Bensen
+ 2
Bensen, 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 you are ryt...my mistake...i've written this code in description multiple times and the text was getting disappeared everytime...I missed writing 'if' but ~ swim ~ has given the logic I was looking for, it is satisfactory answer. Now when I saw Bensen's code, I compared it with mine...then I found it...In my mind it was if a%2: and the code I've written here is a%2:......my bad 😒😔
4th May 2020, 10:51 AM
Riddhi Rana
Riddhi Rana - avatar
+ 2
There will be an infinite loop Because at every i%2 it is gonna add a value i again and again
4th May 2020, 1:26 PM
Akash Dhunde
+ 1
There is code playground ,use it pls
2nd May 2020, 2:15 PM
Abhay
Abhay - avatar
+ 1
Riddhi Rana It cause error the statement i%2: is wrong it give a syntax error message
3rd May 2020, 8:00 AM
Bensen
3rd May 2020, 9:08 AM
Bensen
+ 1
I have more questions
4th May 2020, 7:37 AM
TÕÑY
TÕÑY - avatar
+ 1
shubhra sharma ask me the question
4th May 2020, 7:40 AM
Bensen
+ 1
Pavithra Kanmanirajah No This code is incorrect at i%2: It may be if(i%2):
4th May 2020, 7:57 AM
Bensen
+ 1
It's ok these are the usual mistakes in coding 😀😀😇
4th May 2020, 10:55 AM
Bensen
+ 1
Behruz Rajabov you can ask questions here
4th May 2020, 12:00 PM
Bensen