Tuesday, April 3, 2018

Python Decision Making

Python - Decision Making


Decision Making in Python Decision making involves the anticipation of the conditions that occur during the execution of a given program and specifying the actions based on the conditions. Multiple expressions will be evaluated by the decision structures and they produce a Boolean value that is either TRUE or a FALSE. And the user will need to determine the action to be taken and the statements to be executed basing on the outcome.


The decision-making structure that is found in all major programming languages is given below.

decision making statement


In Python, any non-null and non-zero values will be assumed as TRUE. If the value obtained is either null or zero, the Python assumes it as a FALSE.

The decision making statements that are provided by the Python programming language are stated below.

These are used in order to check if the given condition is satisfied or not.

Ex1:- 

 a = 1   

if a > 5: 

print "This shouldn't happen." else:

 print "This should happen." O/p: This should happen 

Ex2:-

 z = 4 

if z > 70: 

print "Very Wrong" elif z < 7: 

print "This is normal" O/p: This is normal 

             It is to be noted that the else if is shortened to elif in python. We can use multiple elif’s but only one else statement. End of every sentence should have a colon. Even indentation sometimes helps to differentiate the statements that are in the conditional statements or not.

          Single Statement Suites The suit of given if the statement can go on the same line of the header statement in cases where the if clause has a single line. For you to get a better understanding of single line suites, go through the following example.

Example:

one-line if clause− 

#!/usr/bin/python

var = 100

if ( var == 100 ) : print "The value of the given expression is 50" print "Good bye!"

The above code gives the following result− The value of the given expression is 50 Good bye!

0 comments:

Please comment and follow this site blog inbox