Python If Statement

The python if statement is the most commonly used in conditional control statements. If the condition in the 'if statement' is true, then the block within the if statement are executed.

Python represents the block structure with indentation, not with begin and end brackets. There are several benefits to use this indentation. It reduces inconsistency, code from different sources follow the same indentation style and eliminates all the curly brackets.

Syntax of If Statement

If condition: statement1, statement2 ....

The condition is a boolean expression that determines whether or not the body will be executed. A colon must follow the condition. If the condition is true, the given sequence of statements will be executed.

The block is a block of one or more statements to be executed if the condition is true.


Example of If Statement

>>> x = 10
>>> if x > 10:
	print(x, "is a positive number")

>>> y = 20
>>> if y == 20:
	print("y is equal to 20")

y is equal to 20

Python if statement

Output of the above code
Python if statement





Read more articles


General Knowledge



Learn Popular Language