I am trying to use a ternary condition in my program as follows:
c = 12
(print c) if (c == 10) else (print 'c is not 10')
However, this raises a syntax error. What seems to be wrong here?
You would do it like this:
print c if c == 10 else 'c is not 10'
The parenthesis are unnecessary, and you must start with the "print"; Python cannot make the whole command a conditional the way Perl can.
There is no Python equivalent of Perl's:
print "Done!" if $done;
you shall do print(c) instead of (print c)
c = 12 print(c) if (c==10) else print('not c') still gives syntax error. Its not about parenthesis as Will mentioned in his answer.
c = 12 ; (print (c)) if (c == 10) else (print ('c is not 10'))