Expressions
Last updated
Last updated
Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands. The operators +, -,
*, /
and **
perform addition, subtraction, multiplication, division and exponentiation, as in the following examples:
When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed.
Python 3, as well as other languages have an integer division //
. The integer division operates floor division, that is the operator returns the whole part of the division. The result of the integer division is an integer. If both operands are integer the value returned is a integer. Otherwise, if at least one of the operand is a float, the value returned is a float.
An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x
has been assigned a value):
If you type an expression in interactive mode, the interpreter evaluates it and displays the result:
But in a script, an expression all by itself doesn't do anything! This is a common source of confusion for beginners.
You can use a variable in an expression, and you can assign the result of an expression to a variable. In addition, a variable can be used on both side of the assignment operator =
like in x = x + 1
. In this assignment statement the result of x + 1
is assigned to x
. For example, if x
has the value 1
before the assignment statement is executed, then x
becomes 2
after the statement is executed.
When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way to remember the rules:
Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1)
is 4, and (1+1)**(5-2)
is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60
, even if it doesn't change the result.
Exponentiation has the next highest precedence, so 2**1+1
is 3, not 4, and 3*1**3
is 3, not 27.
Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1
is 5, not 4, and 6+4/2
is 8, not 5.
**
Exponentiation (raise to the power)
+, -
Unary plus and minus (method names for the last two are +@ and -@)
*, /, %, //
Multiply, divide, modulo and floor division
+, -
Addition and subtraction
>>, <<
Right and left bitwise shift
<=, <, >, >=
Comparison operators
<>, ==, !=
Equality operators
is
, is not
Identity operators
in
, not in
Membership operators
not
Logical operator
and
Logical operator
or
Logical operator
= ,%=, /=, //=, -=, +=, *=, **=
Assignment operators
The modulus operator works on integers and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%
). The syntax is the same as for other operators:
The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another - if x % y
is zero, then x
is divisible by y
. Also, you can extract the right-most digit or digits from a number. For example, x % 10
yields the right-most digit of x
(in base 10). Similarly x % 100
yields the last two digits.
In general, you cannot perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal:
The +
operator works with strings, but it might not do what you expect: it performs concatenation, which means joining the strings by linking them end-to-end. For example:
The output of this program is throatwarbler
.
The *
operator also works on strings; it performs repetition. For example, 'Spam'*3
is 'SpamSpamSpam'
. If one of the operands is a string, the other has to be an integer. This use of +
and *
__ makes sense by analogy with addition and multiplication. Just as 4*3
} is equivalent to 4+4+4
, we expect 'Spam'*3
to be the same as 'Spam'+'Spam'+'Spam'
, and it is.
On the other hand, there is a significant way in which string concatenation is different from integer addition. Concatenation is not commutative, which means that 'spam'+'sandwich'
is not the same as 'sandwich'+'spam'
, whereas 3+4
is the same as 4+3
.
In mathematics, denotes an equation (where is the solution). In python, it is an assignment statement, and if x
has the value 1
before the statement, executing the statement would change x
to 3
.
Operators with the same precedence are evaluated from left to right. So in the expression degrees/2*pi
, the division happens first and the result is multiplied by pi
, therefore the expression is equal to . To divide by , you can use parentheses (e.g. degrees/(2*pi)
or write degrees/2/pi
.
So 7 divided by 3 is 2 with 1 left over, or in other words .