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 represented by symbols such as +
or special keywords. Operators require operands to operate on. In the case of 2 + 3
, 2
and 3
are the operands.
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:
>>> 2 + 3
5
>>>
Here is a quick overview of the available operators. First, the arithmetic operators:
-
+
(plus)- Adds two values
- Numeric addition:
3 + 5
gives8
,3 + 5.0
gives8.0
- String addition (concatenation):
'a' + 'b'
gives'ab'
. - You cannot mix strings and integers when using the
+
operator.
-
-
(minus)- Subtracts one number from another.
10 - 8
gives2
.- Negative numbers: The
-
sign is used to specify a negative number, e.g.-5.2
.
-
*
(multiply)- Gives the product of two numbers, or returns a string repeated N times.
- Numeric multiplication:
2 * 3
gives6
. - String repetition:
'la' * 3
gives'lalala'
.
-
**
(power)- Returns x to the power of y (exponentiation)
3 ** 4
gives81
(i.e.3 * 3 * 3 * 3
)
-
/
(divide)- Divide x by y
- In Python 2,
/
returns integer division:13 / 3
gives4
- In Python 3,
/
returns float division:13 / 3
gives4.333333333333333
-
//
(integer division, divide and floor)- Divide x by y and round the answer down to the nearest whole number
13 // 3
gives4
-13 // 3
gives-5
-
%
(modulo)- Returns the remainder after division
13 % 3
gives1
.-25.5 % 2.25
gives1.5
.
Comparison operators:
-
<
(less than)- Returns whether x is less than y. All comparison operators return the boolean values
True
orFalse
. 5 < 3
givesFalse
and3 < 5
givesTrue
.- Comparisons can be chained together:
3 < 5 < 7
givesTrue
.
- Returns whether x is less than y. All comparison operators return the boolean values
-
>
(greater than)- Returns whether x is greater than y
5 > 3
returnsTrue
.
-
<=
(less than or equal to)- Returns whether x is less than or equal to y
- if
x = 3
,x <= 6
will returnTrue
-
>=
(greater than or equal to)- Returns whether x is greater than or equal to y
- if
x = 3
,x >= 3
returnsTrue
-
==
(equal to)- Returns whether the objects are equal
- if
x = 2
andy = 2
, thenx == y
returnsTrue
- if
x = 'foo'
andy = 'FOO'
, thenx == y
returnsFalse
- if
x = 'foo'
andy = 'foo'
, thenx == y
returnsTrue
-
!=
(not equal to)- Returns whether the objects are not equal
- if
x = 2
andy = 3
, thenx != y
returnsTrue
Boolean operators:
-
not
(boolean NOT)- If x is
True
,not x
returnsFalse
- If x is
False
,not x
returnsTrue
.
- If x is
-
and
(boolean AND)- If either
x
ory
are False,x and y
returnsFalse
- If
x = False
andy = True
,x and y
returnsFalse
sincex
isFalse
. In this case, Python will not evaluatey
since it knows that the left hand side of theand
expression isFalse
, which makes the whole expressionFalse
irrespective of the other values. This is called short-circuit evaluation.
- If either
-
or
(boolean OR)- If either
x
ory
are True,x or y
returnsTrue
- If
x = True
andy = False
,x or y
returnsTrue
. Short-circuit evaluation applies here as well.
- If either
It is common to run a math operation on a variable and then assign the result of the operation back to the variable. Python's "augmented assignment" operators give you a shortcut for such expressions. Instead of writing:
a = 2
a = a * 3
You can use the augmented assignment operator *=
:
a = 2
a *= 3
Notice that var = var OP expression
becomes var OP= expression
.
If you had an expression such as 2 + 3 * 4
, is the addition done first or the multiplication? Our elementary school math class tells us that the multiplication should be done first. This means that the multiplication operator has higher precedence than the addition operator.
Just like the "order of operations" in math, Python has a similar order of operations 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.
For the complete table of operator precedence, see the Python reference manual. We can also explicitly change the order of evaluation by grouping expressions together with parentheses.
To change the order of evaluation and make expressions more readable we can use parentheses to group operations together. For example, 2 + (3 * 4)
is easier to understand than 2 + 3 * 4
, and requires no knowledge of the operator precedence. As with everything else, the parentheses should be used reasonably (don't overdo it) and should not be redundant, as in (2 + (3 * 4))
.
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
.
Operators are usually associated from left to right. This means that operators with the same precedence are evaluated in a left to right manner. For example, 2 + 3 + 4
is evaluated as (2 + 3) + 4
.
Example (save as expression.py
):
length = 5
width = 2
area = length * width
print('Area is', area)
print('Perimeter is', 2 * (length + width))
Output:
$ python expression.py
Area is 10
Perimeter is 14
How it works
The length and width 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 * width
in the variable area
and then print it using the print
function. In the second case, we directly use the value of the expression 2 * (length + breadth)
in the print function.
We have seen how to write expressions using operators, operands, and parentheses for grouping. These are the basic building blocks of any program. Next, we will see how to make greater use of these in our programs using control flow statements.