Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
25
12
112
10.3K
Sergei Kotov
Sergei Kotov@kotov_dev·
@PythonPr Answer: a) 3|2|1|0| › while(j) means "while j is truthy" › 0 is falsy, all other numbers truthy › j=4: subtract, print 3| › j=3: subtract, print 2| › j=2: subtract, print 1| › j=1: subtract, print 0| › j=0: falsy, loop ends For beginners: while (j) = while j != 0
English
1
1
17
311
Earnest Codes
Earnest Codes@Earnesto037·
@PythonPr Answer: (a) 3|2|1|0| The code starts with the variable j initialized to 4. The while loop continues as long as the condition while(j) is true. In Python, any non-zero number is considered True. First iteration: j is 4. The condition while(4) is true. j
English
1
0
8
353
Ayush
Ayush@iAyushCodes·
@PythonPr Answer: a Explanation👇🏻 The loop starts when j = 4 : while(4) It first decrements j by 1 then prints it and also adds ‘|’ after j It prints 3|2|1| When j = 1 : while(1) It decrements j to 0 and prints 3|2|1|0| j becomes 0 : while(0) Loop terminates.
English
0
0
3
284
#!/usr/bin 🇮🇳
#!/usr/bin 🇮🇳@iamjogi·
@PythonPr a. j is decremented by 1 before print statement and loop doesn't execute for j=0.
English
0
0
2
132
Lee Moras ➰🕯️
Lee Moras ➰🕯️@LeeMoras·
@PythonPr a • j is decremented to 3 before the print operation. • The while loop executes until the valuecl of j is >=0, resulting in a string ending in 0|
English
0
0
1
275
Joe
Joe@Joe46791334·
@PythonPr A decrement happens after test and before print
English
0
0
1
38
Biel Men
Biel Men@BielMenHaha·
@PythonPr Alternative B: 3|2|1| While loop consider something "True" to run. The number 0 isn't True in bool class, it's False. So, the block doesn't consider the condition valid, and terminates.
English
0
0
1
161
Paylaş