Till now we are executing our python program sequentially. Let us suppose a condition where we need to execute a certain part of code based on condition satisfied. For example, we are given marks of a student in Maths. We need to print pass if he has obtained more than 40 marks, else we need to print fail. Here we check the condition, and a decision is made on the basis of condition satisfied. In this problem we use Python flow control.
>>> a = 50
>>> if (a>40) :
print("Student has passed")
>>> else:
print ("Student has failed")
Output:
Student has passed
Without control flow, the program is simply a list of statements that are sequentially executed. Using control flow, decision making allows us to run a particular block of code for a particular decision until the condition is satisfied. Here we’ll cover conditional statements (including if, elif, and else) and loop statements (including for and while, and the accompanying break, continue, and pass).
IF STATEMENT
Python includes if, else & a unique keyword is elif, a contraction of “else if ”. Else & elif are optional, also we can include as few or as many elif statements as we like. The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed. The if-else statement is similar to if statement except the fact that it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed. Nested if statements enable us to use if-else statements inside an outer if statement.
syntax :
----------
>>> if (condition):
statements
>>> a = 'diwas'
>>> if (a[1]=='i'):
print("correct")
Output:
correct
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Generally, four spaces are given to indent the statements which are a typical amount of indentation in python. Other programming languages often use curly-brackets for this purpose. See about python indentation here
EXAMPLE 01
Suppose we need to enter the number & check whether the number is odd or even. So lets start:
>>> a = int(input("enter a number"))
>>> if (a%2==0):
print(a, "is even")
>>> if (a%2!=0):
print (a," is odd")
Output:
enter a number6
6 is even
IF – ELSE STATEMENT
The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. If the condition is true, then the if-block is executed. Otherwise, the else-block is executed. Lets work on above example again:
syntax :
----------
>>> if (condition):
if-statements
>>> else:
else statements
>>> a = int(input("enter a number : "))
>>> if (a%2==0):
print(a, "is even")
>>> else: # we can also use elif instead of else
print (a," is odd")
Output:
enter a number : 5
5 is odd
EXAMPLE 02
Suppose we are given a percentage value of a student and we need to calculate the grade of the student. If he has got more than 80% then we need to print distinction. If he has got more than 60% then we need to print first division. Similarly, if he has obtained more than 40% then we need to print Just pass else we need to print fail.
We use elif in this case which is used to test the condition when the if condition fails. Now lets go through the solution:
>>> a = float(input("enter percentage : "))
>>> if (a>=80):
print(" Congratulations ! You have obtained distinction marks")
>>> elif (a>=60):
print(" Good ! You have obtained first division marks")
>>> elif (a>=40):
print(" Satisfactory result ! You have just pass marks")
>>> else:
print(" Sorry !! you are fail ! ")
Output:
enter percentage : 68.44
Good ! You have obtained first division marks
If we have only one statement to execute, we can put the statement on the same line of if, else, elif statements. Let’s take above example again:
>>> a = float(input("enter percentage : "))
>>> if (a>=80):print(" Congratulations ! You have obtained distinction marks")
>>> elif (a>=60):print(" Good ! You have obtained first division marks")
>>> elif (a>=40):print(" Satisfactory result ! You have just pass marks")
>>> else:print(" Sorry !! you are fail ! ")
Output:
enter percentage : 68.44
Good ! You have obtained first division marks
We can use logical operators like AND, OR in the test condition as shown in example below:
>>> a = int(input("enter marks of Maths : "))
>>> b = int(input("enter marks of Physics : "))
>>> if (a>=40 and b>=40) :
print(" you have passed on both subjects")
>>> else:
print("you have failed in at least one subject")
Output:
enter marks of Maths : 40
enter marks of Physics : 63
you have passed on both subjects
EXAMPLE 03 : LARGEST NUMBER OUT OF 3
>>> a = int(input("Enter integer a : "));
>>> b = int(input("Enter integer b : "));
>>> c = int(input("Enter integer c : "));
>>> if a>b and a>c:
print("a :",a," is the largest integer");
>>> elif b>a and b>c:
print("b :",b," is the largest integer");
>>> elif c>a and c>b:
print("c :",c," is the largest integer");
Output:
Enter integer a : 5
Enter integer b : 3
Enter integer c : 9
c : 9 is the largest integer
NESTED IF ELSE STATEMENT
In a nested if construct, we can have an if…elif…else construct inside another if…elif…else construct. We can use any number of these statements inside one another. Indentation is the only approach to find which block of statements belongs to which if else construct.
syntax:
--------------
if expression1:----------------------------------------------
statement(s) |
if expression2: ---------------------- |
statement(s) | |
if expression2:-------- | |
statement(s) | | |
elif expression3: | | |
statement(s) | | |
else: ----------------- | |
statement(s) | |
elif expression3: | |
statement(s) | |
else: -------------------------------- |
statement(s) |
else:--------------------------------------------------------
statement(s)
EXAMPLE 04: FIND NUMBER FROM 1 TO 5 FROM USER
>>> a = int (input("enter value from 1 to 5 : "))
>>> if (a>=1 and a<=5):
print ("number is between 1 & 5 ")
if (a%2==0):
print("Number is even")
if (a==2):
print ("number is 2")
elif (a ==4):
print ("number is 4")
elif (a%2!=0):
print("number is odd")
if(a==1):
print(" number is 1")
elif(a==3):
print("number is 3")
else:
print("number is 5")
>>> else:
print("Number not in range")
Output:
enter value from 1 to 5 : 4
number is between 1 & 5
Number is even
number is 4
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.
Wonderful goods from you, man. I have bear in mind your stuff previous to and you are
simply extremely magnificent. I actually like
what you have received right here, certainly
like what you’re saying and the way by which you say it.
You are making it entertaining and you continue
to care for to stay it smart. I cant wait to read much more from you.
This is actually a tremendous web site. adreamoftrains best web hosting 2020
I think this is among the most important information for me.
And i am glad reading your article. But want to remark on few general things, The
web site style is great, the articles is really excellent :
D. Good job, cheers