0% found this document useful (0 votes)
3 views1 page

Understanding Python's eval Function

The document describes how the eval function in Python can be used to evaluate strings as Python code or expressions. Eval can evaluate simple expressions, use variables, call functions, and return values. Examples show eval evaluating math expressions and function calls containing variables, and printing the output.

Uploaded by

Anonymous VMJEkv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Understanding Python's eval Function

The document describes how the eval function in Python can be used to evaluate strings as Python code or expressions. Eval can evaluate simple expressions, use variables, call functions, and return values. Examples show eval evaluating math expressions and function calls containing variables, and printing the output.

Uploaded by

Anonymous VMJEkv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Use the eval function.

print eval('2 + 4')

Output:
6

You can even use variables or regular python code.


a = 5 print eval('a + 4')

Output:
9

You also can get return values:


d = eval('4 + 5') print d

Output:
9

Or call functions:
def add(a, b): return a + b def subtract(a, b): return a - b a = 20 b = 10 print eval('add(a,
b)') print eval('subtract(a, b)')

Output:
30 10

You might also like