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

L-3 If-Elif-else Conditional Statements and Loops in Python

Uploaded by

omvati343
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

L-3 If-Elif-else Conditional Statements and Loops in Python

Uploaded by

omvati343
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Session-3

Decision Making in Python


•if-elif-else statements for Decision Making

1
Session Overview

Decision Making in Python

If-elif-else ladder flowchart

Syntax

Example

Exercise

2
Decision making in Python
if-elif-else statements.

❖Decision-making is the anticipation of conditions occurring during the execution of a


program and specified actions taken according to the conditions.

❖Decision structures evaluate multiple expressions, which produce TRUE or FALSE


as the outcome.

❖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.

The syntax of the if...elif...else construct may be:

if expression1:
statement(s)
elif expression2:
statement(s)
else:
statement(s)

5
Example of Decision making in Python
if-elif-else statements.

Example to enter an amount from user and calculate discount accordingly:

amount = int(input("Enter amount: "))


if amount<1000:
discount = amount*0.05
print ("Discount",discount)
elif amount<5000:
discount = amount*0.10
print ("Discount",discount)
else: discount = amount*0.15
print ("Discount",discount)
print ("Net payable:",amount-discount)

6
Exercise 5
Time for Action

❖Given three numbers: a=10, b=17 and c=20

❑Write the code snippet to find maximum and minimum of three numbers.

❑Write code snippet to check which of these is an even number.

❑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.

❖Loops are capable of repeating or iterating some specific lines of


code multiple times.

❖There are the following advantages of loops in Python.

❑Helps in code re-usability.

❑No need to write the same lines of code again and again.

❑Enables easy traversal of elements in data structures such as arrays, lists


etc.

8
Loops in Python (contd.)
Different types of loops.

There are the following 2 types of loops in Python.

Type of Loop Working


For loop should be used if the number of repetitions or iterations are
known in advance. It is used in the case where we need to execute code
for loop
statements until the given condition is satisfied. It is an entry controlled or
pre-tested loop.
The while loop should be used when number of iterations are not known in
while loop advance. In while loop the block of code statements is executed until the
specified condition is satisfied. It is an entry controlled or pre-tested loop.

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:

If true code statement(s)

Execute code statements

Exit loop and


Execute statements after loop
10
while Loop in Python
while loop.

❖ A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition
is true.

❖ Syntax of a while loop:

while expression:
statement(s)

❖ Example

count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1

print ("Good bye!")

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:

for iterating_var in sequence:


statements(s)

❖Example to traverse List sequence using for loop:

dept = [‘IT', ‘Sales’, ‘Marketing’, ‘Production’]


for idx in dept:
print (‘Department is :’ , idx)

12
Flow control statements in Python
Loop control statements

❖Flow Control Statements change the flow of execution of code in a


loop or deviate it from its regular sequence.

❖There are 3 types of flow 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

❖Given three numbers: a=2, b=5 and c=100

❑Write the code snippet to write the table for number in variable a.

❑Write code snippet to check which of these is a prime number.

❑Write the code snippet to factorial of number in variable b.

❑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

You might also like