Python: Conditional expression or C ternary select
Articles may may have files attached at the end of the post
Submitted by chantra on Tue, 04/08/2008 - 20:31
Conditional expressions in python does not have the same syntax than in C.
Python uses the and and or statements instead of the a ? b : c .
This code sample shows how to get the same result in python.
- #!/usr/bin/env python
- a = 3
- print "a = 3 =>"
- print "a is ",
- print (a > 3) and "higher than 3" or "lower or equal to 3"
- print "and"
- print "a is ",
- print (a >= 3) and "higher or equal to 3" or "lower than 3"
will output:
$ ./conditional.py a = 3 => a is lower or equal to 3 and a is higher or equal to 3












