Operators and expressions
Most of the statements you write (logical lines) contain expressions (Expressions). A simple example of an expression is 2+3
. An expression can be split into operators (Operators) and operands (operands).
operators (Operators) are functions that perform certain operations and can be +
expressed with symbols such as, or special keywords. The operator requires some data to operate, which is known as the operand (operands).
Operator
+
Extra
- Add two objects.
3+5
The output 8
. ‘a‘ + ‘b‘
the output ‘ab‘
.
-
Reduction
- Subtract another number from one number, and the default is 0 if the first operand does not exist.
-5.2
Will output a negative number, 50 - 24
output 26
.
*
By
- Gives the product of two numbers, or returns the result of a string repeated a specified number of times.
- 2 * 3 output 6. ' La ' * 3 output ' Lalala '.
**
Powers
- Returns the Y-side of X.
3 ** 4
Output 81
(that is 3 * 3 * 3 * 3
).
/
Remove
- x divided by Y
13 / 3
Output 4.333333333333333
.
//
Divisible
x
Divides y
and takes the result down to the nearest whole number.
13 // 3
Output 4
.
-13 // 3
Output -5
.
%
(Take Mold)
- Returns the remainder after the division operation.
13 % 3
Output 1
. -25.5 % 2.25
output 1.5
.
<<
(move left)
- Shifts the digits of a number to the left by the specified number of digits. (each number is represented by a binary number in memory, i.e.,
0
and 1
)
2 << 2
Output 8
. 2
represented by a binary number 10
.
- Moving to the left
2
will get 1000
this result, which means in decimal 8
.
>>
(Move right)
- Shifts the digits of a number to the right by the specified number of digits.
11 >> 1
Output 5
.
11
expressed as in binary 1011
, the result is shifted right one after output 101
, which represents the decimal 5
.
&
(Bitwise AND)
- Bitwise AND operation of the numbers.
5 & 3
Output 1
.
|
(Bitwise OR)
- Bitwise OR operates on a number.
5 | 3
Output 7
.
^
(Bitwise XOR)
- Bitwise XOR or manipulation of a number.
5 ^ 3
Output 6
.
~
(bitwise reversed)
x
The bitwise inverse of the result is -(x+1)
.
~5
Output -6
.
<
Less than
- Returns whether x is less than Y. All comparison operators return a result of
True
or False
. Note the uppercase letters in these names.
5 < 3
Output False
, 3 < 6
output True
.
- The comparison can be made up of arbitrary constituent Links:
3 < 5 < 7
return True
.
>
Greater than
- Returns whether x is greater than Y.
5 > 3
Return True
. If the two operands are numbers, they will first be converted to a common type. Otherwise, it will always return False
.
<=
(less than equals)
- Returns whether x is less than or equal to Y.
x = 3; y = 6; x<=y
Return True
.
>=
(greater than or equal to)
- Returns whether x is greater than or equal to Y.
x = 4; y = 3; x>=3
Return True
.
==
Equal to
- Compares the equality of two objects.
x = 2; y = 2; x == y
Return True
.
x = ‘str‘; y = ‘stR‘; x == y
Return False
.
x = ‘str‘; y = ‘str‘; x == y
Return True
.
!=
(Not equal to)
- Compares whether two objects are not equal.
x = 2; y = 3; x != y
Return True
.
not
(boolean "Non")
- If X is
True
, it is returned False
. If X is False
, it is returned True
.
x = True; not x
Return False
.
and
(boolean "and")
- If X is
False
, it x and y
returns False
, otherwise the computed value of Y is returned.
- When X is
False
, it x = False; y = True; x and y
will return False. In this scenario, Python will not calculate Y because it already knows that the left side of the and expression is false, which means that the entire expression will be false and not a different value. This condition is known as short-circuit calculation (short-circuit Evaluation).
or
(boolean "or")
- If X is
True
, it returns True
, otherwise it returns the computed value of Y.
x = Ture; y = False; x or y
will return Ture. Here the short-circuit calculation is equally applicable.
Shortcuts for numeric operations and assignments
A more common operation is to perform a mathematical operation on a variable and return the result of the operation to the variable, so for this type of operation there is usually a quick way to express the following:
a = 2a = a * 3
You can also write:
a = 2a *= 3
Be aware that it 变量 = 变量 运算 表达式
becomes 变量 运算 = 表达式
.
Order of Evaluation
The priority table from the lowest priority to the highest priority in Python is given below.
lambda
: LAMBDA expression
if - else
: conditional expression
or
: boolean "or"
and
: boolean "and"
not x
: boolean "Non"
in, not in, is, is not, <, <=, >, >=, !=, ==
: Comparisons, including membership tests (membership Tests) and identity tests (identities Tests).
|
: Bitwise OR
^
: Bitwise XOR OR
&
: Bitwise AND
<<, >>
: Move
+, -
: Add and Subtract
*, /, //, %
: Multiply, divide, divide, take surplus
+x, -x, ~x
: positive, negative, bitwise reverse
**
: exponentiation
x[index], x[index:index], x(arguments...), x.attribute
: Subscript, slice, call, property Reference
(expressions...), [expressions...], {key: value...}, {expressions...}
: Represents a binding or tuple, represents a list, represents a dictionary, represents a collection.
The operators on the same row in the previous table have the same precedence. For example, + and-have the same priority.
Change the order of operations
- To make the expression easier to read, we can use parentheses.
- There is an added advantage to using parentheses-it can help us change the order of operations. As an example, if you want to calculate the addition before the multiplication in an expression, you can write the expression
(2 + 3) * 4
.
Binding nature
- Operators are usually combined from left to right . This means that operators with the same precedence are evaluated sequentially from left to right. If it is to be
2 + 3 + 4
(2 + 3) +4
calculated in the form.
An expression
Example (save it as expression.py
)
#表达式例子length=5breadth=2area=length*breadthprint("Area is",area)print("Perimeter is",2*(length+breadth))
Output:
$ python expression.pyArea is 10Perimeter is 14
How does it work?
The length and width of the rectangle (breadth) are stored in variables named with their respective names. We use them to calculate the area and perimeter (Perimeter) of the rectangle with an expression. We length *breadth
store the result of the expression in the variable area and print it out by using the print
function.
In the second case, we used the value of the expression directly in the Print function 2 * (length + breadth)
.
At the same time, you need to be aware of how Python beautifully prints out the results of the output. Although we do not specifically specify spaces between area is and variable area, Python automatically adds it to us, so we get a neat output, and the program is easier to read because of the way it is handled (because we don't need to consider whitespace issues in the string that we use to output).
Summarize
We've learned how to use operators, operands, and expressions--these are the basic blocks of any program we build. Next, we'll see how these statements are used in the program.
python-Operators and expressions