A simple way to use conditional judgment statements in Python

Source: Internet
Author: User
The simplest conditional statement:

If expression:   expr_true_suite

As above, if is a keyword and expression is a conditional expression, conditional expressions support multiple conditional judgments, can be concatenated with Boolean operators and, or, and not, Expr_true_suite is a code block, expression is true, code block if only one row , the entire conditional statement above can be written to a single line, but with poor readability.
Conditional statements with Elif and else:

If expression1:   expr1_true_suiteelif expression2:   expr2_true_suiteelif expressionn:   exprn_true_ Suiteelse:   none_of_the_above_suite

As above, syntax is similar to conditional statements in other languages, and elif and else are optional.
Conditional expressions Implement ternary operators:
The three-dimensional operator is the following in C + + (e executes X at the time of creation, otherwise executes y)--

E? X:y

Ternary operators for Python simulations--

(E and [X] or [Y]) [0]

Implementation of the Python ternary operator--

X if E else Y

Take a look at some examples of judgments:

>>> if 1 < x < 2:   print (' True ')     

and represents and

or means >>> x 2 >>> if x = = 2 or x = = 3:   print (x)    

Returns a If B is true, otherwise returns C

A If B else c>>> ' True ' if 1 < x <2 els
  • 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.