Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Answer: A Solution: Essentially we are calling func and printing it's return value. So let's work out what func is doing. lst, lst2 = [7], [7] is a tuple assignement lst is assigned [7], and lst2 is assigned [7] lst3 = lst *2 repeats lst twice, ie, [7, 7], assigns to lst3 +
Finally we come to the return statement. Let's be careful here. The return value is lst3 == lst2.extend([lst]) The left-hand-side of the comparison is lst3, [7, 7]. The right-hand-side is lst2.extend([lst]) which is equivalent to lst2.extend([[7]]) Now the extend method +
changes lst2 *in-place*. BUT it returns None. So, for the purpose of comparison, the result of the extend operation is None. Therefore the return value is [7, 7] == None which is False and that's what gets printed.
United States 趨勢
- 1. Lamar 21.8K posts
- 2. Ravens 35.6K posts
- 3. Steelers 77.1K posts
- 4. Mark Andrews 4,051 posts
- 5. Lions 86.9K posts
- 6. Drake Maye 8,222 posts
- 7. #Married2Med 3,320 posts
- 8. Derrick Henry 4,428 posts
- 9. Toya 5,484 posts
- 10. #RHOP 5,743 posts
- 11. Henderson 12K posts
- 12. Jags 16K posts
- 13. Zay Flowers 2,073 posts
- 14. Tyler Huntley 1,351 posts
- 15. Broncos 37.5K posts
- 16. #BaddiesUSA 5,705 posts
- 17. #HereWeGo 9,377 posts
- 18. #Patriots 3,492 posts
- 19. Contreras 11K posts
- 20. Goff 9,100 posts
Something went wrong.
Something went wrong.