Python interview-ternary conditional operator three-mesh operator

Source: Internet
Author: User
Tags pprint

The trinocular operator is an important part of C language. The conditional operator is the only operator with three operands, also known as a ternary operator.


In C-style languages, the ternary operator is in the following form:

<span style= "Font-family:arial;font-size:12px;color: #333333;" ><condition>? <expression1>: <expression2></span>

But there is no such operator in Python, and before Python 2.5, you can represent the ternary operator in the following way

<span style= "Font-family:arial;font-size:12px;color: #333333;" > (X, Y) [c]</span>

where c is the condition, if C is not true, then the first element x in the progenitor is returned, and if C is set, then the second element y is returned.

P.S. Not recommended. Because the expression x, Y will execute, and we only need one.


Example:

<span style= "Font-family:arial;color: #333333;" ><span style= "FONT-SIZE:12PX;" >val = float (raw_input (' Age: ')) print ' You should is ', (' working ', ' retired ') [val>65]# resultage:67you should be ret Ired</span></span>


Expression variants:

Age = 15# Selecting an item from a Tupleprint ((' Adult ', ' Kid ') [age <]) # which are the equivalent Of...print (' adult ', ' Kid ') [true]] # Or more explicit by using Dictprint ({True: ' Kid ', False: ' adult '}[age <]) # using Lambdaage = 15print (Lambda: ' Kid ', Lambda: ' adult ') [age >] ()) # using And/orage = 15print (age <) and ' kid ' or ' adult '



Starting with Python 2.5, when Pep 308 was adopted, a single-line if expression was introduced to replace the trinocular operator.

<expression1> if <condition> else <expression2>

In this way, expression 1 is only executed and returned when the condition is set, and Expression 2 is formed and returned. In this way, only one expression is executed and no resources are wasted.

Val=float (Raw_input (' Age: ')) "print ' You should is ', ' retired ' if val>65 Else ' working ' # resultage:76you should be Retired


Use the following code to detect:

Def f1 ():p rint "f1!" Return 1 def f2 ():p rint "f2!" Return 2 if __name__ = = "__main__": v = F1 () if True else F2 () print "V =", v print "-" * v2 = f1 () if False else F2 () pri NT "v2 =", v2    # resultf1!v = 1----------f2!v2 = 2

As we can see, only the F1 method is executed at first, and the second expression executes only the F2 method, and not two methods are executed.



Transformation of ternary expressions

Age = 15print (' Kid ') if-age < Else (' teenager ') if-age <-else (' adult ') # teenager# which is the same as:if age <:    If age <:        print (' Kid ')    else: print (        ' teenager ') Else:    print (' adult ') # teenager

We can use Pprint to check that the ternary expression of Python is from left to right.

From Pprint import pprintpprint (' expression_1 ') if Pprint (' condition_1 ') Else pprint (' expression_2 ') if Pprint (' Condition_2 ') Else pprint (' expression_3 ') # result ' condition_1 ' condition_2 ' expression_3 '



Reference:

http://www.pythoncentral.io/one-line-if-statement-in-python-ternary-conditional-operator/

Http://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator

http://oldj.net/article/python-ternary-operator/

Python interview-ternary conditional operator three-mesh operator

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.