Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Answer: B) [0, 1, 2], [] (i for i in range(3)) creates a generator, not a list! › First list(g): consumes generator → [0,1,2] › Second list(g): generator exhausted → [] For beginners: Generators are one-time iterators! Use [i for i inrange(3)] (brackets) for reusable…
Stream, call, game, and work in even the most remote locations. Order online in minutes
[0,1,2] [] The list goes empty before the fist iteration. Am I right?
Hey 👋 Engineers! Do you want to teach programming to others hourly basis in part time and earn ? Let me know so that I can count you
Answer:-(A)[0, 1, 2], [0, 1, 2] Here g is a tuple which store value with the help of range function. So g = (0, 1, 2) list(g) convert the tuple into list and print function print two list side by side.
A Line 1: the for loop in the tuple comprehensions loops from 0 to 2, and the tuple g is (0, 1, 2) Line 2: the list(g) changes the tuple g from a tuple to a list.
United States Trends
- 1. Grammy 363K posts
- 2. #FliffCashFriday 1,051 posts
- 3. Dizzy 10K posts
- 4. Clipse 21.5K posts
- 5. #NXXT 1,092 posts
- 6. Kendrick 63.2K posts
- 7. James Watson 5,411 posts
- 8. #GOPHealthCareShutdown 8,500 posts
- 9. Orban 42.3K posts
- 10. #FursuitFriday 12.2K posts
- 11. Darryl Strawberry N/A
- 12. Thune 71.8K posts
- 13. Leon Thomas 21.1K posts
- 14. addison 32K posts
- 15. olivia dean 16K posts
- 16. Chase 87.9K posts
- 17. AOTY 22.4K posts
- 18. Carmen 47.8K posts
- 19. Alfredo 2 1,074 posts
- 20. Katseye 121K posts
Something went wrong.
Something went wrong.