1. If Statements
The if statement in python is same as c language which is used test a condition. If condition is true, statement of if block is executed otherwise it is skipped.
Syntax of python if statement:
if(condition):
statements
Example :
a=10
if a==10:
print ("Hello User")
Output:
Hello User
2. If Else Statements
Syntax:
if(condition): False
statements
else: True
statements
Example :
year=2000
if year%4==0:
print ("Year is Leap")
else:
print ("Year is not Leap")
Output:
Year is Leap
3. If Else If Statement:
When we need to check for multiple conditions to be true then we use elif Statement.
This statement is like executing a if statement inside a else statement.
Syntax:
If statement:
Body
elif statement:
Body
else:
Body
Example:
a = 10
if a >= 20:
print('Condition is True')
elif a >= 15:
print('Checking second value')
else:
print('All Conditions are false')
Output:
All Conditions are false.
The if statement in python is same as c language which is used test a condition. If condition is true, statement of if block is executed otherwise it is skipped.
Syntax of python if statement:
if(condition):
statements
Example :
a=10
if a==10:
print ("Hello User")
Output:
Hello User
2. If Else Statements
Syntax:
if(condition): False
statements
else: True
statements
Example :
year=2000
if year%4==0:
print ("Year is Leap")
else:
print ("Year is not Leap")
Output:
Year is Leap
3. If Else If Statement:
When we need to check for multiple conditions to be true then we use elif Statement.
This statement is like executing a if statement inside a else statement.
Syntax:
If statement:
Body
elif statement:
Body
else:
Body
Example:
a = 10
if a >= 20:
print('Condition is True')
elif a >= 15:
print('Checking second value')
else:
print('All Conditions are false')
Output:
All Conditions are false.