Python Unit 2
Python Unit 2
A program’s control flow is the order in which the program’s code executes.
The control flow of a Python program is regulated by conditional statements, loops, and function calls.
• Sequential
• Selection
• Repetition
Sequential
Sequential statements are a set of statements whose execution process happens in a sequence. The problem with
sequential statements is that if the logic has broken in any one of the lines, then the complete source code execution
will break.
Example
a=10
b=20
c=a-b
Print(c)
The selection statement allows a program to test several conditions and execute instructions based on which condition is
true.Some decision control statements are:
• if
• if-else
• nested if
• if-elif-else
if Statement:
• An if statement is a selection control statement which is based on the valueof a given Boolean Expression.
Syntax:
if test_expression:
statement 1
.....
statement n
statement x
Example
X=10
If(x>0):
x=x+1
Print(x)
if-else –
The if-else statement evaluates the condition and will execute the statement block1 if if the test condition is True, but
if the condition is False, then the statement block2 of else block is executed.
Syntax:
if (test expression):
statement_block 1
else:
statement_block 2
statement x
Flowchart:
Example
n=5
If(n%2==0):
Print(“Even number”)
else:
Print(“Odd number”)
In the following code example, the else body will execute as 5 modulo 2 is not equal to 0.
Nested if Statements :
Nested if statement in python are the nesting of on if statement inside another if ststement with or without else statement
Syntax
If(test expression1):
Statement
Statement
Example
print(“rno=”,rno)
print(“name=”,name)
print(“mark1 =”,m1)
print(“mark3=”,m3)
if(m1>=40)and(m2>=40)and(m3>=40) :
total=m1+m2+m3
avg=total/3
print(“total=”,total)
print(“avg=”,avg)
if(avg>90):
print(“Grade A”)
elif(avg>80)and(avg>=90):
print(“Grade B”)
elif(avg>60)and(avg>=80):
print(“Grade C”)
else:
print(“Grade D”)
else:
print (“Result=fail”)
print(“Grade=Nil”)
if-elif-else Statement :
➢ The if-elif-else construct works in the same way as a usual if-else statement.
➢ A series of if and elif statements have a final else block, which is executed if none of the if or elif expressions is True.
Syntax:
statement block1
statement block2
. . . . . . . . . . . . . . ..
statement block N
else:
statement block X
Flowchart:
Example
a=10
b=15
c=20
Else
Iterative statements are decision control statements that are used to repeat the execution of a list of statements.
⚫ while loop
⚫ for loop.
while Loop :The While loop provides a mechanism to repeat one or more statements while a particular condition is TRUE.
Statement while (condition):
Statement block
Statement y
⚫ If the condition is TRUE, only then the statements will be executed otherwise if the condition is False, the control will
jump to statement y, that is the immediate statement outside the while loop block.
Flowchart:
Example
S=0
While(n>0):
r=n%10
s=s*10+r
n=n//10
for Loop:
For loop provides a mechanism to repeat a task until a particular condition is True. It is usually known as a determinate or
definite loop because the programmer knows exactly how many times the loop will repeat.
The for...in statement is a looping statement used in Python to iterate over a sequence of objects.
Syntax:
for loop_control_var in sequence:
statement block
FLOWCHART
o The range( ) function is a built-in function in Python that is used to iterate over a sequence of numbers.
Syntax
range(beg,end,[step])
⚫ The range( ) produces a sequence of numbers starting with beg (inclusive) and ending with one less than the number end.
⚫ The step argument is optional . By default, every number in the range is incremented by 1 but we can specify a different
increment using step. It can be both negative and positive, but not zero.
EXAMPLE
for i in range(1,5):
Print(i,end= “ “)
Output
1 2 3 4
Example
for I in range(1,10,2)
Print(I,end=” “)
Output
1 3 5 7 9
⚫ If range( ) function is given a single argument, it produces an object with values from 0 to argument-1. For example:
range(10) is equal to writing range(0, 10).
⚫ If range( ) is called with two arguments, it produces values from the first to the second. For example, range(0, 10) gives
0-9.
⚫ If range( ) has three arguments then the third argument specifies the interval of the sequence produced. In this case, the
third argument must be an integer.
Nested Loops :
Nested loop means inside a loop for example while loop inside for loop and for loop inside another for loop.
Example:
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Print( )
For j in range(1,i+1)
Print(j,end=” “)
Jump statement
➢ Break
➢ Continue
➢ pass
TheBreak Statement:
⚫ The break statement is used to terminate a loop execution and skip the next code
after the loop
syntax
Break
Example:Programtodemonstratethebreakstatement
It is used to terminate the current iteration of the loop and the control flow
of the program goes to the next iteration.
Syntax
Continue
Example
For I in range(1,10) :
If(I==5):
Continue
Print(I)
Print(“done”)
Output
12346789
Done
Example:
If the else statement is used with for and while in the else statement is executed when the
loop has completed iterating.
Statement
else:
statement
while(condition):
Statement
else:
statement