Decision making of Python - PYTHON PROGRAMMING (2010)

PYTHON PROGRAMMING (2010)

Decision making of Python

· Decision making is one of the important aspects of any programming languages.

· Similarly it goes with the python the various structures are as follows.

Image

Flow diagram

· In this programming language it assumes non-zero and non-null value as true.

· But if it is either zero or null it is assumed as false.

Following are the three decision making statement.

· If statement.

· If else statement.

· Nested if statement.

If statement

· Contains logical expression.

· Utilizes these expressions and converts them into a result after comparison.

Syntax

If expression:

Statement

Image

If flow table

Program

#!/usr/bin/python

var1 = 100

if var1:

print "1 - Got a true expression value"

print var1

var2 = 0

if var2:

print "2 - Got a true expression value"

print var2

print "Good bye!"

Output

1 - Got a true expression value

100

Good bye!

If else statement

· If and else are combined.

· Else provide an option to the if statement.

Syntax

if expression:

statement(s)

else:

statement(s)

Image

If else flow table

Program

#!/usr/bin/python

var1 = 100

if var1:

print "1 - Got a true expression value"

print var1

else:

print "1 - Got a false expression value"

print var1

var2 = 0

if var2:

print "2 - Got a true expression value"

print var2

else:

print "2 - Got a false expression value"

print var2

print "Good bye!".

Output

1 - Got a true expression value

100

2 - Got a false expression value

0

Good bye!

Nested if statement

· It is having various conditions one by one in sequence.

· It’s used to resolve one condition after one.

Syntax

if expression1:

statement(s)

if expression2:

statement(s)

elif expression3:

statement(s)

else

statement(s)

elif expression4:

statement(s)

else:

statement(s).

Program

#!/usr/bin/python

var = 100

if var < 200:

print "Expression value is less than 200"

if var == 150:

print "Which is 150"

elif var == 100:

print "Which is 100"

elif var == 50:

print "Which is 50"

elif var < 50:

print "Expression value is less than 50"

print "Could not find true expression"

print "Good by!".

Output

Expression value is less than 200

Which is 100

Good bye!

Image





All materials on the site are licensed Creative Commons Attribution-Sharealike 3.0 Unported CC BY-SA 3.0 & GNU Free Documentation License (GFDL)

If you are the copyright holder of any material contained on our site and intend to remove it, please contact our site administrator for approval.

© 2016-2026 All site design rights belong to S.Y.A.