Could someone explain why does it print Hello None None None?


4/19/2019 8:04:22 PM

Soheil Elahi

19 Answers

New Answer


Tahir Usman due to the evaluation order It must evaluate the innermost execution before moving on to the next one Take this code for instace def foo(x): return x + 1 def bar(x): return x * 2 print(bar(foo(20))) As you can see, the "bar" function takes the return value of "foo" so it has to be evaluated first "foo" takes the value 20 and return 21 which is then passed to the "bar" function which in turn returns 42 which is outputted by "print"


Soheil Elahi inner most print, prints "Hello" and returns None. Then the argument to next print is None, so it prints None and returns None and it carrries on, Try this x = print("Hello") print(x) // prints None Any function in python that does not explicitly returns a value, implicitly return None.


~ swim ~ Great answer, thanks.


Burey, thanks.👌👌👌


So why does it print None too?


Burey It was a great example.👍👍👍


The print function does not have a return value So going thru the evaluation order the inner most print call print("Hello") is executed and outputs "Hello" Each other print call will output the value which retuned from the call of print which is None you can test yourself to see what a function with no return value prints def foo(): x = 42 # doesn't matter what the function does, as long as it doesn't return anything print(foo()) output: None


Tahir, it starts from the innermost one.


Yes same doubt here can any one explain it


Burey bro, why it doesn't print none none none hello?


Just print() function returns None


Soheil Elahi 🤘


Print expects a string, which is only true for the innermost one?


Muhammad Reshma i've explained it here See my previous answer to this post


"print( )" is actually a function, and it prints the statements inside the paratheneses, known as arguments. The innermost print prints "Hello". But the next print does not have any string argument, and the value is absent. Absence of a value in python 3 is declared by "None". As the other prints don't have string arguments, they print None.






Print ("hello to the world") RUN hello to the world