Python convert a string to a logic operator

For example, if I have an expression like

x=True or True

if I evaluate in the shell the result is True

print(x)
x=True

So now I want to convert a string or an input to direct to that logic expression like

x=raw_input('Please give an expression:')

I know that the expression is a string so how to convert that string to a logic expression?

print(x)
x="True or True"

Answers


You can use eval() function:

print eval(x)

Note that you must be cautious while using it.

Edit:

As @PriyankPatel mentioned, another way would be using exec:

exec("print " + x)

Need Your Help

Neural Network, Genetic algorithm as an Intrusion detection system

c# algorithm neural-network genetic-algorithm

Hi I need some help on getting started with creating my first algorithm; I want to create a NN/Genetic Algorithm for use as an Intrusion detection system.

Is MOD operation more CPU intensive than multiplication?

c++ division multiplication

Why is MOD operation more expensive than multiplication by a bit more than a factor of 2? Please be more specific about how CPU performs division operation and returns the result for MOD operation....

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.