Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!

PythonPr's tweet image. Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!

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


Start your journey in simulated trading with FTMO, backed by 10 years of experience.


well

kolpuppy's tweet image. well

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


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.


a. j is decremented by 1 before print statement and loop doesn't execute for j=0.


I thought b , if I’m wrong someone help me out


Come to check our latest activities! The thrill starts now!


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.


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|


A decrement happens after test and before print


Stream, call, game, and work in even the most remote locations. Order online in minutes


Answer is A.


United States Trends
Loading...

Something went wrong.


Something went wrong.