While writing some Python code, I happened to use a conditional expression, a Python language feature.
Conditional expressions are expressions (not statements) that have if/else clauses inside them, and they evaluate to either one of two values (in the basic case), depending on the value of a boolean condition. For example:
for n in range(4): print n, 'is odd' if n % 2 == 1 else 'is even' 0 is even 1 is odd 2 is even 3 is oddHere, the conditional expression is this part of the print statement above:
'is odd' if n % 2 == 1 else 'is even'This expression evaluates to 'is odd' if the condition after the if is True, and evaluates to 'is even' otherwise. So it evaluates to a string in either case, and that string gets printed (after the value of n).
Excerpt from the section about conditional expressions in the Python Language Reference:
conditional_expression ::= or_test ["if" or_test "else" expression]
expression ::= conditional_expression | lambda_expr
Conditional expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations.
The expression x if C else y first evaluates the condition, C (not x); if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.
You can see that the definition of conditional_expression is recursive, since it is partly defined in terms of itself (via the definition of expression).
This implies that you can have recursive or nested conditional expressions.
Also, since the syntax of the Python return statement is:
return [ expression_list ](where expression_list means one or more expressions, separated by commas, it follows that we can use a nested conditional expression in a return statement (because a nested conditional expresssion is an expression).
Here is a small program to demonstrate that:
''' File: return_with_nested_cond_exprs.py Purpose: Demonstrate nested conditional expressions used in a return statement, to classify letters in a string as lowercase, uppercase or neither. Also demonstrates doing the same task without a function and a return, using a lambda and map instead. Author: Vasudev Ram Copyright 2017 Vasudev Ram Web site: https://vasudevram.github.io Blog: https://jugad2.blogspot.com ''' from __future__ import print_function from string import lowercase, uppercase # Use return with nested conditional expressions inside a function, # to classify characters in a string as lowercase, uppercase or neither: def classify_char(ch): return ch + ': ' + ('lowercase' if ch in lowercase else \ 'uppercase' if ch in uppercase else 'neither') print("Classify using a function:") for ch in 'AaBbCc12+-': print(classify_char(ch)) print() # Do it using map and lambda instead of def and for: print("Classify using map and lambda:") print('\n'.join(map(lambda ch: ch + ': ' + ('lowercase' if ch in lowercase else 'uppercase' if ch in uppercase else 'neither'), 'AaBbCc12+-')))Running it with:
$ python return_with_nested_cond_exprs.pygives this output:
Classify using a function: A: uppercase a: lowercase B: uppercase b: lowercase C: uppercase c: lowercase 1: neither 2: neither +: neither -: neither Classify using map and lambda: A: uppercase a: lowercase B: uppercase b: lowercase C: uppercase c: lowercase 1: neither 2: neither +: neither -: neitherAs you can see from the code and the output, I also used that same nested conditional expression in a lambda function, along with map, to do the same task in a more functional style
- Vasudev Ram - Online Python training and consulting Get updates (via Gumroad) on my forthcoming apps and content. Jump to posts: Python * DLang * xtopdf Subscribe to my blog by email My ActiveState Code recipesFollow me on: LinkedIn * Twitter Are you a blogger with some traffic? Get Convertkit:Email marketing for professional bloggers