Search code examples
pythonpython-3.xfunctioneval

Why do I get None in output in second line while using eval function?


I am executing this line of code -

print(eval("print(2 +3)"))

but this instead of giving output as 5 gives output-

5
None

Solution

  • This is because you are giving eval() within print().

    eval("(2 +3)")
    

    This will return 5 as the function eval returns the value 5 which gets printed using the print()

    However,

    print(eval("print(2 +3)"))
    

    Here inside the eval() you have used print(). So the inner print() prints the value 5 and the function eval() returns None as it has nothing to return. That None gets printed by the outer print()