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.
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.
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|
Stream, call, game, and work in even the most remote locations. Order online in minutes
United States Trends
- 1. Marshawn Kneeland 43K posts
- 2. Nancy Pelosi 63.1K posts
- 3. Craig Stammen 1,715 posts
- 4. Gordon Findlay 1,971 posts
- 5. Ozempic 5,713 posts
- 6. Michael Jackson 67.9K posts
- 7. Pujols N/A
- 8. #ThankYouNancy 1,290 posts
- 9. Novo Nordisk 5,714 posts
- 10. Abraham Accords 4,107 posts
- 11. GLP-1 4,532 posts
- 12. #NO1ShinesLikeHongjoong 37K posts
- 13. #영원한_넘버원캡틴쭝_생일 36.3K posts
- 14. Kyrou N/A
- 15. Kazakhstan 5,893 posts
- 16. Preller N/A
- 17. Kinley N/A
- 18. Gremlins 3 4,816 posts
- 19. Joe Dante N/A
- 20. Baxcalibur 5,995 posts
Something went wrong.
Something went wrong.