Python conditional statements

Source: Internet
Author: User

The Python programming language specifies that any non-0 and non-null (NULL) values are true, 0, or null is false.

In Python programming, if statements are used in the execution of control programs, in the basic form:

if Judging Condition:    Execute statement ... Else :    EXECUTE statement ...

Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range. Else is an optional statement that can be executed when the condition is not true, for example:

 # example 1:if   basic usage flag  = Falsename  = "  luren   " if  name = =   python   ' : # Determine if the variable is "  python   "  flag  = true # when the condition is set to true print  "  welcome boss   "  else  : Print name # condition is not valid when output variable name 

Output Result: Luren

The judgment condition of the IF statement can be used >, <, = =, >=, <=. You can use the following syntax when judging a condition as multiple values

if Judging condition 1:    executing statement 1......elif judging condition 2:    executing statement 2......elif judging condition 3:    executing statement 3 ... Else :    EXECUTE statement 4 ...

Since Python does not support switch statements, multiple judgment conditions can be implemented with elif, and if it is judged that multiple conditions need to be judged simultaneously, you can use or and and, in the operation judgment of the OR and and, follow and take precedence over or in operations already x and Y X=true equals y, x or y x=true equals x rule.

 num=-1  ;  if  num<0  or Num>10   print ( "  true  "  )  else  : Print ( " Span style= "COLOR: #800000" >false   ") 
In the case of Python
First, the and priority is greater than or when no parentheses are added.
Second, the value of x or y may only be x or Y. X is True is X, X is False, Y
Third, the value of x and Y may only be x or Y. X is True is Y, X is False is x obviously,

1 5 4 4 4 1 is true and the value is 1

For, (15454, 1 true, value 4

Python conditional statements

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.