IF Statements
IF Statements
IF Statement:
Python If statement is a conditional statement wherein a set of statements is executed based on the
result of the condition.
If condition is true (if Boolean is true) the program executes a set of statements but if the
condition is false, the program ignores the statement(s) and moves onto the next part of the
program.
The syntax for the IF statement is:
if boolean_expression:
statement(s)
a=2
b=5
if a<b:
print(a, 'is less than', b)
2 is less than 5
Example 2:
a = 24
b = 5
if a<b:
print(a, 'is less than', b)
Output
blank
The condition provided in the if statement evaluates to false, and therefore the statement inside the if
block is not executed.
Example 3:
a = 2
b = 5
c = 4
Example 4: Nested If
You can write a Python If statement inside another Python If statement. This is called nesting.
a = 2
if a!=0:
print(a, 'is not zero.')
if a>0:
print(a, 'is positive.')
if a<0:
print(a, 'is negative.'
Example 5:
Find Smallest of Three Numbers using IF
Python Program
smallest = 0
Output