Ticker

6/recent/ticker-posts

Python: Control Flow

Flow control describes the order in which statements will be executed at runtime. 

Python: Control Flow

I. Conditional Statements

1) if

if condition : statement

 or

if condition :
 statement-1
 statement-2
 statement-3

If condition is true then statements will be executed.

Eg:

1) name=input("Enter Name:") 
2) if name=="durga" : 
3) print("Hello Durga Good Morning") 
4) print("How are you!!!") 
5) 
6) D:\Python_classes>py test.py 
7) Enter Name:durga 
8) Hello Durga Good Morning 
9) How are you!!! 
10) 
11) D:\Python_classes>py test.py 
12) Enter Name:Ravi 
13) How are you!!! 

2) if-else:

if condition :
 Action-1
else :
 Action-2

if condition is true then Action-1 will be executed otherwise Action-2 will be executed.

Eg:

1) name=input("Enter Name:") 
2) if name=="durga" : 
3) print("Hello Durga Good Morning") 
4) else: 
5) print("Hello Guest Good Moring") 
6) print("How are you!!!") 
7) 
8) D:\Python_classes>py test.py 
9) Enter Name:durga 
10) Hello Durga Good Morning 
11) How are you!!! 
12) 
13) D:\Python_classes>py test.py 
14) Enter Name:Ravi 
15) Hello Guest Good Moring 
16) How are you!!!

3) if-elif-else:

Syntax:

if condition1:
 Action-1
elif condition2:
 Action-2
elif condition3:
 Action-3
elif condition4:
 Action-4
 ...
else:
 Default Action

Based condition the corresponding action will be executed.

Eg:

1) brand=input("Enter Your Favourite Brand:") 
2) if brand=="RC" : 
3) print("It is childrens brand") 
4) elif brand=="KF": 
5) print("It is not that much kick") 
6) elif brand=="FO": 
7) print("Buy one get Free One") 
8) else : 
9) print("Other Brands are not recommended") 
10) 
11) 
12) D:\Python_classes>py test.py 
13) Enter Your Favourite Brand:RC 
14) It is childrens brand 
15) 
16) D:\Python_classes>py test.py 
17) Enter Your Favourite Brand:KF 
18) It is not that much kick 
19) 
20) D:\Python_classes>py test.py 
21) Enter Your Favourite Brand:KALYANI 
22) Other Brands are not recommended 

II. Iterative Statements

If we want to execute a group of statements multiple times then we should go for Iterative statements.

Python supports 2 types of iterative statements.

  1. for loop
  2. while loop

1) for loop:

If we want to execute some action for every element present in some sequence(it may be string or collection)then we should go for for loop.

Syntax:

for x in sequence :
 body

where sequence can be string or any collection.

Body will be executed for every element present in the sequence.

Eg 1: To print characters present in the given string

1) s="Sunny Leone" 
2) for x in s : 
3) print(x) 
4) 
5) Output 
6) S 
7) u 
8) n 
9) n 
10) y 
11) 
12) L 
13) e 
14) o 
15) n 
16) e

Eg 2: To print Hello 10 times

1) for x in range(10) : 
2) print("Hello") 

Eg 3: To display numbers from 0 to 10

1) for x in range(11) : 
2) print(x) 

Eg 4: To display odd numbers from 0 to 20

1) for x in range(21) : 
2) if (x%2!=0): 
3) print(x) 

2) while loop:

If we want to execute a group of statements iteratively until some condition false,then we should go for while loop.

Syntax:

 while condition :
 body

 Eg: To print numbers from 1 to 10 by using while loop

1) x=1 
2) while x <=10: 
3) print(x) 
4) x=x+1 

Eg: To display the sum of first n numbers

1) n=int(input("Enter number:")) 
2) sum=0 
3) i=1 
4) while i<=n: 
5) sum=sum+i 
6) i=i+1 
7) print("The sum of first",n,"numbers is :",sum) 

Infinite Loops:

1) i=0; 
2) while True : 
3) i=i+1; 
4) print("Hello",i) 

Nested Loops:

Sometimes we can take a loop inside another loop,which are also known as nested loops.

Eg: Write a program to dispaly *'s in Right angled triangled form

1) * 
2) * * 
3) * * * 
4) * * * * 
5) * * * * * 
6) * * * * * * 
7) * * * * * * * 
8) 
9) n = int(input("Enter number of rows:")) 
10) for i in range(1,n+1): 
11) for j in range(1,i+1): 
12) print("*",end=" ") 
13) print() 

III. Transfer Statements

1) break:

We can use break statement inside loops to break loop execution based on some condition.

Eg:

1) for i in range(10): 
2) if i==7: 
3) print("processing is enough..plz break") 
4) break 
5) print(i) 
6) 
7) D:\Python_classes>py test.py 
8) 0 
9) 1 
10) 2 
11) 3 
12) 4 
13) 5 
14) 6 
15) processing is enough..plz break 

2) continue:

We can use continue statement to skip current iteration and continue next iteration.

Eg 1: To print odd numbers in the range 0 to 9

1) for i in range(10): 
2) if i%2==0: 
3) continue 
4) print(i) 
5) 
6) D:\Python_classes>py test.py 
7) 1 
8) 3 
9) 5 
10) 7 
11) 9

loops with else block:

Inside loop execution,if break statement not executed ,then only else part will be executed.

else means loop without break

Eg:

1) cart=[10,20,30,40,50] 
2) for item in cart: 
3) if item>=500: 
4) print("We cannot process this order") 
5) break 
6) print(item) 
7) else: 
8) print("Congrats ...all items processed successfully") 
9) 
10)Output
11) 10 
12) 20 
13) 30 
14) 40 
15) 50 
16) Congrats ...all items processed successfully 

3) pass statement:

pass is a keyword in Python.

In our programming syntactically if block is required which won't do anything then we can define that empty block with pass keyword.

pass

 |- It is an empty statement
 |- It is null statement
 |- It won't do anything

Eg:

if True:
SyntaxError: unexpected EOF while parsing
if True: pass
==>valid

"Control Flow In Python W3schools"

"Control Flow In Python - Geeksforgeeks"

"Python Control Flow Practice"

"Control Flow Statements In Python Pdf"

"Python For Loop"

"Conditional Statements In Python"