SlideShare a Scribd company logo
1 of 5
The if statement executes only when specified condition is true. We can pass any valid
expression into the if parentheses.
There are various types of if statements in Python.
o if statement
o if-else statement
o nested if statement
Syntax:
if(condition):
statements
Ex:
a=10
if a==10:
print "Welcome to Durgasoft"
If Else:
The If statement is used to test specified condition and if the condition is true, if block
executes, otherwise else block executes.
The else statement executes when the if statement is false.
if(condition): False
statements
else: True
statements
Ex:
year=2000
if year%4==0:
print "Year is Leap"
else:
print "Year is not Leap"
Program To Find Even or Odd:
x=int(input(print("enter any number:")))
r=x%2
if r==0:
print("Even Number")
else:
print("Odd Number")
Nested If Else:
In python, we can use nested If Else to check multiple conditions. Python
provides elif keyword to make nested If statement.
This statement is like executing a if statement inside a else statement.
If statement:
Body
elif statement:
Body
else:
Body
EX:
a=10
if a>=20:
print "Condition is True"
else:
if a>=15:
print "Checking second value"
else:
print "All Conditions are false"
x=int(input(print("Enter Any Num:")))
if x==1:
print("One")
elif x == 2:
print("Two")
elif x == 3:
print("Three")
elif x == 4:
print("Four")
elif x == 5:
print("Five")
else:
print("Invalid Number")
For Loop:
Python for loop is used to iterate the elements of a collection in the order that they
appear. This collection can be a sequence(list or string).
for <variable> in <sequence>:
x=["mohan",20,34.5]
for i in x:
print(i)
To Find Sum of 10 Numbers:
sum=0
for n in range(1,11):
sum+=n
print (sum)
num=2
for a in range (1,6):
print num * a
Python Nested For Loops
Loops defined within another Loop are called Nested Loops. Nested loops are used to
iterate matrix elements or to perform complex computation.
When an outer loop contains an inner loop in its body it is called Nested Looping.
num_list = [1, 2, 3]
char_list = ['a', 'b', 'c']
for n in num_list:
print(n)
for l in char_list:
print(l)
Ex:
list_of_list = [['coke', 'sprite', 'pepsi'],[1, 2, 3],[1.3, 1.4,1.5]]
for list in list_of_list:
for item in list:
print(item)
Python While Loop
In Python, while loop is used to execute number of statements or body till the specified
condition is true. Once the condition is false, the control will come out of the loop.
Syntax:
while <expression>:
Body
i=1
while i<=5:
print("Hello")
i=i+1
With the while loop we can execute a set of statements as long as a
condition is true.
i = 1
while i < 6:
print(i)
i += 1
The break Statement
With the break statement we can stop the loop even if the while condition is
true:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
drinks = ["coke", "pepsi", "sprite"]
for x in drinks:
if x == "pepsi":
break
print(x)
The continue Statement
With the continue statement we can stop the current iteration, and continue
with the next:
Continue to the next iteration if i is 3:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)

More Related Content

Similar to controlstatementspy.docx

Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfAsst.prof M.Gokilavani
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSAniruddha Paul
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in pythonnitamhaske
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonPriyankaC44
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentalsumar78600
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Python programing
Python programingPython programing
Python programinghamzagame
 

Similar to controlstatementspy.docx (20)

Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
 
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
 
Third session
Third sessionThird session
Third session
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
 
PRESENTATION.pptx
PRESENTATION.pptxPRESENTATION.pptx
PRESENTATION.pptx
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMS
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 
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
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Loops
LoopsLoops
Loops
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Python programing
Python programingPython programing
Python programing
 

More from manohar25689

Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docxmanohar25689
 
Multi Threading.docx
Multi Threading.docxMulti Threading.docx
Multi Threading.docxmanohar25689
 
Modules in Python.docx
Modules in Python.docxModules in Python.docx
Modules in Python.docxmanohar25689
 
Logging in Python.docx
Logging in Python.docxLogging in Python.docx
Logging in Python.docxmanohar25689
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docxmanohar25689
 
File Handling in python.docx
File Handling in python.docxFile Handling in python.docx
File Handling in python.docxmanohar25689
 

More from manohar25689 (6)

Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docx
 
Multi Threading.docx
Multi Threading.docxMulti Threading.docx
Multi Threading.docx
 
Modules in Python.docx
Modules in Python.docxModules in Python.docx
Modules in Python.docx
 
Logging in Python.docx
Logging in Python.docxLogging in Python.docx
Logging in Python.docx
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docx
 
File Handling in python.docx
File Handling in python.docxFile Handling in python.docx
File Handling in python.docx
 

Recently uploaded

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

controlstatementspy.docx

  • 1. The if statement executes only when specified condition is true. We can pass any valid expression into the if parentheses. There are various types of if statements in Python. o if statement o if-else statement o nested if statement Syntax: if(condition): statements Ex: a=10 if a==10: print "Welcome to Durgasoft" If Else: The If statement is used to test specified condition and if the condition is true, if block executes, otherwise else block executes. The else statement executes when the if statement is false. if(condition): False statements else: True statements Ex: year=2000 if year%4==0: print "Year is Leap" else: print "Year is not Leap" Program To Find Even or Odd: x=int(input(print("enter any number:"))) r=x%2 if r==0:
  • 2. print("Even Number") else: print("Odd Number") Nested If Else: In python, we can use nested If Else to check multiple conditions. Python provides elif keyword to make nested If statement. This statement is like executing a if statement inside a else statement. If statement: Body elif statement: Body else: Body EX: a=10 if a>=20: print "Condition is True" else: if a>=15: print "Checking second value" else: print "All Conditions are false" x=int(input(print("Enter Any Num:"))) if x==1: print("One") elif x == 2: print("Two") elif x == 3: print("Three") elif x == 4: print("Four")
  • 3. elif x == 5: print("Five") else: print("Invalid Number") For Loop: Python for loop is used to iterate the elements of a collection in the order that they appear. This collection can be a sequence(list or string). for <variable> in <sequence>: x=["mohan",20,34.5] for i in x: print(i) To Find Sum of 10 Numbers: sum=0 for n in range(1,11): sum+=n print (sum) num=2 for a in range (1,6): print num * a Python Nested For Loops Loops defined within another Loop are called Nested Loops. Nested loops are used to iterate matrix elements or to perform complex computation. When an outer loop contains an inner loop in its body it is called Nested Looping.
  • 4. num_list = [1, 2, 3] char_list = ['a', 'b', 'c'] for n in num_list: print(n) for l in char_list: print(l) Ex: list_of_list = [['coke', 'sprite', 'pepsi'],[1, 2, 3],[1.3, 1.4,1.5]] for list in list_of_list: for item in list: print(item) Python While Loop In Python, while loop is used to execute number of statements or body till the specified condition is true. Once the condition is false, the control will come out of the loop. Syntax: while <expression>: Body i=1 while i<=5: print("Hello") i=i+1 With the while loop we can execute a set of statements as long as a condition is true. i = 1 while i < 6: print(i) i += 1
  • 5. The break Statement With the break statement we can stop the loop even if the while condition is true: i = 1 while i < 6: print(i) if i == 3: break i += 1 drinks = ["coke", "pepsi", "sprite"] for x in drinks: if x == "pepsi": break print(x) The continue Statement With the continue statement we can stop the current iteration, and continue with the next: Continue to the next iteration if i is 3: i = 0 while i < 6: i += 1 if i == 3: continue print(i)