Python eval function Magical

Source: Internet
Author: User

Author's blog address: https://www.cnblogs.com/liu-shuai/

Eval

Function: evaluates the string str as a valid expression and returns the result of the calculation.

Syntax: eval (source[, globals[, locals]), value

Parameters:

Source: The code object returned by a Python expression or function compile ()

Globals: Optional. It must be dictionary.

Locals: Optional. Arbitrary map Object

Examples show:

you can convert list,tuple,dict and string to each other. #################################################string conversion to list>>>a ="[ [up], [3,4], [5,6], [7,8], [9,0]]">>>type (a)<type'Str'>>>> B =Eval (a)>>>Printb[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]>>>type (b)<type'List'>#################################################convert strings into dictionaries>>> A ="{1: ' A ', 2: ' B '}">>>type (a)<type'Str'>>>> B =Eval (a)>>>Printb{1:'a', 2:'b'}>>>type (b)<type'Dict'>#################################################string Conversion Narimoto Group>>> A ="([up], [3,4], [5,6], [7,8], (9,0))">>>type (a)<type'Str'>>>> B =Eval (a)>>>PrintB ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))>>>type (b)<type'tuple'>

Python eval function Magical

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.