please wait
|
|
5
to the variable x
. The assignment operation always takes place from right to left, and never the other way around:
|
|
x
the value contained in variable y
. The value of x
at the moment this statement is executed is lost and replaced by the value of y
.y
to x
at the moment of the assignment operation. Therefore, if y
changes at a later moment, it will not affect the new value taken by x
.
|
|
a:4 b:7 |
a
and b
(4 and 7, respectively). Notice how a
was not affected by the final modification of b
, even though we declared a = b
earlier.
|
|
y
is assigned the result of adding 2 and the value of another assignment expression (which has itself a value of 5). It is roughly equivalent to:
|
|
y
.
|
|
x
, y
and z
; always from right-to-left.operator | description |
---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
% | modulo |
%
), gives the remainder of a division of two values. For example:
|
|
x
containing the value 2, since dividing 11 by 3 results in 3, with a remainder of 2.expression | equivalent to... |
---|---|
y += x; | y = y + x; |
x -= 5; | x = x - 5; |
x /= y; | x = x / y; |
price *= units + 1; | price = price * (units+1); |
|
|
5 |
++
) and the decrease operator (--
) increase or reduce by one the value stored in a variable. They are equivalent to +=1
and to -=1
, respectively. Thus:
|
|
x
.++x
) or after it (x++
). Although in simple expressions like x++
or ++x
, both have exactly the same meaning; in other expressions in which the result of the increment or decrement operation is evaluated, they may have an important difference in their meaning: In the case that the increase operator is used as a prefix (++x
) of the value, the expression evaluates to the final value of x
, once it is already increased. On the other hand, in case that it is used as a suffix (x++
), the value is also increased, but the expression evaluates to the value that x had before being increased. Notice the difference:Example 1 | Example 2 |
---|---|
x = 3; |
x = 3; |
y
is the value of x
after being increased. While in Example 2, it is the value x
had before being increased.operator | description |
---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
|
|
a=2
, b=3
and c=6
, then:
|
|
=
, with one equal sign) is not the same as the equality comparison operator (operator ==
, with two equal signs); the first one (=
) assigns the value on the right-hand to the variable on its left, while the other (==
) compares whether the values on both sides of the operator are equal. Therefore, in the last expression ((b=2) == a
), we first assigned the value 2
to b
and then we compared it to a
(that also stores the value 2), yielding true
.!
is the Python operator for the Boolean operation NOT. It has only one operand, to its right, and inverts it, producing false
if its operand is true
, and true
if its operand is false
. Basically, it returns the opposite Boolean value of evaluating its operand. For example:
|
|
&&
and ||
are used when evaluating two expressions to obtain a single relational result. The operator &&
corresponds to the Boolean logical operation AND, which yields true
if both its operands are true
, and false
otherwise. The following panel shows the result of operator &&
evaluating the expression a&&b
:&& OPERATOR (and) | ||
---|---|---|
a | b | a && b |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
||
corresponds to the Boolean logical operation OR, which yields true
if either of its operands is true
, thus being false only when both operands are false. Here are the possible results of a||b
:|| OPERATOR (or) | ||
---|---|---|
a | b | a || b |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
|
|
(5==5)||(3>6)
), Python evaluates first whether 5==5
is true
, and if so, it never checks whether 3>6
is true
or not. This is known as short-circuit evaluation, and works like this for these operators:operator | short-circuit |
---|---|
&& | if the left-hand side expression is false , the combined result is false (the right-hand side expression is never evaluated). |
|| | if the left-hand side expression is true , the combined result is true (the right-hand side expression is never evaluated). |
|
|
i
by one, but only if the condition on the left of &&
is true
, because otherwise, the condition on the right-hand side (++i<n
) is never evaluated.true
, and a different one if the expression evaluates as false
. Its syntax is:condition ? result1 : result2
condition
is true
, the entire expression evaluates to result1
, and otherwise to result2
.
|
|
|
|
7 |
a
was 2, and b
was 7, so the expression being evaluated (a>b
) was not true
, thus the first value specified after the question mark was discarded in favor of the second value (the one after the colon) which was b
(with a value of 7).,
) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.
|
|
b
, and then assign b+2
to variable a
. So, at the end, variable a
would contain the value 5 while variable b
would contain value 3.operator | asm equivalent | description |
---|---|---|
& | AND | Bitwise AND |
| | OR | Bitwise inclusive OR |
^ | XOR | Bitwise exclusive OR |
~ | NOT | Unary complement (bit inversion) |
<< | SHL | Shift bits left |
>> | SHR | Shift bits right |
|
|
3.14
to an integer value (3
); the remainder is lost. Here, the typecasting operator was (int)
. Another way to do the same thing in Python is to use the functional notation preceding the expression to be converted by the type and enclosing the expression between parentheses:
|
|
|
|
x
is assigned the value 1
, because char
is a type with a size of one byte.sizeof
is a compile-time constant, so it is always determined before program execution.
|
|
x
, because the %
operator has a higher precedence than the +
operator, and is always evaluated before. Parts of the expressions can be enclosed in parenthesis to override this precedence order, or to make explicitly clear the intended effect. Notice the difference:
|
|
Level | Precedence group | Operator | Description | Grouping |
---|---|---|---|---|
1 | Scope | :: | scope qualifier | Left-to-right |
2 | Postfix (unary) | ++ -- | postfix increment / decrement | Left-to-right |
() | functional forms | |||
[] | subscript | |||
. -> | member access | |||
3 | Prefix (unary) | ++ -- | prefix increment / decrement | Right-to-left |
~ ! | bitwise NOT / logical NOT | |||
+ - | unary prefix | |||
& * | reference / dereference | |||
new delete | allocation / deallocation | |||
sizeof | parameter pack | |||
(type) | C-style type-casting | |||
4 | Pointer-to-member | .* ->* | access pointer | Left-to-right |
5 | Arithmetic: scaling | * / % | multiply, divide, modulo | Left-to-right |
6 | Arithmetic: addition | + - | addition, subtraction | Left-to-right |
7 | Bitwise shift | << >> | shift left, shift right | Left-to-right |
8 | Relational | < > <= >= | comparison operators | Left-to-right |
9 | Equality | == != | equality / inequality | Left-to-right |
10 | And | & | bitwise AND | Left-to-right |
11 | Exclusive or | ^ | bitwise XOR | Left-to-right |
12 | Inclusive or | | | bitwise OR | Left-to-right |
13 | Conjunction | && | logical AND | Left-to-right |
14 | Disjunction | || | logical OR | Left-to-right |
15 | Assignment-level expressions | = *= /= %= += -= | assignment / compound assignment | Right-to-left |
?: | conditional operator | |||
16 | Sequencing | , | comma separator | Left-to-right |
Constants | Index | Basic Input/Output |