Computer >> Computer tutorials >  >> Programming >> Python

What is basic syntax of Python if...else statement?


In Python, the conditional statement if can have an optional else clause. The indented block after if statement is executed when logical expression in front of if is true. If an alternative action is sought to be taken by program if the expression is false, it is given as another indented block after else. The usage is described below −

if expr==True:
  #stmt1
  #stmt2
else:
  #stmt3
  #stmt4

Here first block containing stmt1 and stmt2 will be executed if expr is true. Whereas second block containing stmt3 and stmt4 will be executed if expr is false