Python exec built-in expression--exec ()

Source: Internet
Author: User

exec  obj
Function:
EXEC executes python statements stored in a string or file, and can execute more complex Python code than eval,exec. obj is the expression to execute.
The exec return value is always None.
Release Notes:* * It is necessary to note that Exec is not a function in Python2, but a built-in statement (statement), but there is a execfile () function in Python 2.
It can be understood that Python 3 consolidates the functionality of the EXEC statement and execfile () functions into a new EXEC () function.
Instance:
1>>>exec 'print "Hello World"'2 Hello World3 #single-line statement string4>>>exec "print ' runoob.com '"5 runoob.com6  7 #multi-line statement string8>>>exec """For I in range (5):9 ... print "ITER time:%d"% iTen ... """ One ITER time:0 AITER time:1 -ITER Time:2 -ITER Time:3 theITER Time:4

Example 2:

1x = 102Expr ="""3 z =4 sum = x + y + z5 print (sum)6 """7 deffunc ():8y = 209     exec(expr)Ten     exec(Expr, {'x': 1,'y': 2}) One     exec(Expr, {'x': 1,'y': 2}, {'y': 3,'Z': 4}) A      -Func ()

Results:

1 2 3 34

Python exec built-in expression--exec ()

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.