if condition :

Syntax: if condition :
.     statements
Description: executes statements if condition is true (or not 0)
Parameters: condition A value or an expression evaluates to a 0 or not 0
Returns: Nothing
Related:  if .. elif .. else,  while for, condition


 

The   condition can be a value or an expression. An expression may contain contain comparison operators. Such as == , < , >= etc.

If the condition evaluates to true value (or a non zero value) then the  statements that follow are executed once. (the statements are a block of code is where all the lines are indented by the same number of space characters)

Example:


a = 10
if a < 20:
    print('a is ', a)

The Condition may be an expression containing with multiple values and brackets.


a=15
b=10
if (a+b)/4 >= 5:
    print('the sum of a+b is', a+b)

The expression may be made of multiple parts separated by  and and or keywords.


a=15
b=10
c=2
d=3
if (a+b> 20) and (c+d==5) :
    print('the magic combination')

The condition may be a single value such as


a=-2
if a:
    print(a)

This condition works so long as the value a is not 0