SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
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)

More Related Content

PPTX
String Manipulation in Python
Pooja B S
 
PPTX
Dictionary
Pooja B S
 
PDF
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
PDF
Strings in Python
nitamhaske
 
PPTX
Chapter 14 strings
Praveen M Jigajinni
 
String Manipulation in Python
Pooja B S
 
Dictionary
Pooja B S
 
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Strings in Python
nitamhaske
 
Chapter 14 strings
Praveen M Jigajinni
 

What's hot (20)

PPTX
Python Datatypes by SujithKumar
Sujith Kumar
 
PPT
Python
Kumar Gaurav
 
PDF
Strings in python
Prabhakaran V M
 
PPTX
Python programming: Anonymous functions, String operations
Megha V
 
PDF
Python strings
Mohammed Sikander
 
PPTX
Python programming- Part IV(Functions)
Megha V
 
PPTX
Parts of python programming language
Megha V
 
PDF
Datatypes in python
eShikshak
 
PPT
Strings Arrays
phanleson
 
PDF
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
PPTX
Standard data-types-in-py
Priyanshu Sengar
 
PPTX
String in python lecture (3)
Ali ٍSattar
 
ODP
Day2
Karin Lagesen
 
ODP
Python course Day 1
Karin Lagesen
 
PPTX
Python programming –part 3
Megha V
 
PDF
Day3
Karin Lagesen
 
PPTX
Python ppt
Anush verma
 
PDF
Python :variable types
S.M. Salaquzzaman
 
Python Datatypes by SujithKumar
Sujith Kumar
 
Python
Kumar Gaurav
 
Strings in python
Prabhakaran V M
 
Python programming: Anonymous functions, String operations
Megha V
 
Python strings
Mohammed Sikander
 
Python programming- Part IV(Functions)
Megha V
 
Parts of python programming language
Megha V
 
Datatypes in python
eShikshak
 
Strings Arrays
phanleson
 
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
Standard data-types-in-py
Priyanshu Sengar
 
String in python lecture (3)
Ali ٍSattar
 
Python course Day 1
Karin Lagesen
 
Python programming –part 3
Megha V
 
Python ppt
Anush verma
 
Python :variable types
S.M. Salaquzzaman
 
Ad

Similar to Iteration (20)

PDF
Python_Module_2.pdf
R.K.College of engg & Tech
 
PPTX
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
FahmaFamzin
 
PDF
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
PDF
loops python.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
Introduction to python programming ( part-3 )
Ziyauddin Shaik
 
DOC
Slide07 repetitions
altwirqi
 
PPTX
Learn various loops and Iterations in Python
coreyanderson7866
 
PPTX
Pythonlearn-05-Iterations Lecture Python
ssuser2b99481
 
PDF
23UCACC11 Python Programming (MTNC) (BCA)
ssuser7f90ae
 
PDF
The Ring programming language version 1.3 book - Part 11 of 88
Mahmoud Samir Fayed
 
PPTX
Python.pptx
AKANSHAMITTAL2K21AFI
 
PPTX
Csci101 lect03 algorithms_i
Elsayed Hemayed
 
PPTX
High PR PPT submission sites generally allow you to add backlinks
abdullahkhan94790
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
DOCX
Python unit 3 and Unit 4
Anandh Arumugakan
 
PPTX
UNIT 1.pptx Programming for Problem Solving
ramesh130484
 
PPTX
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
PPT
How to Program
Damian T. Gordon
 
PPTX
CHAPTER 5
mohd_mizan
 
Python_Module_2.pdf
R.K.College of engg & Tech
 
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
FahmaFamzin
 
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
Introduction to python programming ( part-3 )
Ziyauddin Shaik
 
Slide07 repetitions
altwirqi
 
Learn various loops and Iterations in Python
coreyanderson7866
 
Pythonlearn-05-Iterations Lecture Python
ssuser2b99481
 
23UCACC11 Python Programming (MTNC) (BCA)
ssuser7f90ae
 
The Ring programming language version 1.3 book - Part 11 of 88
Mahmoud Samir Fayed
 
Csci101 lect03 algorithms_i
Elsayed Hemayed
 
High PR PPT submission sites generally allow you to add backlinks
abdullahkhan94790
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
Python unit 3 and Unit 4
Anandh Arumugakan
 
UNIT 1.pptx Programming for Problem Solving
ramesh130484
 
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
How to Program
Damian T. Gordon
 
CHAPTER 5
mohd_mizan
 
Ad

Recently uploaded (20)

PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
CDH. pptx
AneetaSharma15
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
CDH. pptx
AneetaSharma15
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 

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)