So far, We worked with python programming language and understood that, python interpreter will process our instruction one line after the other sequentially. There is no problem in sequential execution of an instruction. But, In some programs, there are some instructions where it has to execute based on specific condition along with other instructions. Unless the condition is not satisfied, It should not execute that special instruction or skip that instruction and execute other instructions. Condition evaluation can be achieved by using “if, elif, else” statement in python. Below contains syntax of if statement, example with program, one with sequential execution of instruction and other with conditional execution of instruction with explanation.
Syntax of condition evaluation in python
- If <expression that results in one of the bool value>:
- # statements to be executed whenever above expression results in true.
- Elif <another expression related to the same problem that results in one of the bool result>:
- # whenever expression in elif becomes true, this will be executed. In any manner, “if” statement becomes true, the “elif” part will not be evaluated. Elif is always optional. Requirements makes you to use this.
- Else:
- # else is always optional. If you need, you can include. This exist to execute whenever above expression present in if, elif results in false value.
Indentation
In the above syntax, after the line of “if”, “elif” and “else”, observe there is a tab included in the beginning of the line. This is called indentation. Which means, telling to python program by adding a space or tab in the next line, this line of statement belongs to the “if” or “elif” or “else” whatever there above that line.
Note:
You can include more than one line as the body of “if” or “elif” or “else”.
Example of a python program executing instructions sequentially
The python program present below takes input of marks of 5 subject and calculates the total marks and percentage. At last, it presents output of total marks and percentage for the user.
Program of sequential code execution
sub1=float(input("Enter first subject marks"))
sub2=float(input("Enter second subject marks"))
sub3=float(input("Enter third subject marks"))
sub4=float(input("Enter fourth subject marks"))
sub5=float(input("Enter fifth subject marks"))
total=sub1+sub2+sub3+sub4+sub5
percentage=total/500*100
print("Total marks of all subjects is %f" % total)
print("Percentage marks of all subject is %f" % percentage)
output of program (sequential code execution)
Enter first subject marks78
Enter second subject marks55
Enter third subject marks86
Enter fourth subject marks94
Enter fifth subject marks48
Total marks of all subjects is 361.000000
Percentage marks of all subject is 72.200000
As you read above program and its output, the instructions which are there in the program are executed sequentially. That means, one line after the other.
How “if”, “else” and “elif” works in evaluate conditions
Assume a similar program, which takes user input of 5 subjects, provides total marks and percentage, and also provides pass/fail that depends on all the subject marks.
For displaying Pass/Fail status, mainly there are 3 parts involved in this operation. Those are:
- Evaluation of all the subject marks (that is called checking condition of subject variable with one of comparison or logical operator, that results in True/False result in python). If the evaluation results in true, step 2 will be computed else it will skip the instruction there in step 2. Hence it may have chance of skipping instruction that’s there in step 2, we can say that the program will not always execute in sequential order.
- Depending on marks, displaying pass output for the user. Execution of this instruction is not possible if the evaluation of condition results in False value. If the expression results false, program directly moves from step 1 to step 3.
- Executes the statement that is there in the body of the “else” that displays fail result.
To understand this, let’s have a small program, that display’s pass or fail depends on all the subject marks of student.
Note:
Here, we are not evaluating percentage. We are evaluating subject marks. We are using comparison operator to compare the marks whether it is equal to 35 or greater than 35. Like this we are using “and” logical operator and combining all the subjects.
Program of condition evaluation
sub1=float(input("Enter first subject marks"))
sub2=float(input("Enter second subject marks"))
sub3=float(input("Enter third subject marks"))
sub4=float(input("Enter fourth subject marks"))
sub5=float(input("Enter fifth subject marks"))
total=sub1+sub2+sub3+sub4+sub5
percentage=total/500*100
if sub1>=35 and sub2>=35 and sub3>=35 and sub4>=35 and sub5>=35:
print("Student is pass");
else:
print("The student has failed");
print("Total marks of all subjects is %f" % total)
print("Percentage marks of all subject is %f" % percentage)
Output of program (Condition evaluation)
Enter first subject marks78
Enter second subject marks55
Enter third subject marks86
Enter fourth subject marks94
Enter fifth subject marks48
Student is pass
Total marks of all subjects is 361.000000
Percentage marks of all subject is 72.200000
Conclusion
- Conditional execution is a part of python language, whenever you want to evaluate some values, you will use the “if”, “elif” and “else” statements.
When conditions inside “if” and “elif” becomes true, the python program executes that part. Otherwise, It will execute the “else” part if developer specified inside the program. That’s why it prevents sequential execution of line of code. - “If”, “elif” and “else” are the keywords in python. Meaning is that, it have builtin functionality to do something and should not be used for variable names, function names or other things which we can specify as an identifiers.
- When you decide to use conditional evaluation in your program, “if” statement is mandatory. “elif” and “else” statements are optional. Based on your requirements, you can use it.
- When specifying the body of “if”, “elif” or “else” statements, the line should start from a single tab or a single space.
- You can specify one more conditional evaluation (“if”, “elif”, “else”) inside the body of “if”/”elif”/”else”. For specifying inside if statement in the body, you will include one tab as usual. But, for body of inside if statement, you will add one more tab to the existing tab so 1+1 will become 2 (two tabs).
- For inside if statements, we call as “nested if statements”.
- Including tab in python language is called as indentation.
Experiment to do
Take the student pass/fail program given above, and add the conditional evaluation to that for displaying “a” grade, “b” grade, “C” grade and so on.
Hint:
If average>=90:
# display a grade here…
Elif average >=80:
# display b grade here.
Elif average>=70:
…
Else:
# display fail.
Further more, try to find more problems like this and construct a suitable program.
If you found any mistakes in this article, Please contact us through the contact us page and help to correct ourselves.
If you think anything is missed in this concept, reach out to us through the same way mentioned above.
Great harsha fantastic
Good morning Harsha I am very happy to got this information I am searching from many days I want to learn this is it possible for me my qualification is masters in commerce I am working in Punjab National Bank I don’t have knowledge about coding but I have little bit technical knowledge
Yes, The intent of these articles is make you to learn the programming concepts. And for your information, these articles are written by keeping visually impaired people in mind.