InterviewSolution
| 1. |
In what circumstances is the ‘elif’ keyword recommended to be used? |
|
Answer» Python uses if keyword to implement decision control. Python’s syntax for executing block conditionally is as below: if expr==True: stmt1 stmt2 .. stmtNAny Boolean expression evaluating to True or False appears after if keyword. USE : symbol and press Enter after the expression to start a block with increased indent. One or more statements written with the same level of indent will be executed if the Boolean expression evaluates to True. To end the block, press backspace to de-dent. Subsequent statements after the block will be executed if the expression is false or after the block if expression is true. Along with if statement, else clause can also be optionally USED to define alternate block of statements to be executed if the Boolean expression (in if statement) is not true. Following code skeleton shows how else block is used. if expr==True: stmt1 stmt2 .. else: stmt3 stmt4 .. stmtNThere may be situations where cascaded or nested conditional statements are required to be used as shown in the following skeleton: if expr1==True: stmt1 stmt2 .. else: if expr2==True: stmt3 stmt4 ... else: if expr3==True: stmt5 stmt6 .. stmtNAs you can see, the indentation level of each subsequent block goes on increasing because if block starts after empty else. To avoid these clumsy indentations, we can combine empty else and subsequent if by elif keyword. General syntax of if – elif – else usage is as below: if expr1==True: #Block To Be Executed If expr1 is True elif expr2==True: #Block To Be Executed If expr1 is false and expr2 is true elif expr3==True: #Block To Be Executed If expr2 is false and expr3 is true elif expr4==True: #Block To Be Executed If expr3 is false and expr4 is true else: #Block To Be Executed If all preceding expressions falseIn this code, one if block, FOLLOWED by one or more elif blocks and one else block at the end will appear. Boolean expression in front of elif is evaluated if previous expression fails. Last else block is run only when all previous expressions turn out to be not true. Importantly all blocks have the same level of indentation. Here is a simple example to demonstrate the use of elif keyword. The following program computes discount at different rates if the amount is in different slabs. price=int(input("enter price")) qty=int(input("enter quantity")) amt=price*qty if amt>10000: PRINT ("10% discount applicable") discount=amt*10/100 amt=amt-discount elif amt>5000: print ("5% discount applicable") discount=amt*5/100 amt=amt-discount elif amt>2000: print ("2% discount applicable") discount=amt*2/100 amt=amt-discount elif amt>1000: print ("1% discount applicable") discount=amt/100 amt=amt-discount else: print ("no discount applicable") print ("amount payable:",amt)Output: enter price1000 enter quantity11 10% discount applicable amount payable: 9900.0 ======================== enter price1000 enter quantity6 5% discount applicable amount payable: 5700.0 ======================== enter price1000 enter quantity4 2% discount applicable amount payable: 3920.0 ======================== enter price500 enter quantity3 1% discount applicable amount payable: 1485.0 ======================== enter price250 enter quantity3 no discount applicable amount payable: 750 |
|