How to Use eval() Function in PythonPython Basics


The Eval() function is used to execute a string expression as Python code and return the value of the expression execution.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

x = 11
print(eval( '3 * x' ))

print(eval('pow(2,4)'))

n = 11
print( eval("n + 4"))

Output:

33
16
15

Syntax

eval(expression)

Parameters

The method takes string expression as an input parameter.

Return Value

Return the result of expression execution.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us