Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python: Expressions
by RS  admin@robinsnyder.com : 1024 x 640


1. Lua: Expressions

2. Expressions

3. Facial expressions
An expression is a formula that evaluates to and can be expressed as a literal value.

The word expression, from the Latin, literally means the "pressing out" (out being "ex").

A facial expression, such as a smile or frown, is pressed out from the face.

A computer code expression is a formula whose value is "pressed out" (or reduced) to a literal value.

4. Formulas and expressions
Be aware that mathematicians often call an expression a formula while computer scientists often call a formula an expression.


5. Integer arithmetic operations
Note: Floating point operations have no modulus and division yields a number with a fractional part.

Here are the expressions that we evaluated in converting the number 19 from decimal to binary.
Quotient , Modulus/Remainder 19 / 2 => , 9 19 % 2 => 1 9 / 2 => 4 , 9 % 2 => 1 4 / 2 => 2 , 4 % 2 => 0 2 / 2 => 1 , 2 % 2 => 0 1 / 2 => 0 , 1 % 2 => 1


6. Floating point numbers
A floating point number is an approximation of a real number.

What is 1.0/3.0?

The answer in math class is 0.3333333....

Mathematicians are rarely interested in efficiency. In computer terms, we do not have an infinite number of bits, and we need the division operation to compute fast, so we need to approximate the value. In such cases, a floating point approximation is used.

A double is a C/C++ floating point number. There is also a float type with less precision. Unless told to do so, always use double and not float.

7. Recursive
Something is recursive if it directly or indirectly refers to itself. Problems sometimes result if the recursive relationship is circular.

A procedure or function is recursive if the procedure and/or function calls itself either directly or indirectly.

Remember the message passing model.

8. Expressions: recursive definition
An expression is a formula that evaluates to a value. That is, an expression expresses a value. The recursive definition of expressions provides a simple and, yet, powerful way to recognize and evaluate expressions.

9. Parentheses
Consider the expression (3 + 4) * 5 that evaluates to 35. Here is the tree diagram for this expression. Expression tree for (3 + 4) * 5
Mathematicians think in terms of parentheses. Computer scientists write parentheses in programs, but think in terms of tree structures. One works up from the leaves to the root to determine the value of the expression.

10. Parentheses
Let us change the parentheses and use the expression 3 + (4 * 5) which evaluates to 23. Here is the tree diagram for this expression. Expression tree for 3 + (4 * 5)
Notice that tree structures have no parentheses. To a computer scientist, a program is just a big tree structure to be evaluated (and executed).

11. Precedence rule
The precedence rule for 3 + 4 * 5 is to do multiplication first, as in 3 + (4 * 5) instead of addition first as in (3 + 4) * 5.

Whenever one is in doubt, just use parentheses to get the desired expression evaluation.

12. End of page

by RS  admin@robinsnyder.com : 1024 x 640