Eval Module with Python Tutorial




The idea of eval is to evaluate an expression in the form of a string and return the value. So why would you do this?

A common question i see is something like "How do you convert a string of a list to a list?"

Sometimes it gets more complicated, like a string of a list of tuples to a python list of tuples, or maybe its a dictionary.

Sometimes, you can use the json module, but sometimes not. What you can do, if you have a string version of any python code, is use eval.

Keep in mind, just like the pickle module we talked about, eval has no security against malicious attacks. Don't use eval if you cannot trust the source. For example, some people have considered using eval to evaluate strings in the browser from users on their website, as a way to create a sort of "online editor." While you can do this, you have to be incredibly careful!


list_str = "[1,5,7,8,2]"
list_str = eval(list_str)

print(list_str)
print(list_str[1])

First, we define a string, that carries the syntax of a list. Next, we use eval to evaluate it.
Finally, we can show that it has the properties of a Python list.


x = input("see this...")
check_this_out = eval(input("code:"))
Another example, where we allow the user to input the string to be evaluated.

The next question is whether or not we can use logic in evaluate. Evaluate will only evaluate code, it will not compile. For example, this code wont work:


eval("if True: print('yep!')")
This should give you an invalid syntax error

With that shown, all hope is not lost if you want to basically do eval plus logic, because there is always exec, which we're going to cover next!


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

The next tutorial:





  • 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