Python Programming Lesson 03 - Control Structures in Python: 3.1 Decision Making
Python Programming Lesson 03 - Control Structures in Python: 3.1 Decision Making
⮚ Python programming language assumes any non-zero and non-null values as TRUE, and if it is either
zero or null, then it is assumed as FALSE value.
⮚ Python programming language provides following types of decision making statements.
Statement Description
3.2 If Statement
⮚ It 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.
⮚ If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if
statement is executed.
⮚ If boolean expression evaluates to FALSE, then the first set of code after the end of the if statement(s)
is executed.
⮚ 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!"
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
⮚ Core Python does not provide switch or case statements as in other languages, but we can use
if..elif...statements to simulate switch case as follows:
#!/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 var
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!"
3.6 LOOPS
⮚ In general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
⮚ There may be a situation when you need to execute a block of code several number of times.
⮚ Programming languages provide various control structures that allow for more complicated
execution paths.
⮚ A loop statement allows us to execute a statement or group of statements multiple times.
⮚ The following diagram illustrates a loop statement:
⮚ Python programming language provides following types of loops to handle looping requirements.
Loop Type Description
nested loops You can use one or more loop inside any another while, for
or do..while loop.
⮚ The block here, consisting of the print and increment statements, is executed repeatedly until
count is no longer less than 9.
⮚ With each iteration, the current value of the index count is displayed and then increased by 1.
⮚ It is better not try above example because it goes into infinite loop and you need to press CTRL+C keys
to exit.
3.11 For Loop
⮚ It has the ability to iterate over the items of any sequence, such as a list or a string.
⮚ Syntax:
⮚ Here, we took the assistance of the len() built-in function, which provides the total
number of elements in the tuple as well as the range() built-in function to give us the
actual sequence to iterate over.
⮚ When the above code is executed, it produces the following result:
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
⮚ The syntax for a nested while loop statement in Python programming language is as follows:
⮚ A final note on loop nesting is that you can put any type of loop inside of any other
type of loop. For example a for loop can be inside a while loop or vice versa.
3.14 Loop Control Statements
⮚ Loop control statements change execution from its normal sequence.
⮚ When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
⮚ Python supports the following control statements. Click the following links to check their detail.
Control Statement Description
continue statement Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
if letter == 'h':
break
if letter == 'h':
continue
if letter == 'h':
pass
Good bye!