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

  1. Python string/list/tuple/dictionary conversions
  2. Python local and global variables
  3. The Python type function is different from the isinstance function
  4. Python is differs from ==
  5. Python mutable and immutable data types
  6. Shallow and deep copies of Python
  7. Python recursive functions
  8. Python sys module
  9. The Python list
  10. The Python tuple tuple
  11. Python dictionary dict
  12. Python conditional derivations
  13. Python list derivations
  14. Python dictionary derivations
  15. Python function declarations and calls
  16. Python variable argument *argc/**kargcs

The Python eval function

This article is published by the blog – Ape Say Programming Ape Say programming!