Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the built-in statements in Python exec

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the built-in statements in Python exec". In daily operation, I believe many people have doubts about the built-in statements in Python exec. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the built-in sentences in Python exec?" Next, please follow the editor to study!

Description

Exec executes Python statements stored in strings or files, and can execute more complex Python code than eval,exec.

It is important to note that exec is not a function in Python2, but a built-in statement (statement), but there is an execfile () function in Python2. It can be understood that Python 3 integrates the functions of the exec statement and execfile () functions into a new exec () function.

Grammar

The following is the syntax of exec:

Exec obj parameter

Obj-the expression to be executed.

Return value

The return value of exec is always None.

Example

The following shows an example of using exec:

Example 1 > exec 'print "Hello World"' Hello World# single-line statement string > exec "print 'runoob.com'" runoob.com # multiline statement string > exec "for i in range (5):. Print "iter time:% d"% I... "" iter time: 0iter time: 1iter time: 2iter time: 3iter time: 4 instance 2x = 10expr = "z = 30sum = x + y + zprint (sum)" def func (): y = 20 exec (expr) exec (expr, {'x: 1,'y: 2}) exec (expr, {'x: 1,'y: 2}, {'x: 3,'z: 4}) func () "

Output result:

603334 at this point, the study of "what are the built-in statements in Python exec" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report