SlideShare uma empresa Scribd logo
1 de 14
ITERATIONS
PROF POOJAB S
 while statements
 Infinite loop and break
 Finishing iterations with continue
 Definite loops using for
 Loop Pattern
 Counting and Summing loops
 Maximum and Minimum loops
Iteration
The while Statement
 Syntax:
while condition:
Statement 1
Statement 2
…..
Statement N
Statements after while
 while: keyword
conditions will be evaluated and leads True or False
if True Statements will be executed else exits.
 Eg:
1. n=1
while n<=5:
print(n)
n=n+1
print("Over")
2. n=5
while n>=0:
print(n)
n=n-1
print("Exit")
 Loop execute infinite number of times.
 Eg:
n=1
while True:
print(n)
n=n+1
 Above program, loop condition is True, so it will never exits.
 If we want to come out from the loop, we use break.
 Eg:
n=1
while True:
print(n)
n=n+1
break
Infinite Loops, break and continue
 Eg:
1. while True:
x=int(input("Enter a Number:"))
if x>=0:
print("You have entered a positive number. The number is:",x)
else:
print("You have entered a negative number")
2. while True:
x=int(input("Enter a Number:"))
if x>=0:
print("You have entered a positive number. The number is:",x)
else:
print("You have entered a negative number")
break
while True:
line=input(">")
if line=='done':
break
print(line)
print('Done!')
 Move to next iterations , use continue statement.
 Eg:
sum=0
count=0
while True:
x= int (input ("Enter a Number:"))
if x%2==0:
continue
else:
sum=sum+x
count=count+1
if count==5:
break
print("Sum=",sum)
continue statement
Definite Loops using for
 Syntax of for loop:
for var in list/sequence:
Statement 1
Statement 2
…..
Statement N
Statements after for loop
 for and in are keywords
list/ sequence is a set of elements on which the loop iterates
statements constitutes the body of loop
 Eg:
names=['Ram','Shyam','Raj']
for x in names:
print("Happy New Year",x)
print('Done!')
 List is an important data type.
 Can take elements of different types.
 Elements enclosed within square brackets.
 Elements can be extracted using index.
 If there are fixed set of numbers to iterate in a for loop, we can use range()
 range(start, end, steps)
Start: starting values
End: ending values but excluding ending value
Steps: increment/ decrement [default value is 1]
1. for i in range(5):
print(i,end='t')
2. for i in range(5,0,-1):
print(i,end='t')
3. for i in range(0,10,2):
print(i,end='t')
 while and for loop used to go through a list of items and check max or min data values.
1. Initialize one/more variables before loop starts.
2. Perform computation
3. Look result
 Counting and Summing Loops:
count=0
for i in [4,-2,41,34,25]:
count=count+1 #COUNT
print("Count=",count)
total=0
for i in [4,-2,41,34,25]:
total=total+I #SUMMATION
print("Total=",total)
Loop Patterns
big=None
print("Before Loop:",big)
for x in [12,0,21,-3]:
if big is None or x>big: #MAXIMUM
big=x
print("Itearation Variable:",x,'Big',big)
print("Biggest:",big)
small=None
print("Before Loop:",small)
for x in [12,0,21,-3]:
if small is None or x<small: #MINIMUM
small=x
print("Iteration Variable:",x,'Small',small)
print("Smallest:",small)

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
Python
PythonPython
Python
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Python strings
Python stringsPython strings
Python strings
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)
 
Day2
Day2Day2
Day2
 
Python course Day 1
Python course Day 1Python course Day 1
Python course Day 1
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Day3
Day3Day3
Day3
 
Python ppt
Python pptPython ppt
Python ppt
 
Python :variable types
Python :variable typesPython :variable types
Python :variable types
 

Semelhante a Iteration

Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Ziyauddin Shaik
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)ssuser7f90ae
 
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88Mahmoud Samir Fayed
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_iElsayed Hemayed
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Ziyauddin Shaik
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
The Ring programming language version 1.7 book - Part 27 of 196
The Ring programming language version 1.7 book - Part 27 of 196The Ring programming language version 1.7 book - Part 27 of 196
The Ring programming language version 1.7 book - Part 27 of 196Mahmoud Samir Fayed
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureNurjahan Nipa
 
