Python ternary operations

Source: Internet
Author: User

  ternary operations, also known as trinocular operations, are shorthand for simple conditional statements .

For example: Simple conditional statement ———— > Ternary operation

If condition established:    val = 1else:    val = 2

  

val = 1 if condition set up else 2

  

The ternary operators in Python are not like other languages, others are generally

The result of a true time: a false result

Such as:

result = 5 >3?1:0

In Python, the format is:

As a result of the true time if the condition is determined and  else is false
Result =x if condition else y

Such as:

1 if 5>0 else 0

  

def func (x, y    ): " " defines a function, if x is greater than Y, returns XY, otherwise, returns Y-x ""    return if else y= func (ten,= func) Print (r1) print (r2) results:86

Python ternary operations

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.