If condition judgment statement 21

Source: Internet
Author: User

One, Python's comparison operation

1. All Python objects support comparison operations

1) can be used to test the equality, relative size, etc.;

2) If it is a composite object, Python will check all its parts, including automatically traversing all levels of nested objects until the final result can be obtained;

2. Test operator

1) The equality of the test value of the "= =" operator;

2) "is" expression test object consistency;

For example:

In [1]: a=5 in [2]: b=6in [3]: a>bout[3]: falsein [4]: l1 ={' x ', ' y ', ' z '}in [5]: l2={' x ', ' y ', ' z '}in [6]: id (L1) out[6]: 39385440in [7]:  id (L2) out[7]: 39385672in [8]: l1 == l2out[8]: truein [9]: x  in l1                        //x is not quoted and is considered a variable ;---------------------------------------------------------------------------nameerror                                   Traceback  (Most recent call  last) <ipython-input-9-87d65672e822> in <module> ()----> 1 x in  l1NameError: name  ' x '  is not definedin [10]:  ' X '  in l1out[10]: true

Different types of comparison methods in 3.python

1) Number: compare by relative size;

2) String: Compare in dictionary order by character;

3) List and tuple: From left to right to compare the contents of each part;

4) Dictionary: Compare the list of (keys, values) after sorting;

4.python True and False

1) Any non-0 numeric and non-empty objects are true;

2) The number 0, the empty object and the special object none are false;

3) Comparisons and equivalence tests are applied recursively to the data structure;

4) The return value is true or false;

5. Combination condition test

1) x and Y: Logic and arithmetic;

2) x or Y: logic or operation;

3) x or Y: non-operational;

Second, if condition test syntax format

1.if Branch Syntax

If Boolean_expression1:suite1elif boolean_expression2:suite2...else:else_suite

The elif statement is optional;

Pass is used only for placeholders, and then when the related statements are filled;

2.if Combination Conditional syntax

A = X if Y else Z

If Y://If y is established, perform a=x.    If Y is not established, perform a=x. A = xelse a = Z

Example of 2.if condition judgment

Dual Branch in [14]: x = 5in [15]: y = 7in [16]: if x  > y:   ....:     print  "The max number is  %d. "  % x   ....: else:   ....:     print   "the max number is %d."  % y   ....:     the max number is 7.// Multi-branch in [17]: name =  "Jack" in [18]: if name ==  "Tom":    ....:     print  "It ' s %s."  % name   ....: elif name ==  "Mary":   ....:      print  "It ' s %s."  % name   ....: else:   ....:     print   "Alien."    ....:      alien.//Combination condition judgment In [25]: a = 7in [26]: b = 9in [27]: max  = a if a > b else bin [28]: print max9

        

This article is from "Jessen Liu's blog," Please make sure to keep this source http://zkhylt.blog.51cto.com/3638719/1707686

If condition judgment statement 21

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.