Python Decision Making

Decision making in anticipation of conditions occurring during the execution of the program and significant actions taken according to the conditions.

Decision structures calculate multiple expressions which produce TRUE or FALSE as a result. It is necessary to find which action is needed to take and which statement is right to execute if the result is TRUE or FALSE otherwise.

 The general form of a typical decision-making structure found in the maximum number of the programming languages is as follows−

Decision making statements in Python

Python programming language considers any non-zero and non-null values as TRUE, and if it is either zero or null, then it is considered as FALSE value.

Python programming language supports various types of decision-making statements which are as follows:

Sr.No. Statement & Description
1 if statements

An if statement contains a boolean expression followed by one or more statements.

2 if...else statements

An if statement can be followed by an optional else statement, and it is executed only if the boolean expression is FALSE.

3 nested if statements

you can use one if or else if statement inside another if or else if statement(s) according to your requirement.

Let's understand each decision in brief as follows: 

Single Statement Suites

If the suite of an if clause contains only a single line, it may go on the same line as the header statement.

Illustration of a one-line if clause −

#!/usr/bin/python

var = 100
if ( var == 100 ) : print "Value of expression is 100"
print "Good bye!"

When the above code is executed, it produces the following result −

Value of expression is 100
Good bye!