Python binary operator, logic operation Short Circuit principle

Source: Internet
Author: User

The previous round spoke of Python's history and its basic applications. For this round, you can say Python operators. Very simple. Operators, as the name implies, are taught in math classes in school, subtraction. +-*/This is the way the computer is expressed. The only difference is that in a programming language, the expression is more special than that. There is a division that has a% of the remainder. For example, let's first say, divisible. To divide is to take an integer. Do not remainder. For example:

This is the acquisition of integers. Do not remainder 0.5. Why, I don't know. This is the mechanism of Python. Again, the remainder is taken, for example:

The origin of this 1 is that you use 9÷2 in mathematics to divide the other in accordance with the calculation. 2*4=8 9-8=1. I have no way of drawing it. This 1 is the remainder. Well, math should know what I'm talking about. Thank you.

Finished the whole number, said the remainder. Plus and minus are simple. You can do it directly, for example:

Here, the 1~9 seen is called an expression, and the + number is called an operator. These two add together to form a statement. So I'm going to give an assignment operation, what is an assignment, or a reference to this in a variable. For example:

As you can see here, I first assign a value of 1 to Num. And what does this + = mean? Put num this number 1 plus 1, then will come out of the result is, the meaning of the. What does it equal. Assign the variable num = 3 directly

In a logical operation, it is possible to save some written content. Then the same applies to subtraction, for example:

Here, I assign a value of 5 to NUM, and 5-1 is 4. That multiplication is the same. For example:

As you can see, I assign a value of 4 to NUM, and then you use 4÷2=2 and then 2*2=4. That's a good idea. Then divide and take the remainder, how to do it, for example:

The assignment num is 9, 9÷2 more than 1. The assignment num 5 5÷2 equals 2.5, leaving an integer, which is 2.

equals is not equal to the expression is equal to in Python = = two Plus, and not equal to,! = an exclamation mark equals number, take the opposite meaning. Let's make an example of whether it's right. For example:

Assignment, A is 4 B is 6, judge the first, a equals 6. The result must be wrong (False). Then judge A is not equal to 6. The result must be true.

Greater than less is also the same use, for example:

A assigns a value of 6, B assigns a value of 7, A is greater than >b must be wrong. Conversely < less than B is right.

You have read the above is equal to, not equal to, greater than less than the use of it. Let's upgrade. Apply the upper logical operator. To do the logical operation. What is a logical operation, and or not three is a logical operation. Let's start by saying what they represent and how to use it.

Usage of and

And Chinese translation comes to mean. When the condition 1and condition 2 is judged, if the first condition above is false. Then the second condition is no longer judged. For example:

In this example, condition 1 means that 9 is greater than 2, and condition 2 is 5 greater than 2. Their relationship is whether condition 1 and condition 2 are correct. It is clear that two conditions are established. So the display results are correct. As I said above, when condition 1 is false, condition 2 will not be judged. Because it follows a left-to-right count. This is the use of and.

Use of OR

or meaning in Chinese, that is to say, when two conditions, one result is true, then the judgment display is true. To illustrate:

Here you can see. Condition 1 in 9 is greater than 2, or 5 is less than 2. It is obvious that this 9 is greater than 2. It's true. The corresponding explanation is that when the first condition is judged to be true, no second judgment is made, so it is true, the second condition, when 5 is less than 2 or 9 is greater than 2, pay attention. The first condition here is not tenable. Is wrong. The second condition is then judged. So this true is to say that the second condition is set up to mean the right thing. Don't Mongha here!!!!

Use of not

Not in Chinese is not the meaning. So how does this apply? To illustrate:

Here, see a very interesting phenomenon. When 5 is greater than 3 o'clock, the normal judgment should be, yes Yes is true. How can it be 5 less than 3 is correct? Because not in the program, one of the functions is to reverse the result of the calculation. Make a real fake, fake it for real.

So what is the result of using and or not combining together? Example:

What is the meaning of this phenomenon, just now I said, the logical operation is from the left to the right of the operation, first count not is not the true,true is true meaning, add a not is not true, that is flase, then add a not, and the fake made the real. So the result of not true is true. Well, now that it's out, it's not going to be the back of the operation. So the direct display of a true.

Interested, you can find this kind of practice, do not Fameng oh.

Python binary operator, logic operation Short Circuit principle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.