0% found this document useful (0 votes)
3 views

Python Ch-3_Notes

This document covers the flow of control in Python programming, detailing sequential, selection, and repetition control structures. It explains various control statements such as if, if-else, nested if, elif, while loops, and for loops, along with examples and syntax. Additionally, it discusses the break and continue statements, highlighting their differences and usage in loop control.

Uploaded by

hetshihora247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Ch-3_Notes

This document covers the flow of control in Python programming, detailing sequential, selection, and repetition control structures. It explains various control statements such as if, if-else, nested if, elif, while loops, and for loops, along with examples and syntax. Additionally, it discusses the break and continue statements, highlighting their differences and usage in loop control.

Uploaded by

hetshihora247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Programming (UNIT-3) | 4311601

Unit-III: Flow of Control (Marks-16)


Introduction to Flow of Control
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.
Python has three types of control structures:
1. Sequential - default mode
2. Selection - used for decisions and branching
3. Repetition - used for looping, i.e., repeating a piece of code multiple times.
1. 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:
## This is a Sequential statement
a=20
b=10
c=a-b
print("Subtraction is : ",c)
Define selection statement. Explain any one in detail. WINTER – 2021, SUMMER-2022
2. Selection/Decision control statements
In Python, the selection statements are also known as Decision control statements or
branching statements.
The selection statement allows a program to test several conditions and execute instructions
based on which condition is true.
Some decision control statements are:
1. if
2. if-else
3. nested if
4. if-elif-else

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 1


Python Programming (UNIT-3) | 4311601
1. if – It help us to run a particular code, but only when a certain condition is met or satisfied.
A if only has one condition to check.

Syntax:
if expression:
Statements(s)
Example:
num = int(input("enter the number:"))
if num%2 == 0:
print("The Given number is an even number")
O/P:
enter the number: 10
The Given number is an even number
Explain if…else statement with suitable example SUMMER-2022
2. if-else
The if-else statement is similar to if statement except the fact that, it also provides the block
of the code for the false case of the condition to be checked.
If the condition provided in the if statement is false, then the else statement will be executed.

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 2


Python Programming (UNIT-3) | 4311601

Syntax:
if (condition):
#block of statements
else:
#another block of statements (else-block)
Example:
num = int(input("enter the number:"))
if (num%2 == 0):
print("The Given number is an even number")
else:
print("The Given number is odd number")
O/P:
enter the number: 10
The Given number is an even number
Explain nested if statements by giving syntax, flowchart and example. WINTER –
2021,WINTER-2022
3. Nested if
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.

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 3


Python Programming (UNIT-3) | 4311601
In a nested if construct, you can have an if...elif...else construct inside another if...elif...else
construct.
Syntax:
if (condition1):
statement(s)
if (condition2):
statement(s)
else:
statement(s)
else:
statement(s)
Example:
num = 15
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
O/P:
Positive number
4. elif statement
The elif statement enables us to check multiple conditions and execute the specific block of
statements depending upon the true condition among them.
We can have any number of elif statements in our program depending upon our need.
Syntax:
if (condition1):
# block of statements
elif (condition2):
# block of statements

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 4


Python Programming (UNIT-3) | 4311601
elif (condition3):
# block of statements
else:
# block of statements
Example:
marks = int(input("Enter the marks? "))
if (marks > 85 and marks <= 100):
print("Congrats ! you scored grade A ...")
elif (marks > 60 and marks <= 85):
print("You scored grade B + ...")
elif (marks > 40 and marks <= 60):
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
O/P:
Enter the marks 89
Congrats ! you scored grade A ...
Explain for loop by giving flowchart and example. WINTER – 2021, WINTER-2022
3. Repetition: - used for looping, i.e., repeating a piece of code multiple times.
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.
Write short note on while loop. SUMMER-2022
1. while loop
A while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is true.

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 5


