Multiple ways to If-else in Python

Source: Internet
Author: User
A, b= 1, 2 assigns the maximum value in a and B two variables to C (1) General notation if a>b:c = Aelse:c = B (2) expression C = A If a>b else B (3) Two-dimensional list c = [B,a][a>b] (4) logical Assignment c = (A>b and [a] or [b]) [0] Analysis: 1, 2 for the basic syntax of the program does not discuss 3: First a>b value is true or false, while in Python the default value of True is 1False, the default value is 0. can be c = [B, a][1] or [B, a][0], that is, from the list by the index of the way to take the value to C. 4: First in Python, the result returned for the logical operator and, or:and:x, and y is the value that determines the result of the expression. According to the "short-circuit" principle, when X is true, the true and false of the expression is determined by the value of Y, all return y, and if X is False, the x is returned without the judgment of Y. Or:x or Y when X is true directly returns the value of x, when x is False, Y is true, the value of Y is returned, and Y is returned when all is false. So when in the formula allele: When A>b is true: A>b and [A] is true, not in or judgment, return [a] when A>b is false: A>b and [A] is false, return [b] to the list [a] or [b] by subscript 0 to take a value, get a or B.

Multiple ways to If-else in Python

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.