Conditional constructs are used to incorporate decision making into programs. The result of this decision making determines the sequence in which a program will execute instructions. You can control the flow of a program by using conditional constructs.
This tutorial will discuss the programming conditional constructs available in Python, such as if, if...else, elif, and nested if.
The if statement:
The if statement of Python is similar to that of other languages. The if statement contains a logical expression using which data is compared, and a decision is made based on the result of the comparison.
The syntax of the if statement is:
if expression: statement(s) |
Here if statement, condition is evaluated first. If condition is true that is, if its value is nonzero then the statement(s) block are executed. Otherwise, the next statement following the statement(s) block is executed.
Note: In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
Example:
#!/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!" |
This will produce following result:
1 - Got a true expression value 100 Good bye! |
The else Statement:
An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value.
The else statement is an optional statement and there could be at most only oneelse statement following if .
The syntax of the if...else statement is:
if expression: statement(s) else: statement(s) |
Example:
#!/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!" |
This will produce following result:
1 - Got a true expression value 100 2 - Got a false expression value 0 Good bye! |
The elif Statement
The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true.
Like the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elifstatements following an if.
The syntax of the if...elif statement is:
if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s) |
Note: Python does not currently support switch or case statements as in other languages.
Example:
#!/usr/bin/python var = 100 if var == 200: print "1 - Got a true expression value" print var elif var == 150: print "2 - Got a true expression value" print var2 elif var == 100: print "3 - Got a true expression value" print var else: print "4 - Got a false expression value" print var print "Good bye!" |
This will produce following result:
3 - Got a true expression value 100 Good bye! |
The Nested if...elif...else Construct
There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested ifconstruct.
In a nested if construct, you can have an if...elif...else construct inside anotherif...elif...else construct.
The syntax of the nested if...elif...else construct may be:
if expression1: statement(s) if expression2: statement(s) elif expression3: statement(s) else statement(s) elif expression4: statement(s) else: statement(s) |
Example:
#!/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" else: print "Could not find true expression" print "Good bye!" |
This will produce following result:
Expression value is less than 200 Which is 100 Good bye! |
Single Statement Suites:
If the suite of an if clause consists only of a single line, it may go on the same line as the header statement:
Here is an example of a one-line if clause:
if ( expression == 1 ) : print "Value of expression is 1" |
No comments:
Post a Comment