Python's judgment

Source: Internet
Author: User

Today we're going to talk about Python's judgment. Yes, and our previous science of the Boolean value, that is, the judgment is true when the return of 1, the judgment is false when the return of 0. In fact, it is up to us to determine the true and false criteria.

Let's take a look at how this conditional judgment statement is implemented, first of all, you have to write the criteria for judging, that is, to judge the true and false criteria, and then write if the judgment is true, the next step in the implementation of the situation. Then write if you judge the execution of the false. This is probably the framework of the entire judgment statement. Let's take a look at the sample code:

 LSF = 1if  LSF = = 1 :  print  ( " yes  "  )  else  :  print  ( " no  "  )  #   press ENTER, The resulting result is: Yes   

Let's explain the code: assign the LSF a value of 1 first, and then use the IF statement to determine. We said before that a = is an assignment, two = is used to determine whether the equality, if the lsf==1 after the if the LSF value is 1, the result is true, if the LSF value is not 1, the result is false. Obviously, the result is true, because we assign a value of 1 to LSF in the first line of code, and the result is true if the first code behind the If, that is, print (' yes '), so we see the result is yes. Of course, we can change the IF statement, change 1 to 2, the result is false, execute the code behind the else, the output will become No. Yes and no are all custom, because we use that print statement, you can also write other statements, such as if it is true, lsf+1 and so on.

You might think that if there are more than two conditions to judge, there may be three or more conditions. This time we are going to introduce a new keyword: elif. Let's take a look at the sample code:

 LSF = 1if  LSF >  0:  print  ( " yes  "  )  elif  LSF <  0:  print  ( " none  "  )  else  :  print  ( " no  "  )  #   press ENTER, The resulting result is: Yes   

Let's explain the above statement, assignment, If,else are the same as above, but there is a elif between if and else, let's simply say that this elif:elif is equivalent to the else if, you can take it directly as a condition of judgment, Actually, it's almost like else. The code first to determine whether LSF is greater than 0, yes, the output yes, not the words to execute the following judgment statement, to determine whether LSF is less than 0, is the output of none, not the words on the execution of the next sentence, that is, else, because did not write the judging conditions, so, As long as it does not conform to either of the two cases, it will be in the other's scope, and in the else context, output No.

Let's take a look at some common judgments:

equals: = =

Less than:<

Greater than:>

Greater than or equal to: >=

Less than or equal to: <=

Not equal to:! =

OK, today's content is here!

(If the article image is infringing, please contact the author to delete)

Python's judgment

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.