SlideShare a Scribd company logo
CONDITIONAL STATEMENTS
and
CONTROL STATEMENTS
1
Prepared by –
AMAY JAISWAL
CS (4A) , 2100290120027
Python Programming
Kiet Group Of Institutions
CONDITIONAL STATEMENTS (Decision Making)
The basic decision statements in computer is
selection structure.
The decision is described to computer as a
conditional statement that can be answered
True or False.
Python language provide the following
conditional (Decision making) statements.
if statement
if...else statement
if...elif...else staement
Nested if..else statement
2
The if statement
The if statement is a decision making statement.
It is used to control the flow of execution of the
logically
statements and also used to test
whether the condition is true or false.
Syntax
if test expression:
statement(s)
3
Example program
i=int(input(“Enter the number:”))
If (i<=10):
print(“ condition is true”)
OUTPUT
Enter the number: 9
Condition is true
4
If … else statement
The if…else statement is called alternative
execution, in which there are two possibilities
and the condition determines wich one gets
executed.
Syntax
if test expression:
Body of if
else:
Body of else
5
Write a program to check if a number is Odd or Even
num = int(input(“Enter the number:”))
if (num % 2)== 0:
print (“Given number is Even”)
else:
print(“ Given number is Odd”)
OUTPUT
Enter the number: 9
Given number is Odd
6
elif Statements
elif – is a keyword used in Python in
replacement of else if to place another condition
in the program. This is called chained conditional.
Chained conditions allows than two possibilities
and need more than two branches.
SYNT
AX
if expression:
Body of if
elif expression:
Body of elif
else:
Body of else
7
Figure – elif conditionFlowchart
8
Example: largest among three numbers
a = int(input(“Enter 1st number:”))
b= int(input(“Enter 2nd number:”))
c= int(input(“Enter 3rd number:”))
if (a > b) and (a > c):
print("a is greater")
elif (b < a) and (b < c):
print(“bis greater")
else:
print(“c is greater")
OUTPUT
Enter 1st number:10
Enter 2nd number:25
Enter 3rd number:15
B is greater
9
10
Nested if … else Statements
We can write an entire if… else statement in
another if… else statement called nesting, and the
statement is called nested if.
 In a nested if construct, you can have an if … elif
… else construct inside an if … elif.. Else construct.
Syntax
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
.
11
• Example program
n = int(input(“Enter number:”))
If (n<=15):
if (n == 10):
print(‘play cricket’)
else:
print(‘play kabadi’)
Else:
print(‘Don’t play game’)
OUTPUT
Enter number : 10
12
Play cricket
CONTROL STATEMENT (Looping Statement)
Program statement are executed sequentially
one after another. In some situations, a block
of code needs of times.
These are repetitive program codes, the
computers have to perform to complete tasks.
The following are the loop structures available
in python.
while statement
for loop statement
Nested loop staement
13
While loop statement
A while loop statement in Python
programming language repeatedly executes a
target statement as long as a given condition is
true.
Syntax of while loop
while expression:
statement(s)
14
Write a program to find sum of number
num = int(input("Enter a number: "))
sum = 0
while(num > 0):
sum = sum+num
num = num-1
print("The sum is",sum)
OUTPUT
Enter a number: 10
The sum is 55
15
Using else statement with while loops
 Python supports t 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 false.
Program to illustrate the else in while loop
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print(“Outside loop")
OUTPUT
Inside loop
Inside loop
Inside loop
Outside loop
16
17
For loop statement
The for loop is another repetitive control
structure, and is used to execute a set of
instructions repeatedly, until the condition
becomes false.
The for loop in Python is used to iterate over a
sequence (list, tuple, string) or other iterable
objects. Iterating over a sequence is called
traversal.
Syntax
for val in sequence:
Body of for loop
For loop flow chart
Addition of number using for loop
numbers = [6, 5, 3, 8, 4, 2, 5, 4]
sum1 = 0
for val in numbers:
sum1 = sum1+val
print("The sum is", sum1)
OUTPUT
The sum is 37
18
for Loop and for Loop with else
EX-01:
genre = ['pop', 'rock', 'jazz']
for i in range(len(genre)):
print("I like", genre[i])
EX-02:
genre = ['pop', 'rock', 'jazz']
for i in range(len(genre)):
print("I like", genre[i])
else:
print("No items left.")
OUTPUT
I like pop
I like rock
I like jazz
OUTPUT
I like pop
I like rock
I like jazz
No items left.

More Related Content

Similar to loops.pdf

Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
Prakash Jayaraman
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Chandrakant Divate
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
DheeravathBinduMadha
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
maznabili
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
Panimalar Engineering College
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Chap 4 c++
Chap 4 c++Chap 4 c++
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
RohitSindhu10
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
AkashdeepBhattacharj1
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Soran University
 
Loops
LoopsLoops
Loops
Kamran
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
Intro C# Book
 

Similar to loops.pdf (20)

Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Loops
LoopsLoops
Loops
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
 

More from AmayJaiswal4

EMPLOYEE MANAGEMENT SYSTEM FINAL presentation
EMPLOYEE MANAGEMENT SYSTEM FINAL presentationEMPLOYEE MANAGEMENT SYSTEM FINAL presentation
EMPLOYEE MANAGEMENT SYSTEM FINAL presentation
AmayJaiswal4
 
agile development models IN SOFTWARE ENGINEERING
agile development models IN SOFTWARE ENGINEERINGagile development models IN SOFTWARE ENGINEERING
agile development models IN SOFTWARE ENGINEERING
AmayJaiswal4
 
Mobile application for diet recall presentatiob
Mobile application for diet recall presentatiobMobile application for diet recall presentatiob
Mobile application for diet recall presentatiob
AmayJaiswal4
 
economic inequality in India to consider.ppt
economic inequality in India to consider.ppteconomic inequality in India to consider.ppt
economic inequality in India to consider.ppt
AmayJaiswal4
 
diet recall
diet recalldiet recall
diet recall
AmayJaiswal4
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
deterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdfdeterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdf
AmayJaiswal4
 
4.1 Interview skills.ppt
4.1 Interview skills.ppt4.1 Interview skills.ppt
4.1 Interview skills.ppt
AmayJaiswal4
 
Mobile Appliction For Diet Recall.pptx
Mobile Appliction For Diet Recall.pptxMobile Appliction For Diet Recall.pptx
Mobile Appliction For Diet Recall.pptx
AmayJaiswal4
 

More from AmayJaiswal4 (9)

EMPLOYEE MANAGEMENT SYSTEM FINAL presentation
EMPLOYEE MANAGEMENT SYSTEM FINAL presentationEMPLOYEE MANAGEMENT SYSTEM FINAL presentation
EMPLOYEE MANAGEMENT SYSTEM FINAL presentation
 
agile development models IN SOFTWARE ENGINEERING
agile development models IN SOFTWARE ENGINEERINGagile development models IN SOFTWARE ENGINEERING
agile development models IN SOFTWARE ENGINEERING
 
Mobile application for diet recall presentatiob
Mobile application for diet recall presentatiobMobile application for diet recall presentatiob
Mobile application for diet recall presentatiob
 
economic inequality in India to consider.ppt
economic inequality in India to consider.ppteconomic inequality in India to consider.ppt
economic inequality in India to consider.ppt
 
diet recall
diet recalldiet recall
diet recall
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
deterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdfdeterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdf
 
4.1 Interview skills.ppt
4.1 Interview skills.ppt4.1 Interview skills.ppt
4.1 Interview skills.ppt
 
Mobile Appliction For Diet Recall.pptx
Mobile Appliction For Diet Recall.pptxMobile Appliction For Diet Recall.pptx
Mobile Appliction For Diet Recall.pptx
 

Recently uploaded

State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
kuntobimo2016
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
Lars Albertsson
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
Social Samosa
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
nyfuhyz
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
jitskeb
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
zsjl4mimo
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
74nqk8xf
 
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
74nqk8xf
 
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
g4dpvqap0
 

Recently uploaded (20)

State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
 
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
 
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
 

loops.pdf

  • 1. CONDITIONAL STATEMENTS and CONTROL STATEMENTS 1 Prepared by – AMAY JAISWAL CS (4A) , 2100290120027 Python Programming Kiet Group Of Institutions
  • 2. CONDITIONAL STATEMENTS (Decision Making) The basic decision statements in computer is selection structure. The decision is described to computer as a conditional statement that can be answered True or False. Python language provide the following conditional (Decision making) statements. if statement if...else statement if...elif...else staement Nested if..else statement 2
  • 3. The if statement The if statement is a decision making statement. It is used to control the flow of execution of the logically statements and also used to test whether the condition is true or false. Syntax if test expression: statement(s) 3
  • 4. Example program i=int(input(“Enter the number:”)) If (i<=10): print(“ condition is true”) OUTPUT Enter the number: 9 Condition is true 4
  • 5. If … else statement The if…else statement is called alternative execution, in which there are two possibilities and the condition determines wich one gets executed. Syntax if test expression: Body of if else: Body of else 5
  • 6. Write a program to check if a number is Odd or Even num = int(input(“Enter the number:”)) if (num % 2)== 0: print (“Given number is Even”) else: print(“ Given number is Odd”) OUTPUT Enter the number: 9 Given number is Odd 6
  • 7. elif Statements elif – is a keyword used in Python in replacement of else if to place another condition in the program. This is called chained conditional. Chained conditions allows than two possibilities and need more than two branches. SYNT AX if expression: Body of if elif expression: Body of elif else: Body of else 7
  • 8. Figure – elif conditionFlowchart 8
  • 9. Example: largest among three numbers a = int(input(“Enter 1st number:”)) b= int(input(“Enter 2nd number:”)) c= int(input(“Enter 3rd number:”)) if (a > b) and (a > c): print("a is greater") elif (b < a) and (b < c): print(“bis greater") else: print(“c is greater") OUTPUT Enter 1st number:10 Enter 2nd number:25 Enter 3rd number:15 B is greater 9
  • 10. 10 Nested if … else Statements We can write an entire if… else statement in another if… else statement called nesting, and the statement is called nested if.  In a nested if construct, you can have an if … elif … else construct inside an if … elif.. Else construct.
  • 11. Syntax if expression1: statement(s) if expression2: statement(s) elif expression3: statement(s) else: statement(s) . 11
  • 12. • Example program n = int(input(“Enter number:”)) If (n<=15): if (n == 10): print(‘play cricket’) else: print(‘play kabadi’) Else: print(‘Don’t play game’) OUTPUT Enter number : 10 12 Play cricket
  • 13. CONTROL STATEMENT (Looping Statement) Program statement are executed sequentially one after another. In some situations, a block of code needs of times. These are repetitive program codes, the computers have to perform to complete tasks. The following are the loop structures available in python. while statement for loop statement Nested loop staement 13
  • 14. While loop statement A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true. Syntax of while loop while expression: statement(s) 14
  • 15. Write a program to find sum of number num = int(input("Enter a number: ")) sum = 0 while(num > 0): sum = sum+num num = num-1 print("The sum is",sum) OUTPUT Enter a number: 10 The sum is 55 15
  • 16. Using else statement with while loops  Python supports t 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 false. Program to illustrate the else in while loop counter = 0 while counter < 3: print("Inside loop") counter = counter + 1 else: print(“Outside loop") OUTPUT Inside loop Inside loop Inside loop Outside loop 16
  • 17. 17 For loop statement The for loop is another repetitive control structure, and is used to execute a set of instructions repeatedly, until the condition becomes false. The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal. Syntax for val in sequence: Body of for loop
  • 18. For loop flow chart Addition of number using for loop numbers = [6, 5, 3, 8, 4, 2, 5, 4] sum1 = 0 for val in numbers: sum1 = sum1+val print("The sum is", sum1) OUTPUT The sum is 37 18
  • 19. for Loop and for Loop with else EX-01: genre = ['pop', 'rock', 'jazz'] for i in range(len(genre)): print("I like", genre[i]) EX-02: genre = ['pop', 'rock', 'jazz'] for i in range(len(genre)): print("I like", genre[i]) else: print("No items left.") OUTPUT I like pop I like rock I like jazz OUTPUT I like pop I like rock I like jazz No items left.