Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!

PythonPr's tweet image. 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.


Go ad-free on X with Premium+ Includes access to SuperGrok.


United States Trends
Loading...

Something went wrong.


Something went wrong.