[Basic Python] 04. Operator (super detailed)

        As you can see from the previous section, the expression isOperator(operator) andOperand(operand) constitute the sequence. The function of operators is to accomplish something. They are represented by symbols like + or other specific keywords. Operators are divided into arithmetic operators, assignment operators, comparison operators, logical operators, bit operators, member operators and identity operators. The following will explain one by one.

1 Arithmetic operators

Insert picture description here
E.g:

>>>1+1                        #两个数字的相加
2
>>>'Hello'+' '+'World'        #两个字符串的相加
'Hello World'
>>>5-2                        #两个数相减
3
>>>2*3                        #两个数相乘
6
>>>'abc'*2                    #字符串和数字(正整数)的相乘
'abcabc'
>>>8/2                        #两个数相除,结果为浮点数
4.0
>>>7//2                       #整除,向下取正
3
>>>-7//2                      #整除,向下取正
-4
>>>7%2                        #取模
1
>>>2**3                       #幂运算
8
>>>25**0.5                    #开平方
5

2 Assignment operator

Insert picture description here

3 Comparison operators

Comparison operators are used to compare the relationship between two values ​​and return a boolean value. If the relationship holds, return True, otherwise return False.
Insert picture description here

  • If the comparison is a string, it is a comparison of the size of the ASCII code value, the comparison method is bit comparison.
    E.g:
>>>'c'>'a'        #比较c和a对应的ASCII码值
True
>>>'ab'>'b'       #对位比较,比较a和b对应的ASCII码值
False
>>> 'bc'>'ad'     #对位比较,仅比较两个字符串的第一位字符对应的ASCII码值即可
True
>>> 'ab'>'ac'     #对位比较,两个字符串的第一位字符相等,则比较b和c对应的ASCII码值
False
>>>'ax'<'xa'      #对位比较,仅比较两个字符串的第一位字符对应的ASCII码值即可
True

Illustration of bit comparison:
Insert picture description here
first compare the ASCII values ​​of the first character of the two strings, if they are not equal, then output True or False according to the comparison result, the following characters do not need to be considered; if they are equal, the two characters are compared The ASCII value of the second character of the string, and so on.

通过函数ord()可获得对应字符的ASCII码值;通过函数chr()可获得ASCII码值对应的字符。

4 Logical operators

The Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:
Insert picture description here

5 bit operator

Bitwise operators treat numbers as binary. The bitwise algorithm in Python is as follows: In the
following table, the variable a is 60 and b is 13 in binary format as follows:

a=0011 1100
b=0000 1101

Insert picture description here

6 Member operator

In addition to some of the above operators, Python also supports member operators. The test case contains a series of members, including strings, lists, or tuples.
Insert picture description here

7 Identity operator

It is used to compare whether the storage units of two objects A and B are consistent, that is, to determine whether the two identifiers A and B refer to an object.
Insert picture description here

通过id() 函数可以获取对象的内存地址。

What is the difference between the operator is and ==?

is compares whether the id values ​​of two objects are equal, that is, whether the two objects are the same instance object and whether they point to the same memory address.
== Comparing whether the contents of the two objects are equal, the _eq_ () method of the object is called by default.

After knowing the difference between is and ==, you can understand that Python has some pitfalls in integer comparison.

8 Other (ternary operator)

8.1 Syntax of ternary operator

The ternary operator is also called ternary operator or conditional operator. The ternary operator is to judge the variable first, and then assign it. Let's start with a specific example. Suppose there are two numbers now, and we want to get the larger one, then we can use if else statement, for example:

if a>b:
    max = a;
else:
    max = b;

But Python provides a more concise way of writing, as follows:

max = a if a>b else b

This is similar to the trinocular operator in other programming languages ​​?: Python is a minimalist programming language, it is not introduced ?: This new operator, but uses the existing if else keyword to achieve the same function.
The format of the ternary operator using if else is as follows:

exp1 if contion else exp2

condition is a judgment condition, and exp1 and exp2 are two expressions. If the condition is true (the result is true), exp1 is executed, and the result of exp1 is used as the result of the entire expression; if the condition is not true (the result is false), exp2 is executed, and the result of exp2 is used as the result of the entire expression.

The preceding statement max = a if a> b else b means:

  • If a> b holds, then use a as the value of the entire expression and assign it to the variable max;
  • If a> b is not true, then use b as the value of the entire expression and assign it to the variable max.

8.2 Nesting of ternary operators

The Python trinocular operator supports nesting, which can form more complex expressions. When nesting, you need to pay attention to the pairing of if and else, for example:

a if a>b else c if c>d else d

It should be understood as:

a if a>b else ( c if c>d else d )

[Example] Use the Python trinocular operator to determine the relationship between three numbers:

a = int(input('please enter the firest number:'))
b = int(input('please enter the second number:'))
c = int(input('please enter the third number:'))
(print('max_num=', a) if(a > c) else print('max_num=', c))if(a > b) else (print('max_num=', b) if a > c else print('max_num=', c))

9 Operator precedence

9.1 Priority order

Insert picture description here

9.2 Operator associativity

  • The so-called associativity is when there are multiple operators with the same priority in an expression, which operator is executed first: the one on the left is called left associativity, and the one on the right is called right associativity.
  • Most operators in Python have left associativity, that is, they are executed from left to right; except for unary operators (such as not logical not operators), assignment operators, and ternary operators, they have right associativity. That is from right to left. The above table lists the associativity of all Python operators.

Finally, a picture to summarize the content of this section.
Insert picture description here

Recommended reading:
[Python basics] 03. Basic concepts (expressions, statements, etc.) and identifiers and keywords
[Python basics] 02. Python environment construction and installation and configuration of PyCharm
[Python basics] 01. Introduction to Python
  Computer and programming Basic knowledge

Reference link:
http://blog.itpub.net/26736162/viewspace-2305456/
http://c.biancheng.net/view/2187.html

 

To be continued ...

Published 5 original articles · praised 3 · visits 319

Guess you like

Origin blog.csdn.net/qq_37572548/article/details/105493166