Python Unit3 230321 202219
Python Unit3 230321 202219
Control Structure
Decision making is anticipation of conditions occurring while execution of the
program and specifying actions taken according to the conditions.
Python programming language provides following types of decision making statements. Click
the following links to check their detail.
If..else statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.
You can use one if or else if statement inside another if or else if statement(s).
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.
#!/usr/bin/python
var = 100
if ( var == 100 ) : print "Value of expression is 100"
print "Good bye!"
The else statement is an optional statement and there could be at most only one else statement
following if.
if expression:
statement(s)
else:
statement(s)
var1 = 100
if var1:
print var1
else:
print var1
var2 = 0
if var2:
print var2
else:
print var2
The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to
TRUE.
Similar to 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 elif statements following an if.
Syntax
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
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 if construct.
In a nested if construct, you can have an if...elif...else construct inside another if...elif...else construct.
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)
Nested if example
var = 100
if var == 150:
else:
print('number is zero')
else:
print('number is positive')
It terminates the current loop and resumes execution at the next statement, just like the traditional break statement in C.
The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while
and for loops.
If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.
# break Example
if letter == 'h':
break
It returns the control to the beginning of the while loop.. The continue statement rejects all the remaining statements in
the current iteration of the loop and moves the control back to the top of the loop.
The continue statement can be used in both while and for loops.
# Example
if letter == 'h':
continue
It is used when a statement is required syntactically but you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been
written yet
if letter == 'h':
pass
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.
Syntax
The syntax of a while loop in Python programming language is
while expression:
statement(s)
statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero
value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
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.
Here, key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop
body will be skipped and the first statement after the while loop will be executed.
while loop example
count = 0
count = count + 1
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.
for loop
It has the ability to iterate over the items of any sequence, such as a list or a string.
statements(s)
If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the
iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var,
and the statement(s) block is executed until the entire sequence is exhausted.
statements(s)
statements(s)
The syntax for a nested while loop statement in Python programming language is as follows −
while expression:
while expression:
statement(s)
statement(s)
example
i=2
j=2
if not(i%j): break
j=j+1
i=i+1
When above code executed the prime number till 100 will be printed
i=1
while(i <= 5):
j=1
while(j <= i):
j=j+1
print (i)
i=i+1
print("Hello World")
# Program to check if a number is prime or not
num = 29
flag = False
if num > 1:
if (num % i) == 0:
flag = True
break
if flag:
# Program to display the Fibonacci sequence up to n-th term
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1)
nth = n1 + n2
# update values
n1 = n2
# Python program to find the factorial of a number provided by the user.
num = 7
factorial = 1
if num < 0:
elif num == 0:
else:
factorial = factorial*i
exponent = 4
result *= base