directory
- A preface.
- An introduction to the Python eval function
- A Python eval function
- Four. Guess you like it
Recommended path for learning Python: Python Learning Directory >> Python Basics
A preface.
Input and Eval are interactive built-in functions that directly execute string expressions and return the value of the expression.
An introduction to the Python eval function
The eval function directly executes a string expression and returns the result of the expression.
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python eval function. py @time :2021/04/11 07:37 @motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" "a = eval (" 3 + 2.5 ") print (" a = {}, is a type of {} ". The format (type a, type (a))) a = eval (" 3 + 2 ") print (" a = {}, A type is {} ". The format (type a, type (a))) 33 a = b = eval (" b / 2 ") print (" a = {}, is a type of {} ". The format (type a, type (a))) "' output: <class 'int'> a=16.5 <class 'float'>"Copy the code
A Python eval function
The input and eval functions are used together to create an unbounded calculator:
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python eval function. py @time :2021/04/11 07:37 @motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! """ while True: Result = eval(input(" please input expression: ")) print(" please input expression: ") print(" please input expression: ") print(" please input expression: ") print(" please input expression: ") print(" please input expression: ") Please enter the expression: ""Copy the code
Note:
- 1. The return value of input isString STRType, if requiredintorlistA type can be converted
- ** Eval is a powerful built-in function that allows you to directly delete system files
Four.Guess you like
- Python string/list/tuple/dictionary conversions
- Python local and global variables
- The Python type function is different from the isinstance function
- Python is differs from ==
- Python mutable and immutable data types
- Shallow and deep copies of Python
- Python recursive functions
- Python sys module
- The Python list
- The Python tuple tuple
- Python dictionary dict
- Python conditional derivations
- Python list derivations
- Python dictionary derivations
- Python function declarations and calls
- Python variable argument *argc/**kargcs
The Python eval function
This article is published by the blog – Ape Say Programming Ape Say programming!