Python eval() Function
Python 3 Tutorial
Python3 Tutorial
Python3 Introduction
Python3 Environment Setup
Python3 VScode
Python3 Basic Syntax
Python3 Basic Data Types
Python3 Data Type Conversion
Python3 Interpreter
Python3 Comments
Python3 Operators
Python3 Number
Python3 String
Python3 List
Python3 Tuple
Python3 Dictionary
Python3 Set
Python3 Conditional Control
Python3 Loop Statements
Python3 First Step of Programming
Python3 Comprehensions
Python3 Iterator and Generator
Python3 with
Python3 Function
Python3 lambda (Anonymous Function)
Python Decorator
Python3 Data Structure
Python3 Module
Python __name__
Python3 Input and Output
Python3 File
Python3 OS
Python3 Errors and Exceptions
Python3 Object-Oriented
Python3 Namespace/Scope
Python Virtual Environment Creation
Python Type Hints
Python3 Standard Library Overview
Python3 Examples
Python Quiz
Python 3 Advanced Tutorial
Python3 Regular Expressions
Python3 CGI Programming
Python MySQL - mysql-connector Driver
Python3 MySQL Database Connection - PyMySQL Driver
Python3 Network Programming
Python3 SMTP Send Email
Python3 Multithreading
Python3 XML Parsing
Python3 JSON Data Parsing
Python3 Date and Time
Python3 Built-in Functions
Python MongoDB
Python3 urllib
Python uWSGI Installation and Configuration
Python3 pip
Python3 operator Module
Python math Module
Python requests Module
Python random Module
Python OpenAI
Python Useful Resources
Python AI Drawing
Python statistics Module
Python hashlib Module
Python Quantization
Python pyecharts Module
Python selenium Library
Python eval() Function
Description
The eval() function evaluates the specified expression (as a valid Python expression) and returns the result.
Syntax
eval(expression, globals=None, locals=None)
Parameters
- expression: This is the expression to be evaluated, as a string.
- globals: This is a dictionary object that holds global variables and available built-in functions.
- locals: This is a dictionary object that holds local variables and available built-in functions.
Return Value
Returns the result of evaluating the expression.
Example
The following example demonstrates the usage of the eval() function:
#!/usr/bin/python3
x = 7
print(eval('3 + x'))
print(eval('pow(2,3)'))
print(eval('2 + 2'))
print(eval('"a" + "b"'))
Output:
10
8
4
ab
Another example:
#!/usr/bin/python3
n=81
print(eval('n + 4'))
Output:
85
YouTip