print("Klay", print("Thompson"))
Running this code gives an output of
Thompson
Klay None
Why does the internal print
execute before the outer one?
I expected the output to be
Klay Thompson
None
print("Klay", print("Thompson"))
Running this code gives an output of
Thompson
Klay None
Why does the internal print
execute before the outer one?
I expected the output to be
Klay Thompson
None
When using nested print statements in python it is important to note that print
will first execute all print statements nested int it first from left to right(like all functions) to get the returned value which will always be none unless redefined, and then the result is constructed from right to left. So, when print('Thompson')
is run the print statement will return none and print Thompson
to the shell. Then the outer print statement prints the first value and the returned value of the inner print
which will be none