Exec with Python Tutorial



Something similar to eval is the exec function. What exec is going to do is both compile and evaluate a statement from a string. So, as you might guess, this one is even more dangerous than pickle and eval, so again, only do this with sources that can be trusted. This is another example of something people might get the wild idea of using unprotected with some sort online form so they can create their own embedded Python IDE in a browser. Let's see some examples of exec. If you recall from the previous tutorial, eval wouldn't compile any code. Exec, however, will. Let's see:

First, we can see how it can do the same things eval does, like:


exec("print('so this works like eval')")
That should work, just like with eval.

Awesome, but what about:


list_str = "[5,6,2,1,6]"
list_str = exec(list_str)
print(list_str)
Remember, this compiles and evaluates.

That code is like just simply inputting the list in the Python IDE and running it. You just won't see anything happen.


exec("list_str = [1,5,7,8,2]")
print(list_str)
Magic! Sorcery!

So the above code works. If that isn't awesome and scary at the same time, I don't know what is.


exec("def test(): print('oooo snap!!!')")
test()
Even more awesome, and even scarier!

Do you have to do these one-liners though?


exec("""
def test2():
    print('lets see if multi line works....')
""")

test2()

Shut the front door!

Pretty awesome stuff if you ask me! Just note that you need to use those triple quotes for the multi-line codes.



There exists 1 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

That is all for now in this basics series, for more tutorials, head to the:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial