Operator

1. Operator

1.1 The concept of operators

Operators are used to perform program code operations, and will perform operations on more than one operation item. For example: 2+3, the operands are 2 and 3, and the second operator is "+".

1.2 Classification of operators

1.2.1 Arithmetic operators

"+", "-", "*", "/", "//" (division, rounding), "%" (modulo is also called remainder) "**" (power operation, also can be square root )
Note: Adding strings equals splicing;
Insert picture description here

Strings cannot be subtracted;
Insert picture description here
operations involving floating-point numbers all return floating-point numbers;
Insert picture description here
because the operations must be converted to binary, some decimal operations will produce scientific notation. Such as:

print(0.1+0.1+0.1-0.3)

The result should be 0, but the result is:

5.551115123125783e-17

How to solve this problem? Introduce the decimal method to solve floating point problems:

from decimal import Decimal
print(decimal('0.1')+decimal('0.1')+decimal('0.1')-decimal(0.3))

The result is:

0.0

Multiplying a number and a string is equal to doubling the number of strings;
Insert picture description here

String and string cannot be multiplied;
Insert picture description here

When integers are divided, they always return floating-point numbers; 0 cannot be used as a divisor; strings cannot be divided;
Insert picture description here

Divide can only return the integer bits of the result;
Insert picture description here

Take the remainder to return the remainder of the division of two numbers;
Insert picture description here

-10//3=-4

To explain, originally, it should be -3.33333. Because it needs to be rounded down, the result is -4
.

10%3=1
-10%3=2

To explain why this is the case, please see the following derivation process:

10%3=1  的计算过程: 10//3=3  余数的计算是    10-3×3=1
-10%3=2 的计算过程: -10//3=-4 余数的计算是    -10-3×(-4)=2

In the rounding operation, the rounding down is followed, and then the remainder is calculated.
The calculation of finding quotient and remainder can also be done like this:

x=10
y=3
print(divmod(x,y))

result

(3, 1)

We can verify:

x=-10
y=3
print(divmod(x,y))

result

(-4, 2)

Exponentiation can also be used as a square root, and the square root returns a decimal
Insert picture description here

1.2.2 Assignment Assignment Operator

"=" assigns the value on the right side of the symbol to the variable on the left side of the symbol. If there are multiple assignments, keep the latest assignment.
Insert picture description here
For assignments like x=x+3, there are simple arithmetic in python:
x = x + 3 # need to re-assign
x x += 3 # equivalent to x = x + 3
x -= 3 # equivalent At x = x-3
x *= 3 # equivalent to x = x * 3
x /= 3 # equivalent to x = x / 3
x **= 3 # equivalent to x = x ** 3
x // = 3 # equivalent to x = x // 3
x %= 3 # equivalent to x = x% 3

1.2.2.1 Extension

The rule for finding the remainder on behalf of negative values ​​in VF is: the sign of the remainder depends on the sign of the divisor, -10%3 is -1 according to normal division, but the correct result is the divisor + remainder, that is, 3+(-1)=2; 10%-3 is 1 according to the normal division method, but the correct result is to use the divisor + remainder, that is, -3 + 1=-2; -10%-3=-1 is the division with the same sign, and the sign of the remainder depends on the sign of the divisor , So -10%-3=-1. (taken from the Internet)

1.2.3 Comparison operators (relational operators)

symbol meaning
> Compare whether the value on the left is greater than the value on the right
>= Compare whether the value on the left is greater than or equal to the value on the right
< Compare whether the left value is less than the right value
<= Compare whether the value on the left is greater than or equal to the value on the right
== The comparison is whether the values ​​of the two objects (data) are equal
!= The comparison is whether the values ​​of the two objects (data) are not equal
is Compare whether two objects are the same object and compare the object id
is not Compare whether two objects are not the same object compare the object id

Note: The comparison between strings is their ASCII value.
Multi-digit string comparison, pairwise comparison, if the first digit compares the size, the second digit will not be looked at. Those with characters in a certain position are greater than those without characters.
Insert picture description here
Insert picture description here
Insert picture description here

1.2.4 Logical operators

  • not Logical negation operation : the value on the right side of the symbol can be logically negated.
    Insert picture description here
    The logical negation operation of a non-Boolean value first converts it to a Boolean value. All 0, empty, and none represent the meaning of emptiness. The values ​​are False, and the rest are True.
    Insert picture description here
  • and Logic AND finds False, if the first value is False, the second value is not looked at.
    Insert picture description here
  • Or logical or finds True, once True appears, the program stops immediately, and the following will not be executed.
    Insert picture description here

1.2.5 Non-Boolean logical operations

When we perform a logical operation on a non-Boolean value, Python will treat it as a Boolean value and finally return the original value (not a Boolean value).
Insert picture description here
Insert picture description here
Insert picture description here

1.2.6 Conditional Operator (Ternary Operator)

The format is: the result when the condition is true if the judgment condition is the result when the else condition is false. For example:
a=1
b=2
h=ab if a>b else a+b
then the value of h is 3. h=a+b
Example: Find the maximum value of a and b.
a=30
b=40
m=a if a>b else b
print(m)
Execution result:
Insert picture description here
homework: find the maximum value between a, b, and c, use conditional operators.
Answer:
A = 20 is
B = 30
C = 40
m = A IF A> B and A> C the else B IF B> A and B> C the else C IF C> A and C> B the else A
Print (m)
Insert picture description here
on FIG. Output result

1.2.7 Precedence of operators

Let's look at the AND operation first. We use an expression to guess their priority
a=2 or 3 and 4 What is the value of
print(a)
?
If or has a higher priority than and, or is calculated first, then the value of a is 2. Calculating and, the value of 2 and 4 a is 4.
If and has a higher priority than or, and is counted first, then the value of a is 4. Calculate the value of or, 2 or 4, and the result of a is 2.
The output result is:
Insert picture description here
You can find the order of precedence of operators in the python documentation.
Insert picture description here
End of this section.

Guess you like

Origin blog.csdn.net/m0_46738467/article/details/109447624