0

In java script there is an alternative to if statement i mean like ternary operator : (boolean expression )? option 1: option 2, it is elegant compact and sometimes less confusing. I have searched a lot to find an alternative for 'if' statement in python but i couldn't find anything. Is there any way to trick python so we can avoid if statement ?

Charles Duffy
  • 257,635
  • 38
  • 339
  • 400
Sam Farjamirad
  • 227
  • 1
  • 12

1 Answers1

1

If expression:

option1 if boolean_expression else option2
yuri kilochek
  • 12,018
  • 2
  • 28
  • 57
  • See "Answer Well-Asked Questions" in https://stackoverflow.com/help/how-to-answer, particularly guiding against answering questions you know are duplicative. – Charles Duffy Jul 23 '17 at 18:27
  • The inline if statement ain't any replacement for if statment – Sam Farjamirad Jul 23 '17 at 18:28
  • 1
    @SamFarjamirad, just because it uses the word "if" doesn't mean it's the same syntax. It's **absolutely** a different thing (different grammar in the parser even if it uses the same keywords). – Charles Duffy Jul 23 '17 at 18:28
  • 1
    @SamFarjamirad yes it is. It's an expression that yields a result, not just causes side effects. – yuri kilochek Jul 23 '17 at 18:30
  • @CharlesDuffy thanks , but i'm already convinced , just one more thing, which one is faster ? – Sam Farjamirad Jul 23 '17 at 18:31
  • @SamFarjamirad, ..."faster"? Neither is necessarily faster. They do different things, as yuri pointed out above, and selection should be made in accordance with which one best models intent. – Charles Duffy Jul 23 '17 at 18:32