Python programing
Python programingPython programing
Python programinghamzagame
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 

Semelhante a Iteration (20)

Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
loops python.pdf
loops python.pdfloops python.pdf
loops python.pdf
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 
L06
L06L06
L06
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
 
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_i
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
How to Program
How to ProgramHow to Program
How to Program
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
The Ring programming language version 1.7 book - Part 27 of 196
The Ring programming language version 1.7 book - Part 27 of 196The Ring programming language version 1.7 book - Part 27 of 196
The Ring programming language version 1.7 book - Part 27 of 196
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
Python Lecture 5
Python Lecture 5Python Lecture 5
Python Lecture 5
 
Python programing
Python programingPython programing
Python programing
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 

Último

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
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
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Último (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
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
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
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 ...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Iteration

  • 2.  while statements  Infinite loop and break  Finishing iterations with continue  Definite loops using for  Loop Pattern  Counting and Summing loops  Maximum and Minimum loops Iteration
  • 3. The while Statement  Syntax: while condition: Statement 1 Statement 2 ….. Statement N Statements after while  while: keyword conditions will be evaluated and leads True or False if True Statements will be executed else exits.
  • 4.  Eg: 1. n=1 while n<=5: print(n) n=n+1 print("Over") 2. n=5 while n>=0: print(n) n=n-1 print("Exit")
  • 5.  Loop execute infinite number of times.  Eg: n=1 while True: print(n) n=n+1  Above program, loop condition is True, so it will never exits.  If we want to come out from the loop, we use break.  Eg: n=1 while True: print(n) n=n+1 break Infinite Loops, break and continue
  • 6.  Eg: 1. while True: x=int(input("Enter a Number:")) if x>=0: print("You have entered a positive number. The number is:",x) else: print("You have entered a negative number") 2. while True: x=int(input("Enter a Number:")) if x>=0: print("You have entered a positive number. The number is:",x) else: print("You have entered a negative number") break
  • 8.  Move to next iterations , use continue statement.  Eg: sum=0 count=0 while True: x= int (input ("Enter a Number:")) if x%2==0: continue else: sum=sum+x count=count+1 if count==5: break print("Sum=",sum) continue statement
  • 10.  Syntax of for loop: for var in list/sequence: Statement 1 Statement 2 ….. Statement N Statements after for loop  for and in are keywords list/ sequence is a set of elements on which the loop iterates statements constitutes the body of loop
  • 11.  Eg: names=['Ram','Shyam','Raj'] for x in names: print("Happy New Year",x) print('Done!')  List is an important data type.  Can take elements of different types.  Elements enclosed within square brackets.  Elements can be extracted using index.  If there are fixed set of numbers to iterate in a for loop, we can use range()
  • 12.  range(start, end, steps) Start: starting values End: ending values but excluding ending value Steps: increment/ decrement [default value is 1] 1. for i in range(5): print(i,end='t') 2. for i in range(5,0,-1): print(i,end='t') 3. for i in range(0,10,2): print(i,end='t')
  • 13.  while and for loop used to go through a list of items and check max or min data values. 1. Initialize one/more variables before loop starts. 2. Perform computation 3. Look result  Counting and Summing Loops: count=0 for i in [4,-2,41,34,25]: count=count+1 #COUNT print("Count=",count) total=0 for i in [4,-2,41,34,25]: total=total+I #SUMMATION print("Total=",total) Loop Patterns
  • 14. big=None print("Before Loop:",big) for x in [12,0,21,-3]: if big is None or x>big: #MAXIMUM big=x print("Itearation Variable:",x,'Big',big) print("Biggest:",big) small=None print("Before Loop:",small) for x in [12,0,21,-3]: if small is None or x<small: #MINIMUM small=x print("Iteration Variable:",x,'Small',small) print("Smallest:",small)