SlideShare uma empresa Scribd logo
1 de 25
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

Mais conteúdo relacionado

Mais procurados

Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaEdureka!
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming pptismailmrribi
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialQA TrainingHub
 
Data types in python
Data types in pythonData types in python
Data types in pythonRaginiJain21
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPTShivam Gupta
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-pythonAakashdata
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners Sujith Kumar
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in PythonDevashish Kumar
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
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
 

Mais procurados (20)

Python basic
Python basicPython basic
Python basic
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Python ppt
Python pptPython ppt
Python ppt
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-python
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
Python
PythonPython
Python
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python programming
Python  programmingPython  programming
Python programming
 
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
 

Semelhante a Looping statement in python

loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdfNehaSpillai1
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
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 ...DR B.Surendiran .
 
keshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxkeshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxKeshavBandil2
 
Python programing
Python programingPython programing
Python programinghamzagame
 
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 collegessuser7a7cd61
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfsdvdsvsdvsvds
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONvikram mahendra
 

Semelhante a 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
 
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
 
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
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 

Mais de RaginiJain21

Jump statment in python
Jump statment in pythonJump statment in python
Jump statment in pythonRaginiJain21
 
Conditionalstatement
ConditionalstatementConditionalstatement
ConditionalstatementRaginiJain21
 
Python media library
Python media libraryPython media library
Python media libraryRaginiJain21
 
Basic python programs
Basic python programsBasic python programs
Basic python programsRaginiJain21
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and ModulesRaginiJain21
 
Final presentation on python
Final presentation on pythonFinal presentation on python
Final presentation on pythonRaginiJain21
 

Mais de 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
 

Último

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
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
 
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
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
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
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
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
 
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
 
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
 
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
 
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
 

Último (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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...
 
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
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
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
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
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
 
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
 
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
 
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"
 
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
 
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
 

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