Popular recipes tagged "expressions" but not "parsing"http://code.activestate.com/recipes/tags/expressions-parsing/2017-04-27T21:26:00-07:00ActiveState Code RecipesClassifying characters using nested conditional expressions (Python) 2017-04-27T21:26:00-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580792-classifying-characters-using-nested-conditional-ex/ <p style="color: grey"> Python recipe 580792 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/characters/">characters</a>, <a href="/recipes/tags/classification/">classification</a>, <a href="/recipes/tags/conditional_expressions/">conditional_expressions</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/map/">map</a>). </p> <p>Python has a feature called conditional expressions, similar to C's ternary operator. For example:</p> <p>print n, 'is odd' if n % 2 == 1 else 'is even'</p> <p>Here, the conditional expression is this part of the print statement above:</p> <p>'is odd' if n % 2 == 1 else 'is even'</p> <p>This expression evaluates to 'is odd' if the condition after the if keyword is True, and evaluates to 'is even' otherwise.</p> <p>The Python Language Reference section for conditional expressions shows that they can be nested. This recipe shows that we can use nested conditional expressions (within a return statement in a user-defined function) to classify characters into lowercase letters, uppercase letters, or neither.</p> <p>It also shows how to do the same task using map, lambda and string.join, again with a nested conditional expression, but without using return or a user-defined function.</p> Evaluator 2.0 (Python) 2010-11-25T18:24:09-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577469-evaluator-20/ <p style="color: grey"> Python recipe 577469 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/evaluator/">evaluator</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/parser/">parser</a>). </p> <p>This is a complete rewrite of <a href="http://code.activestate.com/recipes/576790/">recipe 576790</a>. While aiming to maintain similar functionality and continuing its implementation for self-academic purposes, a much cleaner parser / tokenizer and operator execution engine were developed. A slightly different math syntax is supported in this version, but it is arguably better and more capable than it previously was. Base prefixes are a feature now supported, and the single downgrade is calculating with integers instead of floats.</p> Calculations with error propagation, and semi-formal expressions (Python) 2010-01-15T06:08:56-08:00Eric-Olivier LE BIGOThttp://code.activestate.com/recipes/users/2672032/http://code.activestate.com/recipes/576721-calculations-with-error-propagation-and-semi-forma/ <p style="color: grey"> Python recipe 576721 by <a href="/recipes/users/2672032/">Eric-Olivier LE BIGOT</a> (<a href="/recipes/tags/calculus/">calculus</a>, <a href="/recipes/tags/error_propagation/">error_propagation</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/formal_calculations/">formal_calculations</a>, <a href="/recipes/tags/lazy_evaluation/">lazy_evaluation</a>, <a href="/recipes/tags/uncertainties/">uncertainties</a>). Revision 22. </p> <p><strong>Do not use this module</strong>, but use instead the more powerful <a href="http://pypi.python.org/pypi/uncertainties/">uncertainties.py module</a>.</p> <p>Module for performing calculations with error propagation, such as (1 +- 0.1) * 2 = 2 +- 0.2. Mathematical operations (addition, etc.), operations defined in the math module (sin, atan,...) and logical operations (&lt;, &gt;, etc.) can be used.</p> <p>Correlations between parts of an expression are correctly taken into account (for instance, the error on "x-x" is strictly zero).</p> <p>Code written for floats should directly work with the numbers with uncertainty defined here, without much need for modifications.</p> <p>The module also contains a class that represents non-evaluated mathematical expressions. This class is used for performing the differentiation required by the error propagation calculation, but can be used on its own, for manipulating "semi-formal" expressions whose variables can be accessed.</p>