Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

The exec and eval statements

The exec statement is used to execute Python statements which are stored in a string or file. For example, we can generate a string containing Python code at runtime and then execute these statements using theexec statement. A simple example is shown below.

>>> exec 'print "Hello World"' Hello World

 

Theeval statement is used to evaluate valid Python expressions which are stored in a string. A simple example is shown below.

 

>>> eval('2*3') 6