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 Trends
- 1. Nicki Minaj 49.8K posts
- 2. Browns 26.5K posts
- 3. Chase Brown 2,143 posts
- 4. Bucs 6,493 posts
- 5. James Cook 4,923 posts
- 6. Bryce Young 2,531 posts
- 7. Judkins 5,759 posts
- 8. JJ McCarthy 2,580 posts
- 9. Ewers 4,977 posts
- 10. #KeepPounding 2,424 posts
- 11. #BillsMafia 6,542 posts
- 12. Titans 14.2K posts
- 13. Jaxson Dart 2,137 posts
- 14. #DawgPound 2,417 posts
- 15. #Skol 1,793 posts
- 16. Abdul Carter N/A
- 17. Myles Garrett 1,796 posts
- 18. Theo Johnson N/A
- 19. Sean Tucker N/A
- 20. Kenneth Murray N/A
Something went wrong.
Something went wrong.