SlideShare a Scribd company logo
Looping statements
A loop statement allows us to execute a statement
or group of statements multiple times.
Looping statements
Types of Looping statements
For loop
While
loop
Nested
loop
For Loop
The for loop in python is
used to iterate the
statements or part of
the program several
times. It is frequently
used to traverse the
data structures like list,
tuple, or dictionary.
Flowchart
for iterating_var in sequence:
statements
Syntax
Write a
program to
print 1 to 10
series
i=1
For i in range(0,11):
Print(i)
Output
0 1 2 3 4 5 6 7 8 9
Example
To print
multiplication table
i=0
n=int(input("enter no"))
for i in range(0,11):
print("%d X %d = %d" %
(n,i,n*i))
enter no 6
6 X 0 = 0
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60
Output
Program
to
Iterating
string
using for
loop
str = "Python"
for i in str:
print(i)
P
Y
T
H
O
N
Output
Example
Program to iterate through a
list using indexing
city = ['Bhopal', 'Indore', 'Gwalior',
'Ujjain', 'Sagar‘]
# iterate over the list using index
for i in range(len(city)):
print("I go to", city[i])
I go to Bhopal
I go to Indore
I go to Gwalior
I go to Ujjain
I go to Sagar
Output Example
While loop syntax
while (
test_expression):
Body of while
Repeats a statement or
group of statements while
a given condition is TRUE. It
tests the condition before
executing the loop body.
Flow chart
i = 1
while i < 6:
print(i)
i += 1
1
2
3
4
5
Output
Example
Program to
print 1 to 5
series
a =[‘student1', ‘student2',
‘student3']
while a:
(a.pop(-1))
Student3
Student2
Student1
Program to print list in reverse order
Example Output
10
9
8
7
6
5
4
3
2
1
Example
Output
n=10
while n > 0:
n -= 1;
if True: print(n)
Using if Statement with While Loop
Using else Statement with While Loop
Python supports to 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 becomes
false.
Syntax
while expr:
statement(s)
else:
additional_statement(s)
count = 0
while count < 5:
print count, " is
less than 5"
count = count + 1
else:
print count, " is
not less than 5"
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
Program to print
series with else state
Example
Output
Find Even odd no
number = 2
while number < 10 :
# Find the mod of 2
if number%2 == 0:
print("The number
"+str(number)+" is even")
else:
print("The number
"+str(number)+" is odd")
# Increment `number` by 1
number = number+1
The number 2 is even
The number 3 is odd
The number 4 is even
The number 5 is odd
The number 6 is even
The number 7 is odd
The number 8 is even
The number 9 is odd
Output
Nested loop
2
3
5
7
11
13
prime no
i = 2
print("prime no")
while(i < 15):
j = 2
while(j <= (i/j)):
if not(i%j): break
j = j + 1
if (j > i/j) : print(i)
i = i + 1
You can use one or more
loop inside any another
while, for or do..while
loop.
Example
Program to
print given
pattern
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
while(i<=5):
j=5
while(j>=i):
print(j, end=' ‘)
j-=1
i+=1
print()
Output
Example
program to
print given
below
pattern
*
**
***
****
*****
rows = int(input("Enter the rows:"))
# Outer loop will print number of ro
ws
for i in range(0,rows+1):
# Inner loop will print number of Ast
risk
for j in range(i):
print("*",end = '')
print()
Example
Program to print
given pattern
1
22
333
4444
55555
rows = int(input("Enter the
rows"))
for i in range(0,rows+1):
for j in range(i):
print(i,end = '')
print()
For more presentation
contact us
raginijain0208@gmail.com
Looping statement in python

More Related Content

What's hot

What's hot (20)

List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python list
Python listPython list
Python list
 
Array ppt
Array pptArray ppt
Array ppt
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python for loop
Python for loopPython for loop
Python for loop
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Python libraries
Python librariesPython libraries
Python libraries
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
2D Array
2D Array 2D Array
2D Array
 
Arrays
ArraysArrays
Arrays
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Slicing
SlicingSlicing
Slicing
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
 

Similar to Looping statement in python

loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
DheeravathBinduMadha
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
DURAIMURUGANM2
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 

Similar to Looping statement in python (20)

loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
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
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
loops.pdf
loops.pdfloops.pdf
loops.pdf
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
keshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxkeshavnptel_ppt[1].docx
keshavnptel_ppt[1].docx
 
Python programing
Python programingPython programing
Python programing
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
While_for_loop presententationin first year students
While_for_loop presententationin first year studentsWhile_for_loop presententationin first year students
While_for_loop presententationin first year students
 
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 fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 

More from RaginiJain21

More from RaginiJain21 (7)

Jump statment in python
Jump statment in pythonJump statment in python
Jump statment in python
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
 
Python media library
Python media libraryPython media library
Python media library
 
Basic python programs
Basic python programsBasic python programs
Basic python programs
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
 
Python second ppt
Python second pptPython second ppt
Python second ppt
 
Final presentation on python
Final presentation on pythonFinal presentation on python
Final presentation on python
 

Recently uploaded

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 

Looping statement in python

  • 2. A loop statement allows us to execute a statement or group of statements multiple times. Looping statements
  • 3. Types of Looping statements For loop While loop Nested loop
  • 4. For Loop The for loop in python is used to iterate the statements or part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.
  • 6. for iterating_var in sequence: statements Syntax
  • 7. Write a program to print 1 to 10 series i=1 For i in range(0,11): Print(i) Output 0 1 2 3 4 5 6 7 8 9 Example
  • 8. To print multiplication table i=0 n=int(input("enter no")) for i in range(0,11): print("%d X %d = %d" % (n,i,n*i)) enter no 6 6 X 0 = 0 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 6 X 7 = 42 6 X 8 = 48 6 X 9 = 54 6 X 10 = 60 Output
  • 9. Program to Iterating string using for loop str = "Python" for i in str: print(i) P Y T H O N Output Example
  • 10. Program to iterate through a list using indexing city = ['Bhopal', 'Indore', 'Gwalior', 'Ujjain', 'Sagar‘] # iterate over the list using index for i in range(len(city)): print("I go to", city[i]) I go to Bhopal I go to Indore I go to Gwalior I go to Ujjain I go to Sagar Output Example
  • 11. While loop syntax while ( test_expression): Body of while Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
  • 13. i = 1 while i < 6: print(i) i += 1 1 2 3 4 5 Output Example Program to print 1 to 5 series
  • 14. a =[‘student1', ‘student2', ‘student3'] while a: (a.pop(-1)) Student3 Student2 Student1 Program to print list in reverse order Example Output
  • 15. 10 9 8 7 6 5 4 3 2 1 Example Output n=10 while n > 0: n -= 1; if True: print(n) Using if Statement with While Loop
  • 16. Using else Statement with While Loop Python supports to 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 becomes false.
  • 18. count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" 0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5 Program to print series with else state Example Output
  • 19. Find Even odd no number = 2 while number < 10 : # Find the mod of 2 if number%2 == 0: print("The number "+str(number)+" is even") else: print("The number "+str(number)+" is odd") # Increment `number` by 1 number = number+1 The number 2 is even The number 3 is odd The number 4 is even The number 5 is odd The number 6 is even The number 7 is odd The number 8 is even The number 9 is odd Output
  • 20. Nested loop 2 3 5 7 11 13 prime no i = 2 print("prime no") while(i < 15): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print(i) i = i + 1 You can use one or more loop inside any another while, for or do..while loop. Example
  • 21. Program to print given pattern 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5 while(i<=5): j=5 while(j>=i): print(j, end=' ‘) j-=1 i+=1 print() Output Example
  • 22. program to print given below pattern * ** *** **** ***** rows = int(input("Enter the rows:")) # Outer loop will print number of ro ws for i in range(0,rows+1): # Inner loop will print number of Ast risk for j in range(i): print("*",end = '') print() Example
  • 23. Program to print given pattern 1 22 333 4444 55555 rows = int(input("Enter the rows")) for i in range(0,rows+1): for j in range(i): print(i,end = '') print()
  • 24. For more presentation contact us raginijain0208@gmail.com