Python IF statement is compound statement that allows it to embed multiple statements inside the IF block. The statement that can be included inside IF statement can be any Python statement like IF, WHILE, FOR or any other python statement.
Syntax
Python Syntax for IF statement is as below :
IF Statement |
Above figure shows the basic syntax of Python IF statement.
Following are set of rules for writing IF statement :
- If 'test1' condition return 'True' then code block inside If statement ('Statement1' and 'Statement2') will be executed.
- 'elif' code block is optional.
- 'elif' must be following by a condition. In the above syntax, 'test2' defines the condition for 'elif' and if 'test2' returns True then Statement3 and Statement4 will be executed.
- else block is option.
- No condition is required in else statement.
- If the test conditions for If and elif returns false then only Statement5 and Statement6 will be executed.
- There can be only one If and else block in the statement.
- There can be multiple 'elif' block in the statement.
IF-ELIF code basic examples
Following code shows the basic example of IF statement.
num = 50
if (num < 70)
print ("Needs improvement")
elif (num > 70):
print ("Congratulation !! You got distinction ")
Needs
improvement
In the above IF block, based on the 'num' value control will either go to If or elif code. Since num=50, so test statement of If loop will be True and output will be printed as "Needs Improvement"
num = 80
if (num < 70):
print ("Needs improvement")
elif (num > 70):
print ("Congratulation !! You got distinction ")
Congratulation !! You got distinction
In the above IF block, based on the 'num' value control will either go to If or elif code. Since num=80, so test statement of If loop will be False. Then the test statement of elif block will be evaluated. In this case it will retirn True and so print statement of elif will be executed.
Incorrect logic to be taken care in IF-ELIF
num = 30
if (num < 70):
print ("Needs improvement")
elif (num < 30): ##Dead code.
print ("Congratulation !! You got distinction ")
Needs Improvement
One should take care that the test condition for If and elif are mutually exclusive. In the above block elif code is dead code as whenever test condition for elif is True then test condition for If block will also be True and hence control will also go to If block and never to elif block.
IF-ELIF-Else block
x = "low"
if x == "low":
print ( "Average marks is 30")
elif x == "below average":
print ("Average marks is 70")
elif x == "Average":
print ("Average marks is 90")
else:
print ("Grading is not proper")
Average marks in 30
The above code shows the If clock with If, elif and else block. Based on the value of x, control will go to If block. Please note that if one write such code they need to make sure that condition in If and elif code are mutually exclusive othere there will be chance of introduction of redundant code.
Shortcut IF statement
num = 50
if (num < 70):
print ("Needs improvement")
else :
print ("Congratulation !! You got distinction ")
The above 4 lines of code of If-else can be written in one line as per following syntax.
num = 50
print ("Needs improvement") if (num < 70) else print ("Congratulation !! You got distinction ")
As evident from the above code, If block statement is written before If and else and corresponding print statement is written in the same order as the code above. In terms of functionality, both code are same but it terms of readability, second code block is better.
IF statement alternative
Python also provide dictionary based syntax as an alternative of If statement.
x = "Distinction"
y = {"Distinction" : 90,
"No distinction" : 50}[x]
90
The above statement returns the value by passing the dictionary key as an argument to dictionary in square bracket.
Conclusion
IF statement is one of powerful python statement that is used in almost all python application to take decisions. This is simple statement to understand and write the code.
0 comments:
Post a Comment