16. Python's if condition judgment syntax

Insert picture description here
Pass is a grammar, this is a grammar that only Python has, because Python code judges a statement by line alignment (the role of typesetting in Python is very important), but there is a problem at this time, you are like the above When you wrote elif judgment condition 2, if I did not immediately follow the execution statement under judgment condition 2, what should you do here? I just don’t want to deal with this one for the time being, but if you don’t have this execution statement on your side, there will be an error in the grammar here, and you can’t find this grammar, so we can add a pass to add this at this time. An alignment indicates this execution statement.

You know that there is a switch in C or Java language, but there is no switch statement in our Python language. If so many conditions are judged, you must use if...elif... to judge, so this syntax is very common.

Python supports two inequality symbols:
Insert picture description here
Insert picture description here
Insert picture description here
The logical operator of the Python language is and or not, not the type of C language && |! Oh.

Remember to add a colon after each judgment condition of the if statement!

Conditional operator:
Insert picture description here
Logical operator:
Insert picture description here
Member operator:
Insert picture description here
Insert picture description here
Dictionary In the if condition judgment, the default search is the key. If you want to judge whether the value is in the dictionary, you can use the dic.values() function as shown in the figure above.

Identity operator:
Why is there an identity operator?
Each of our variables is not the actual value stored, but an address, which is a reference to an address. Then there may be two variables pointing to the same address. At this time, we have to judge whether they are the same. In fact It is equivalent to: id(a) == id(b).
Insert picture description here
We know that in Python, some small integer types are cached internally, which means that the same object is used. At this time, it is like the above As shown in the figure, two variables point to the same object, and they are equal.
If the value is particularly large, the two variables may not be equal:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/109273733