SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler offline
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
loopingstatementinpython-210628184047 (1).pdf

Mais conteúdo relacionado

Semelhante a loopingstatementinpython-210628184047 (1).pdf

Python programing
Python programingPython programing
Python programinghamzagame
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
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
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
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
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
 
keshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxkeshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxKeshavBandil2
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfsdvdsvsdvsvds
 
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
 
Python+Syntax+Cheat+Sheet+Booklet.pdf
Python+Syntax+Cheat+Sheet+Booklet.pdfPython+Syntax+Cheat+Sheet+Booklet.pdf
Python+Syntax+Cheat+Sheet+Booklet.pdfGanteng tengab
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statementnarmadhakin
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONvikram mahendra
 

Semelhante a loopingstatementinpython-210628184047 (1).pdf (20)

Python programing
Python programingPython programing
Python programing
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
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
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
keshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxkeshavnptel_ppt[1].docx
keshavnptel_ppt[1].docx
 
loops.pdf
loops.pdfloops.pdf
loops.pdf
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
 
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
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Programming egs
Programming egs Programming egs
Programming egs
 
Python+Syntax+Cheat+Sheet+Booklet.pdf
Python+Syntax+Cheat+Sheet+Booklet.pdfPython+Syntax+Cheat+Sheet+Booklet.pdf
Python+Syntax+Cheat+Sheet+Booklet.pdf
 
C tutorial
C tutorialC tutorial
C tutorial
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 

Último

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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
 
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
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Último (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
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
 
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
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

loopingstatementinpython-210628184047 (1).pdf

  • 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