Python en:Operators and Expressions

Introduction
Most statements (logical lines) that you write will contain expressions. A simple example of an expression is 2 + 3. An expression can be broken down into operators and operands.

Operators are functionality that do something and can be represented by symbols such as + or by special keywords. Operators require some data to operate on and such data is called operands. In this case, 2 and 3 are the operands.

Operators
We will briefly take a look at the operators and their usage:

Note that you can evaluate the expressions given in the examples using the interpreter interactively. For example, to test the expression 2 + 3, use the interactive Python interpreter prompt:

Shortcut for math operation and assignment
It is common to run a math operation on a variable and then assign the result of the operation back to the variable, hence there is a shortcut for such expressions:

You can write:

as:

Notice that var = var operation expression becomes var operation= expression.

Evaluation Order
If you had an expression such as 2 + 3 * 4, is the addition done first or the multiplication? Our high school maths tells us that the multiplication should be done first. This means that the multiplication operator has higher precedence than the addition operator.

The following table gives the precedence table for Python, from the lowest precedence (least binding) to the highest precedence (most binding). This means that in a given expression, Python will first evaluate the operators and expressions lower in the table before the ones listed higher in the table.

The following table, taken from the Python reference manual, is provided for the sake of completeness. It is far better to use parentheses to group operators and operands appropriately in order to explicitly specify the precedence. This makes the program more readable. See Changing the Order of Evaluation below for details.

The operators which we have not already come across will be explained in later chapters.

Operators with the same precedence are listed in the same row in the above table. For example, +</tt> and -</tt> have the same precedence.

Changing the Order Of Evaluation
To make the expressions more readable, we can use parentheses. For example, 2 + (3 * 4)</tt> is definitely easier to understand than 2 + 3 * 4</tt> which requires knowledge of the operator precedences. As with everything else, the parentheses should be used reasonably (do not overdo it) and should not be redundant (as in 2 + (3 + 4)</tt>).

There is an additional advantage to using parentheses - it helps us to change the order of evaluation. For example, if you want addition to be evaluated before multiplication in an expression, then you can write something like (2 + 3) * 4</tt>.

Associativity
Operators are usually associated from left to right i.e. operators with same precedence are evaluated in a left to right manner. For example, 2 + 3 + 4</tt> is evaluated as (2 + 3) + 4</tt>. Some operators like assignment operators have right to left associativity i.e. a = b = c</tt> is treated as a = (b = c)</tt>.

Expressions
Example:

Output:

$ python expression.py   Area is 10 Perimeter is 14

How It Works:

The length and breadth of the rectangle are stored in variables by the same name. We use these to calculate the area and perimeter of the rectangle with the help of expressions. We store the result of the expression length * breadth</tt> in the variable area</tt> and then print it using the print</tt> function. In the second case, we directly use the value of the expression 2 * (length + breadth)</tt> in the print function.

Also, notice how Python 'pretty-prints' the output. Even though we have not specified a space between 'Area is'</tt> and the variable area</tt>, Python puts it for us so that we get a clean nice output and the program is much more readable this way (since we don't need to worry about spacing in the strings we use for output). This is an example of how Python makes life easy for the programmer.

Summary
We have seen how to use operators, operands and expressions - these are the basic building blocks of any program. Next, we will see how to make use of these in our programs using statements.

Previous Next