Top-rated recipes tagged "expressions" Code RecipesClassifying characters using nested conditional expressions (Python) 2017-04-27T21:26:00-07:00Vasudev Ram <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> Expression Evaluator (Python) 2009-06-02T23:57:44-07:00Stephen Chappell <p style="color: grey"> Python recipe 576790 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/parsing/">parsing</a>). Revision 4. </p> <p>After reading a portion of the book "The C# Programming Language: Third Edition," I found a section in the introduction that introduced abstract classes and methods that involved an example that included the concept of expression trees. The code was easy to implement since it just had to be copied out of the book. After playing around with the program a little and extending it, I thought that it would be fun to write a program in C# that could (interactively) evaluate expressions and display the results. Not knowing C# quite as well as Python led to the following program written and tested in Python 3.0 (not sure about previous languages).</p> <p>The first section of the code includes port of the program from the aforementioned book along with extra code that allows for further features not originally included in the C# version. Those sections are clearly marked as being new code written by yours truly. The second area of the program has six functions that are profusely documented so as to explain how they go about parsing and processing expressions entered for evaluation. For those wishing to use the code, the "run" function should be all that you need. The final part of the module contains a test program that can be used to check the validity of the how well the program works.</p> <p>The parser is not very complicated and will except expressions that are both normal to Python and completely illegal in Python. The main features are its ability to (1) identify simple assignment and mathematical operations, (2) identify constant floating point numbers, and (3) identify variables that would otherwise have no other meaning to the program. A limited number of error messages are given when appropriate but may leave one guessing what the problem really is. Mathematical operations are evaluated from left to right without regards to precedence, and assignment statements are evaluated from right to left.</p> Evaluator 2.0 (Python) 2010-11-25T18:24:09-08:00Stephen Chappell <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="">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 BIGOT <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=""> 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>