The ternary operation of Python

Source: Internet
Author: User

The ternary operation of Python is to first output the result and then determine the condition. The format is as follows:

>>> def f (x, Y):
return x-y if x>y else abs (x-y)
#如果x大于y就返回x-y value, otherwise returns the absolute value of X-y

>>> f (3,4) #3 <4, does not meet the IF condition, it returns the absolute value inside the Else
>>> F (4,3)
>>> def f (x, Y):
Return 1 if X>y else-1
#如果x大于y就返回x-y value, or return-1
>>> f (3,4) #3小于4, return-1
-1
>>> f (4,3) #4大于3, return 1
>>>

The ternary operation of Python

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.