An approach to embedding the IF statement in Python

Source: Internet
Author: User
There may be a situation when you want to check other conditions after a condition resolves to true. In this case, you can use a nested if structure.

In a nested IF statement structure, you can have another if ... else structure inside an if ... else structure. elif. elif.
Grammar:

The syntax for nesting if...elif...else structures can be:

If expression1:  statement (s)  if expression2:   statement (s)  elif Expression3:   statement (s)  Else   statement (s) elif Expression4:  statement (s) Else:  statement (s)

The limit, please correct me. Please be sure to indicate the source of the reprint.

There may be a situation when you want to check other conditions after a condition resolves to true. In this case, you can use a nested if structure.

In a nested IF statement structure, you can have another if ... else structure inside an if ... else structure. elif. elif.
Grammar:

The syntax for nesting if...elif...else structures can be:

If expression1:  statement (s)  if expression2:   statement (s)  elif Expression3:   statement (s)  Else   statement (s) elif Expression4:  statement (s) Else:  statement (s)

For example:

#!/usr/bin/pythonvar = 100if var < $:  print "Expression value is less than"  if var = =:   print "Whi CH is  elif var = =:   print "which is"  elif var = =:   print "which is" Elif var <:  Print "expression value is less than" else:  print "Could not" find true Expression "print" Good bye! "

When executing the above code, it produces the following result:

Expression value is less than 200Which is 100Good bye!
  • 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.