L-3 If-Elif-else Conditional Statements and Loops in Python
L-3 If-Elif-else Conditional Statements and Loops in Python
1
Session Overview
Syntax
Example
Exercise
2
Decision making in Python
if-elif-else statements.
❖The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.
❖If the condition is true, then the if-block is executed. Otherwise, the else-block is
executed.
❖The elif statement works like an if-else-if ladder statement in C. It must be succeeded
by an if statement.
3
Decision making in Python (contd.)
if-elif-else statements.
Start
if False
Check
Condition 1
False
elif
True Check
Condition 2
Execute Statement 1
True
Execute Else
Execute Statement 2
Execute Statement 3
4
Syntax of Decision making in Python (contd.)
if-elif-else statements.
if expression1:
statement(s)
elif expression2:
statement(s)
else:
statement(s)
5
Example of Decision making in Python
if-elif-else statements.
6
Exercise 5
Time for Action
❑Write the code snippet to find maximum and minimum of three numbers.
❑Write the code snippet to swap the values of a and b without using any
third variable.
7
Loops in Python
Iterating elements multiple times in loops.
❑No need to write the same lines of code again and again.
8
Loops in Python (contd.)
Different types of loops.
9
Working Loops in Python
How to write Loops?
Start
Syntax of For Loop:
Initialize iteration variable for iterative_variable in sequence:
code statement(s)
If False/ After
specified number of
times iterations are Check
complete Repeat loop steps
loop Syntax of while Loop:
conditio
n while conditional_expression:
❖ A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition
is true.
while expression:
statement(s)
❖ Example
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
11
for Loop in Python
for loop.
❖The for statement in Python has the ability to iterate over the items of any sequence, such as
a list or a string.
❖Syntax:
12
Flow control statements in Python
Loop control statements
❑Break statement
❑Continue statement
❑Pass statement
13
Flow control statements in Python
Loop control statements
Type of
Working Syntax
Statement
❖Break statement halts the loop and brings the flow control out of the loop to the next
statement after the loop.
Break ❖In the case of nested loops, it halts and jumps out of the inner loop first and then break;
proceeds to outer loop.
❖It is used in the scenario where we need to break the loop for a given condition.
❖The continue statement brings the program flow control to the beginning of the loop.
❖It forces to skip the remaining lines of code inside the loop and jump to the next
Continue iteration. continue;
❖It is used for the scenario where it is required to skip some specific code for a specific
condition.
❖It is used to execute nothing i.e. the pass statement can be used to execute empty in
the scenario where we do not want to execute the code statements.
Pass pass
❖It just makes the control to pass by without executing any code. If we want to bypass
any code pass statement can be used.
14
Exercise 6
Time for Action
❑Write the code snippet to write the table for number in variable a.
❑Write code snippet to print Fibonacci series starting from number in variable a
and ending at number in variable c , i.e. Fibonacci series from 2 to 100.
❑Write code snippet to reverse a number say, 565 and check if it’s equal to the
original number.
15