Python Data Persistence – Decision Control
Python’s if keyword constructs a conditional statement. It evaluates a boolean expression and executes an indented block of statements following it. The block is initiated by putting the Y symbol after the expression. (Indentation was already explained in the previous chapter.) The following skeleton shows typical usage of if statement and conditional block in a Python script:
Example
#using if statement if expression==True: #block of one or more statements statement 1 statement 2 . . . . . . end of if block #rest of the statements
Let us try to understand this with the help of a simple example. Following script computes tax on employee’s salary @10% if it is greater than or equal to 50,000. No tax is deducted if the salary is less than 50,000.
Example
# tax1 . py salary= int (input("enter salary . . " ) ) tax=0 if salary>=50000: #tax @10% tax=salary*10/100 net_sal=salary-tax print ("salary={ } tax= { } net payable= { }". format (salary, tax , net_sal) )
Save the above script as ‘tax1.py’ and run it from the command prompt as shown below (figure 2.2):
C:\python37>python tax1.py
enter salary..75000
Salary=75000 tax=7500.0 net payable=67500.0
C:\python37>python tax1.py
enter salary..30000
Salary=30000 tax=0 net payable=30000
C:\python3 7 >
Python also provides else keywords. If there is another block of statements to be executed when the expression in if statement happens to be no True (False), it appears as:
Example
#using if statement if expression==True: #if block statement 1 statement 2 . . . . . . #end of if block else: #else block statement 1 statement 2 . . . . . . #end of else block #rest of the statements
To show the use of else block, let us modify the previous example of computation of tax, assuming that tax is calculated 5% for salary<50000
Example
#tax2 . py salary=int (input("enter salary . . " ) ) tax = 0 if salary>=50000: # tax@10% tax=salary*10/100 else: #tax@5% tax=salary*5/100 net_sal=salary-tax print ("salary={ } tax={ } net payable = { }". format (salary, tax, net_sal ) )
Two sample executions of the above script (tax2.py) are as shown below
C:\python37>python tax2.py enter salary..60000 Salary=60000 tax=6000.0 net payable=54000.0 C:\python37 >python tax2.py enter salary..20000 Salary=20000 tax=1000.0 net payable;=19000.0 C:\python3 7 >
Python also has elif keyword. Before using it in a Python script, let us modify the above tax calculation program to include few more tax slabs. The tax rate for salary above 50,000 is the same @10%. However, 5% tax is imposed for salary between 25,001 – 50,000. An employee with a salary between 10,001 – 25000 pays 2% tax, and anything below it requires no tax to be deducted. The script looks like as:
Example
#tax3 . py salary=int ( input ( " enter salary . . " ) ) tax=0 if salary>=50000: #tax @10% tax=salary*10/100 else: if salary>25000 : #tax @5% tax=salary*5/100 else: if salary>10000: #tax @2% tax=salary*2/100 else: #no tax print ("No tax applicable") net_sal=salary-tax print ("Salary={ } tax={ } net payable={ }". format(salary, tax, net_sal))
Here’s the output showing different tax slabs (figure 2.4)
C:\python37>python tax3.py enter salary..60000 Salary=60000 tax=6000.0 net payable=54000.0 C:\python37>python tax3.py enter salary..40000 Salary=40000 tax=2000.0 net payable = 38000.0 C:\python37>python tax3.py enter salary..18000 Salary=18000 tax=360.0 net payable=17640.0 C:\python37>python tax3.py enter salary..5000 No tax applicable Salary=5000 tax=0 net payable=5000 E:\python37>
While the output is satisfactory as per the expectations, the code (tax3. py) looks a little clumsy because of the increasing indent level of successive if blocks which fall in else part of the previous if statement. This is where the use of elif keyword provides a more elegant way to avoid these indentations and combine empty else with subsequent if block. The general syntax of if- elif- else usage is as shown below:
Example
#using elif statement if exprl==True: #first block . . . #end of the first block elif expr2==True: #second block executes if expr1 is False and expr2 is true. . . . #end of the second block elif exp3==True: #third block executes if expr2 is false and expr3 is true . . . #end of the third block else: #else block, executes if all preceding expressions are false . . . end of else block #rest of the statements
In this structure, there is one if block, followed by one or more elif blocks and one else block at the end. Each subsequent elif is evaluated if the previous expression fails. The 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 another version of tax3.py which uses elif blocks.
Example
#tax4.py salary=int(input("enter salary..")) tax=0 if salary>=50000: #tax @10% tax=salary*10/100 elif salary>25000: #tax @5% tax=salary*5/100 elif salary>10000: #tax @2% tax=salary*2/100 else : #no tax print ("No tax applicable") net_sal=salary-tax print ("Salary={ } tax={ } net payable={ }". format(salary, tax, net_sal))
The output of course will be similar to before.