Python returns a false value (True or False) tips

Source: Internet
Author: User
In yesterday's blog about substring has the following code:

You may have found that in Python 3 there is a way to complete a function in just one line:
Copy the Code code as follows:


>>> def isSubstring2 (S1,S2):
Return True if S2.find (S1)!=-1 else False


But... Could it be easier?

How to make it easier to use Python to express conditional statements, just for fun:)

One approach is to use a list index:
Copy the Code code as follows:


>>> def isSubstring2 (S1,S2):
return [False,true][s2.find (S1)!=-1]


The principle is simple, the Boolean value TRUE is evaluated by the index as 1, and false is equal to 0. Can it be easier? Leave a message for me;-)

  • 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.