The most basic programming conditional structure is the standard
else statements in Python are used to check logical conditions of an expression.
else statements are the selection statements in Python. Programs use selection statements to choose among alternative courses of action. For example, the passing marks in an examination are
50 out of
100. To show if the student is passed look at the following pseudocode.
If student’s marks are greater than or equal to
“Student is passed”
In the above example, the
‘if’ part of the statement determines whether the condition “student’s marks are greater than or equal to 50” is
true, statement is executed and “Student is passed” is printed. If the condition is
false, the print statement is completely ignored by the Python interpreter.
Python uses the Boolean concept of
false while determining whether to execute the statements or not. If the condition is
true, the statements are executed. If it’s
false, the statements are not executed.
The above mentioned pseudo-code is written in Python code is as under:
StudentMarks = 50 if StudentMarks >= 50: print('Student is Passed')
Student is Passed
The above Python code completely corresponds closely to the pseudocode.
Here, one thing must be in your mind that the indentation of the second line is syntactically required. Because in Python, indentation is a necessary part of code construct and layout. Failure to use a colon
(:) sign at the end of the condition expression is a
syntax error. Be very mindful as your code with conditional statements.
Now let’s see what happens if student marks are less than 50.
StudentMarks = 40 if StudentMarks >= 50: print('Student is Passsed')
No output, because condition tested as false, and therefore, print statement is totally ignored by the Python interpreter.
Let’s do a little bit more syntax related discussion. You can write your conditional statement in one line if you want. It totally depends on the liking and disliking of the programmer. General rule by most of the programmers is, write single line statement if execution
if block is a single line. Whatever you adopt, but it is a good practice to stick with the language standards.
Look at the following code example.
StudentMarks = 60 if StudentMarks >= 50: print('Student is Passed.')
Student is Passed.
We can write our print statement just after the colon
(:) which separate the first line to the second one.
You may run across code in this format. However, I think it’s best to use the multiline format with indentation to improve readability, consistency and minimize errors.
if-else statement in Python Examples and Explanation
The next control structure we will discuss is the
if – else conditional statement.
if statement performs some action only when the condition is
true, otherwise the action is completely skipped or ignored by the Python compiler. The
if-else is a double selection statement which allows you to execute one or more statement when a condition is
true and executes one or more other statements when the condition is
false. Look at the following pseudo-code statement to understand the concept.
print “Student is passed”
print “Student is failed”
It is clear from the above statement that if the student marks are greater than or equal to 50, the program prints “Student is passed”. On the other hand, by the addition of else statement, we now capable of printing “Student is failed”, if the student marks are less than the passing marks i.e. 50. Now we convert the above pseudocode into Python programming instructions or code.
StudentMarks = 60 if StudentMarks >= 50: print('Student is passed ') else: print('Student is failed')
Student is passed
Now we take an example where condition becomes false and the second action is performed.
StudentMarks = 40 if StudentMarks >= 50: print('Student is passed ') else: print('Student is failed')
Student is failed
Here note that the statement of the
“else” part is also indented which is compulsory in Python and syntactically required. Missing out the colon
(:) after else is also a syntax
if-else Statement in Python with Examples and Explanation
Now we take an example where we test multiple conditions by using
if–else statement inside other
else–if statements to create nested
StudentMarks = 65 if StudentMarks >= 90: print('Student scored grade A ') else: if StudentMarks >= 80: print('Student scored grade B ') else: if StudentMarks >= 70: print('Student scored grade C ') else: if StudentMarks >= 60: print('Student scored grade D ') else: if StudentMarks >= 50: print('Student scored grade Passed ') else: if StudentMarks >= 40: print('Student scored grade Failed ')
Student scored grade D
if-else in Python:
If the variable
StudentMarks is greater than or equal to
90., the first four condition in the nested
if-else statements are true but only the first statement will execute. After the execution of that statement, the remaining part of the outermost
if-else statement is skipped or ignored.
if-else statements look nasty and complex to handle. But there is an easy way of
elif statements to do things as we are able to do in nested
if – else statement. In the Following section, we will discuss
elif Statement in Python with Examples and Explanation
Multiple statements can also be tested by
elif statements. The following code is the alternate to the above example of nested
if – statement.
StudentMarks = 65 if StudentMarks >= 90: print('Student scoured grade A ') elif StudentMarks >= 80: print('Student secured grade B ') elif StudentMarks >= 70: print('Student scoured grade C ') elif StudentMarks >= 60: print('Student secured grade D ') elif StudentMarks >= 50: print('Student scoured grade Passed ') elif StudentMarks >= 40: print('Student secured grade Failed ')
Student secured grade D
if-elif in Python:
The above code does the same thing as nested
if-else statement structure do. This code looks clean and easy to handle. It is a preferable method for all programmers to test any expression in Python. Some beginner level programmers use
if-else nested structure to test their logical expression but it is not the recommended way. It doesn’t follow the general programming standards of all levels as well. All comes to personal choice of a programmer but it should follow the standards and should be readable.
As you may have noticed, nested
if-else structure is not easy to read and if structure nesting level is deep then it is quite cumbersome and complex. A general thumb rule is, “follow the standard programming rules at the start of your programming career of any language and it will definitely fruit you at all level”. Happy programming.