better way than using if-else statement in python
if-statement, python
Solution
You can do:
s = [1, 2, 3, 4]
print 'y' if len(s) > 5 else 'n'
However I don't think this makes the code more readable (at a glance). Also note that `if` and `else` don't create a loop, they are simply statements for control flow. Loops are written using `for` and `while`.
Problem
Possible Duplicate: Putting a simple if-then statement on one line I am working on a python expression and I want that expression to be compressed than using the if else statement. ``` s = [1, 2, 3, 4] if len(s)>5: print s.index(5) else: print 'cant print' ``` Is there a better way than using as if else statement?