if statement in Python is very powerful decision making statement used to control the flow of execution of statements.
It is a two way decision making statement used with a relational or logical expression.
It can be used in five ways:
- Simple if
- If else
- Nested if
- If else if ladder
- Single Line if
1. Simple if statement
In simple if statement, there is only one condition and depending upon the condition, a particular set of statements will be executed.
The syntax of simple if statement is:
if condition : Statement-Block Statement-X |
condition is any relational or logical expression. If condition is true, only then Statement-Block will be executed.
If condition is false Statement-Block will not be executed.
Statement-Block: It is a set of Python statements, which will be executed if condition is true.
Statement-X: It may be a single statement or group of statements which will always be executed even if condition is true or false.
Simple if statement can be represented with the help of a flowchart as:
Example:
num=int(input(‘Enter a number:’)) if num>0 : print “value is +ve” print(“Program Ended”) |
Output
Enter a number:10
Value is +ve
Program ended
In the above program, Value of variable num has been put as 10.
Value of num is positive, so message value is +ve is displayed followed by the message Program Ended.
If you input a negative value, Condition becomes false, so Program Ended will be displayed only.
2. If else statement
There is only one condition in if else statemtn. If condition is true, one set of statements will be executed.
Otherwise another set of statements will be executed.
The syntax of if else statement is:
if condition : Statement-Block1 else: Statement-Block2 |
condition is any relational or logical expression. If condition is true, then Statement-Block1 will be executed.
If condition is false Statement-Block2 will be executed.
Statement-X will be executed in both the cases of condition to be true as well as false.
Statement-Block1: It is a set of Python statements, which will be executed if condition is true.
Statement-Block2: It is a set of Python statements, which will be executed if condition is false.
if else statement can be represented with the help of a flowchart as :
Example:
num1=float(input(‘Enter first number’)) num2=float(input(‘Enter second number’)) if num1>num2 : print(num1,”is largest”) else : print(num2,”is largest”) print(“Program ended”) |
Output:
Enter first number3
Enter second number4
4.0 is largest
In the above program, Value of variable num1 has been put as 3 and value of variable num2 has been put as 4.
Condition num1>num2 is true so message 4.0 is largest is printed followed by Program ended.
If this Condition becomes false, so message 3.0 is largest will be displayed followed by Program ended.
3. Nested if statement
In nested if else statement, one if statement is enclosed within another if statement. Depending upon these if statements, particular set of statements will be executed. The syntax of nested if statement is:
if condition1: if condition2: Statement-Block1 else: Statement-Block2 else: if condition3: Statement-Block3 else: Statement-Block4 |
condition1,condition2 and condition3 are relational or logical expressions. Initially condition1 is checked.
If it is true, condition2 is checked. If condition2 is true, Statement-Block1 gets executed.
If condition2 is false, Statement-Block2 will be executed.
If condition1 is false, condition3 will be checked. If it is true, Statement-Block3 will be executed.
If condition3 is false, Statement-Block4 will be executed.
Statement-Block1, Statement-Block2, Statement-Block3 and Statement-Block4 are set of Python statements.
Nested if statement can be represented with the help of a flowchart as:
Example:
num1=int(input(‘Enter first number’)) num2=int(input(‘Enter second number’)) num3=int(input(‘Enter third number’)) if num1>num2 : if num1>num3 : print (num1,”is largest “) else : print(num2,” is largest”) else : if num2>num3 : print (num2,”is largest “) else : print(num3,” is largest”) |
Output
Enter first number3
Enter second number5
Enter third number2
5 is largest
In the above program, value of variable num is input as 3, num2 is 5 and num3 is 2
Initially condition num1>num2 is checked.
It is false, So condition num2>num3 is checked. It is true, So message 5 is largest is displayed.
4. If elif ladder statement:
It is multiway decision making statement. In if else if ladder statement, there are multiple conditions one after another.
Depending upon these conditions, specific sets of statements are executed.
The syntax of if else if ladder statement is:
if condition1: Statement-Block1 elif condition2: Statement-Block2 elif condition3: Statement-Block3 :: else: Statement-BlockN |
condition1,condition2 and condition3 are relational or logical expressions. Initially condition1 is checked. If it is true, Statement-Block1 gets executed.
In case condition1 is false, condition2 is checked. If it evaluates to true, Statement-Block2 gets executed.
Suppose if condition2 is false, condition3 gets checked. If it is true, Statement-Block3 gets executed and so on.
If all the conditions are false, Statement-BlockN gets executed.
Statement-Block1, Statement-Block2, Statement-Block3 and Statement-BlockN are set of Python statements.
if else if ladder statement can be represented with the help of a flowchart as :
Example:
day=int(input(‘Enter value of week day’)) if day==1 : print(“Sunday”) elif day==2 : print(“Monday”) elif day==3 : print(“Tuesday”) elif day==4 : print(“Wednesday”) elif day==5 : print(“Thursday”) elif day==6 : print(“Friday”) elif day==7 : print(“Saturday”) else : print(“Wrong day”) |
Output
Enter value of week day2
Monday
In the above program, value of variable day is input as 2
Initially condition day==1 is checked. It is false,
So condition day==2 is checked. It is true, So message Monday is displayed.
5. Single Line if statement
In single line if statement, there is only one condition and depending upon the condition, a particular statement will be executed.
The syntax of single line if statement is:
if condition : Statement-1 Statement-X |
condition is any relational or logical expression. If result of condition is true, only then Statement-1 will get executed. If it is false Statement1 will not be executed.
Statement-1 It is a Python statement which gets executed if condition is true.
Statement-X: It is a single statement or group of statements which will always get executed
Single Line if statement can be represented with the help of a flowchart as:
Example:
num=int(input(‘Enter a number:’)) if num>0 : print (“value is +ve”) print(“Program Ended”) |
Output
Enter a number:10
Value is +ve
Program ended
In the above program, Value of variable num is put as 10. As the value of num is positive, so message value is +ve is shown followed by the Program Endedmessage.
If you input a negative value, Condition becomes false, so Program Ended will be displayed only.