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. DK Metcalf 8,858 posts
- 2. Jags 8,428 posts
- 3. Broncos 23.3K posts
- 4. Steelers 29.8K posts
- 5. Nicki 146K posts
- 6. Lions 44.2K posts
- 7. Trevor Lawrence 4,103 posts
- 8. Gainwell 8,796 posts
- 9. Jaguars 12.7K posts
- 10. #HereWeGo 3,921 posts
- 11. Parker Washington 1,767 posts
- 12. Riley Moss 1,178 posts
- 13. Scotty Miller N/A
- 14. Browns 35.7K posts
- 15. #OnePride 3,167 posts
- 16. Dan Campbell 1,207 posts
- 17. Goff 3,543 posts
- 18. Baker 23.1K posts
- 19. #PITvsDET 2,564 posts
- 20. #KeepPounding 6,373 posts
Something went wrong.
Something went wrong.