Python Programming (UNIT-3) | 4311601
Syntax:
while (condition):
statement(s)

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.
Example
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
print "Good bye!"
While Loop with else statement
Python supports to have an else statement associated with a loop statement.
If the else statement is used with a while loop, the else statement is executed when the
condition becomes false.
Example:
count = 0
while count < 5:
print (count, " is less than 5")
count = count + 1
else:
print (count, " is not less than 5")
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 6
Python Programming (UNIT-3) | 4311601
O/P:
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
Explain for loop by giving flowchart and example. WINTER – 2021, WINTER-2022
2. for Loop
Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable
like String, Tuple, List, Set, or Dictionary.
The process of traversing a sequence is known as iteration.
Syntax:
for value in sequence:
# code block
Example:
list = ["geeks", "for", "geeks"]
for i in list:
print(i)
O/P:
geeks
for
geeks
Using else Statement with for Loop
Example:
list = ["geeks", "for", "geeks"]
for (i in list):
print(i)
else:
print(“exit”)
O/P:
geeks

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 7


Python Programming (UNIT-3) | 4311601
for
geeks
exit
for Loop with a range() function
To loop through a set of code a specified number of times, we can use the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
Syntax:
range(start, stop, step_size)
Example:
print(range(15))
print(list(range(15)))
print(list(range(4, 9)))
print(list(range(5, 25, 5)))
O/P:
range(0, 15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[4, 5, 6, 7, 8]
[5, 10, 15, 20]
Example:
fruits = ['banana', 'apple', 'mango']
for i in range(len(fruits):
print ('Current fruit :', fruits[i])
print "Good bye!"
O/P:
Current fruit: banana
Current fruit: apple
Current fruit: mango
Good bye!

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 8


Python Programming (UNIT-3) | 4311601
3. Nested Loop
Nested loops mean loops inside a loop. For example, while loop inside the for loop, for loop
inside the for loop, etc.
Syntax:
Outer_loop Expression:
Inner_loop Expression:
Statement inside inner_loop
Statement inside Outer_loop
Syntax for nested for loop
for value in sequence:
for value in sequence:
# code block 1
# code block 2
Syntax for nested for while
while expression:
while expression:
# code block 1
# code block 2
Example:
x = [1, 2]
y = [4, 5]
for i in x:
for j in y:
print(i, j)
O/P:
14
15
24
25

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 9


Python Programming (UNIT-3) | 4311601
Example:
i=1
while i<=2:
j=1
while j<=3:
print (i,"*",j,'=',i*j)
j+=1
i+=1
print ("\n")
O/P:
1*1=1
1*2=2
1*3=3

2*1=2
2*2=4
2*3=6
Explain break and continue statement with suitable example. SUMMER-2022,
WINTER-2022
break Statement
The break is a keyword in python which is used to bring the program control out of the loop.
The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks
the inner loop first and then proceeds to outer loops.
In other words, we can say that break is used to abort the current execution of the program
and the control goes to the next line after the loop.
The break is commonly used in the cases where we need to break the loop for a given
condition.
Syntax:
break;
Example:
my_str = "python"

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 10


Python Programming (UNIT-3) | 4311601
for char in my_str:
if char == 'o':
break
print(char)
O/P:
p
y
t
h
continue Statement
In Python, the continue keyword return control of the iteration to the beginning of the Python
for loop or Python while loop.
All remaining lines in the prevailing iteration of the loop are skipped by the continue
keyword, which returns execution to the beginning of the next iteration of the loop.
Example:
for i in range(10, 15):
# If iterator is equals to 13, loop will continue to the next iteration
if i== 13:
continue
# otherwise printing the value of iterator
print( i)
O/P:
10
11
12
14

Difference between Break and Continue


Sr. No Break Continue
1 It is used for the termination of all the It is used for the termination of the only
remaining iterations of the loop. current iteration of the loop.

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 11


Python Programming (UNIT-3) | 4311601
2 The line which is just after the loop The control will pass to the next iteration
will gain control of the program. of that current loop by skipping the
current iteration.
3 It performs the termination of the loop. It performs early execution of the next
loop by skipping the current one.
4 It stops the continuation of the loop. It stops the execution of the current
iteration.

**********
“Do not give up, the beginning is always hardest!”

INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 12

You might also like