Python Programming - Decision Making

Python Programming – Decision Making

Decision-making is done with a conditional statement which allows selecting one or another execution path in a program. It causes to execution of a particular block of statements after the evaluation of a given conditional expression. In other words, appropriate action is preceded by the evaluation of a test condition to either true or false. Table 4.1. depicts a list of decision-making control structures supported by Python language. Now, we will learn each of the control structures in detail.

Statement

Description

if statementAn if statement consists of an expression (which results in either true or false) followed by a block of one or more statements.
if..else statementAn if statement followed by an else statement, which executes when the boolean expression turns false.
if…elif. ..else statementAn if statement can be followed by an optional elif and else statements, where elif is accompanied with a test condition similar to if statement.
nested if statementIt contains if statement inside another if or else if statement(s).

Note:
Python programming language assumes any non¬zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.

Python if Statement

The Python if statement can execute either a simple or compound statement depending on the result of the expression. The syntax of Python if statement is given as follows:

if test condition:
statement(s)

Here, the program evaluates the test condition (expression) and will execute statement(s) only if the test expression evaluates to True. If the test expression is False, the statement(s) does not get executed. As we know that, in Python, indentation is necessary. The beginning of a block is always marked with the indented statement and the closing of a block is marked by the unindented statement. Therefore, the body of the if statement is indicated by the indentation. The body starts with an indentation and the first unindented line marks the end. Python interprets non-zero values as True. None and 0 are interpreted as False. The flow diagram of the if statement is given in Fig. 4.1.

Python Programming - Decision Making chapter 3 img 1

Code: 4.1 Illustration of if statement to determine whether a number is even.

# This program determines whether a number is even using if statement

number=input(‘Enter a number:’)
number=int(number)
if number%2==0:
print(‘Number is even’)
print(‘out of if block’)

Output 1:
Enter a number: 12
Number is even
Out of if blockOutput 2:
Enter a number: 7
Out of if block

The programming example of the if statement “to determine whether a number is even” is given in Code 4.1. In this code, number%2 == 0 is the test expression. The body of if is executed only if this evaluates to True. When a user enters 12, the test expression is true then the indented statement under if statement gets executed. When a user enters 7, the test expression is false then the indented statement under if is skipped. The unindented print() statement falls outside of the if block. Hence, it is executed regardless of the test expression.

We can see this in our two outputs above. Another example of if a statement is given in Code 4.2, the program code determines whether a number is positive. It is explicable from the code that if the user inputs the value of the number variable 5 then the “It is a positive number” message will be displayed along with the “out of if block” message. However, when the user inputs a – 5 then if the statement evaluates to false, and “Out of if block” message is displayed bypassing the indented statement associated with the if statement.

Code 4.2. Illustration of if statement to determine whether a number is positive.

# This program determines whether a number is positive using if statement.
number=input(‘Enter a number:’)
number=int(number)
if number>0:
print(‘It is a positive number’)
print(‘Out of if block’)
Output 1:
Enter a number: 5
It is a positive number
Out of if blockOutput 2:
Enter a number: -5
Out of if block

Python if-else Statement

The if statement executes a simple or compound statement when the test expression provided in the if statement evaluates to true. However, as in the above Code 4.1 and Code 4.2, we see that what if the test expression evaluates to false. Python language provides the solution for this by the if-else statement. An else statement contains the block of code that gets executed if the conditional test expression in the if statement evaluates to 0 or a false value. The else statement is an optional statement and there could be at most only one else statement following if. The syntax of if…else statement is given as under

if test condition:
statement(s)
else:
statement(s)

As shown in the syntax, initially test condition associated with if is evaluated, if it evaluates to true then statements following if block is executed. If the test condition evaluates to false then the statements associated with else block are executed. The flow diagram of if…else construct is represented in Fig. 4.2.

Python Programming - Decision Making chapter 3 img 2

The programming example of if…else construct is given in Code 4.3. The program determines whether a number is even or odd. In the previous example, we see that when the test expression number%2==0 evaluates to false then the “Out of if block” statement is executed. However, by using if.. .else construct the output is displayed as “Number is odd” for a false test condition. Similarly, in the case of another program, where it is determined that whether a number is positive or not. For a false condition, the else statement block is executed, which displays “It is a negative number”, as shown in Code 4.4.

Code: 4.3. To determine whether a number is even or odd using if…else construct.

#This program determines whether a number is even or odd using if…else construct

number=input(‘Enter a number:’)
number=int(number)
if number%2==0:
print(‘Number is even’)
else:
print(‘Number is odd’)

Output 1:
Enter a number: 12
Number is evenOutput 2:
Enter a number: 7
Number is odd

Code 4.4. To determine whether a number is positive or negative using if…else construct.

# This program determines whether a number is positive or negative using if…else statement

number=input(‘Enter a number:’)
number=int(number)
if number>0:
print(‘It is a positive number’)
else:
print(‘It is a negative number’)

Output 1:
Enter a number: 7 It is a positive number
Output 2:
Enter a number: -9 It is a negative number

Python if…elif…else

The elif statement allows you to check multiple expressions for true and execute a block of code as soon as one of the conditions evaluates to true. It is similar to if…else if…else construct of C, C++, and Java. Similar to the else, the elif statement is optional. However, unlike else, there can be an arbitrary number of elif statements following an if. Like, if statement elif is associated with a test condition. The syntax of if…elif…else is given as under:

if test condition:
statement(s)
statement(s)
else
statement(s)

As shown in the syntax, If the test condition for if evaluates to false, it checks the condition of the next elif block and so on. If all the conditions are false, a block of else is executed. Only one of the several if…elif…else blocks is executed according to the condition. The flow diagram of if…elif… else is given in Fig. 4.3.

Code:4.5. To determine whether a number is positive, negative or zero using if.. .elif.. .else construct

# This program determines whether a number is positive, negative or zero
# by using if…elif…else statementnumber=input(‘Enter a number:’)
number=int(number)
if number>0:
print(‘Number is positive’)
elif number==0:
print(‘Number is zero’)
else:
print(‘Number is negative’)
Output 1

Enter a number: 5
Number is positive

Output 2

Enter a number: -5
Number is negative

Output 3

Enter a number: 0
Number is zero

Python Nested if Statements

There may be a situation when the programmer wants to check for another condition after a condition resolves to true. In such a situation, the nested if construct can be used. In a nested, if construct, we can have an if…elif…else construct inside another if…elif…else construct and so on. The syntax of nested if construct is given as follows:

if test condition:
statement(s) if test condition:
statement(s)
elif test condition:
statement(s)
else
statement(s)
elif test condition:
statement(s)
else
statement(s)

The program representing the significance of nested if is given in Code:4.6. When the user inputs the value of number=5 then the condition number>=0 becomes true and control goes inside the nested if block, where it checks for number=0 test condition, which becomes false. Then else part associated with the nested if is executed and the result is displayed as “Positive number”. Similarly, in the second output when the user inputs number=0 the “Zero” message is displayed and in the third output, we see that by entering a negative number the outer if the condition becomes false and it’s associated else part is executed, which displays “Negative number”.

Code:4.6. To determine whether a number is positive, negative or zero using nested if statements.

# This program input a number and determines whether it is positive, negative or zero using
# nested if statementsnumber= input(‘Enter a number:’)
number=int(number)
if number>=0:
if number == 0:
print(‘Zero’)
else:
               Print (‘positive number’)

Else:

Print (‘Negative number’)

Output 1
Enter a number: 5
Positive number
Output 2
Enter a number: 0
Zero
Output 3
Enter a number: -10
Negative number

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *