codescracker
python

Python Decision Making Statements



All those statements in Python, that are used to decide the order of execution of other specified statement(s) in the program. That is, decision making statements in Python, helps in controlling the program based on required or given condition.

Types of Decision Making Statements

There are basically 4 types of decision making statements. Here are the list of all those four decision making statements:

All these decision making statements are described with examples, in detail, in one separate tutorial. Therefore, all the links goes to a single tutorial page.

Python Decision Making Examples

As all the decision making statements are described in separate tutorial. Therefore, here we only see the basic example program based on those statements. Let's create a simple example, demonstrates decision making statement in Python:

print("Enter a Number: ")
val = int(input())

if val>0:
    print("You've entered a number, greater than 0.")

The initial output produced by above program, is shown in the snapshot given below:

python decision making

Now enter a number say 14 and press ENTER key to see the output as shown in the snapshot given below:

decision making statements in python

In above program, the first statement:

print("Enter a Number: ")

prints a message, Enter a Number: on output screen. And the second statement, that is:

val = int(input())

receives the entered number using input(), and the value gets converted into integer type using int(), finally the value gets initialized to val variable. So val=14, if user enters 14 as input. Now the condition of if gets evaluated. That is, the condition, val>0 or 14>0 evaluates to be True, therefore program flow goes inside the if's body. And the statement:

print("You've entered a number, greater than 0.")

prints You've entered a number, greater than 0. on output screen. That's it. Now let's modify the above program, uses two decision making statements:

print("Enter a Number: ")
val = int(input())

if val>0:
    print("You've entered a number, greater than 0.")
else:
    print("You've entered a number, equals to 0 or less.")

Here is its sample run with user input -20:

decision making example program

In above program, if the condition val>0 evaluates to be True, then program flow goes to if's body, otherwise goes to else's body. Now here is the third program created, that uses three decision making statements:

print("Enter a Number: ")
val = int(input())

if val>0:
    print("You've entered a number, greater than 0.")
elif val==0:
    print("You've entered 0.")
else:
    print("You've entered a number, less than 0.")

Here is its sample run with user input 0:

python decision making statements

In above program, first the condition of if gets evaluated, if the condition evaluates to be False, then the condition of elif gets evaluated.

If the condition of if evaluates to be True, then program flow goes to its body. Otherwise, if the condition of elif evaluates to be True, then program flow goes in its body. And if both the condition evaluates to be False, then program flow goes to else's body.

Note - You can provide as many condition as possible, using multiple elif, lied between if and else.

Here is the last program, that uses all the decision making statements, including nested if:

print("Enter a Number: ", end="")
val = int(input())

if val>0:
    if val<10000:
        print("The number is less than 10000.")
    else:
        print("The number is a big number.")
elif val==0:
    print("You've entered 0.")
else:
    print("The number is a negative number.")
    if val>-100:
        print("The number is greater than -100.")

Here is its sample output with user input -40:

decision making statement program python

More Examples

Python Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker