SlideShare a Scribd company logo
Python Loops
Python has two primitive loop commands:
• While loops
• for loops
The while Loop:
• With the while loop we can execute a set of statements
as long as a condition is true.
• Example:
i = 1
while i < 6:
print(i)
i += 1
• Output:
1
2
3
4
5
The break Statement(While loop):
• With the break statement we can stop the loop even if
the while condition is true
• Example:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
• Output:
1
2
3
The continue Statement(while loop)
• With the continue statement we can stop the current
iteration, and continue with the next.
• Example:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
• Output:
1
2
3
4
5
6
The else Statement(while loop)
• With the else statement we can run a block of code once
when the condition no longer is true
• Example:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
• Output:
1
2
3
4
5
i is no longer less than 6
Python For Loop:
• A for loop is used for iterating over a sequence (that is
either a list, a tuple, a dictionary, a set, or a string).
• This is less like the for keyword in other programming
languages, and works more like an iterator method as
found in other object-orientated programming
languages.
• With the for loop we can execute a set of statements,
once for each item in a list, tuple, set etc.
• Example:
• Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
• Output:
Apple
Banana
cherry
Looping Through a String(for loop)
• Even strings are iterable objects, they contain a sequence of
characters.
• Example:
• Loop through the letters in the word "banana“:
for x in "banana":
print(x)
• Output:
b
a
n
a
n
a
The break Statement(for loop)
• With the break statement we can stop the loop before it
has looped through all the items.
• Example:
• Exit the loop when x is “banana”:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
• Output:
Apple
banana
• Example:
• Exit the loop when x is “banana”,but this time the break comes before the
print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
• Output:
Apple
The continue Statement(for loop)
• With the continue statement we can stop the current
iteration of the loop, and continue with the next.
• Example:
• Do not print banana
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
• Output:
Apple
cherry
The range() Function (for loop)
• To loop through a set of code a specified number of
times, we can use the range() function,
• The range() function returns a sequence of numbers,
starting from 0 by default, and increments by 1 (by
default), and ends at a specified number.
• Example:
for x in range(6):
print(x)
• Output:
0
1
2
3
4
5
• The range() function defaults to 0 as a starting value,
however it is possible to specify the starting value by
adding a parameter: range(2, 6), which means values
from 2 to 6 (but not including 6)
• Example:
for x in range(2, 6):
print(x)
• Output:
2
3
4
5
Else in For Loop
• The else keyword in a for loop specifies a block of code to be executed
when the loop is finished:
• Example:
• Print all numbers from 0 to 5, and print a message when the loop has
ended
for x in range(6):
print(x)
else:
print("Finally finished!")
• Output:
0
1
2
3
4
5
Finally finished!
Nested Loops
• A nested loop is a loop inside a loop.
• The "inner loop" will be executed one time for each
iteration of the "outer loop"
• Example:
• Print each adjective for every fruit
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
• Output:
Red apple
Red banana
Red cherry
Big apple
Big banana
Big cherry
Testy apple
Testy banana
Testy cherry
The pass Statement
• For loops can not be empty but if you for some reason have a for loop
with no content, put in the pass statement to avoid getting an error.
• Example:
for x in [0, 1, 2]:
pass
• Output:
# having an empty for loop like this, would raise an
error without the pass statement

More Related Content

PPTX
python ppt.pptx
PDF
Python unit 2 M.sc cs
PPTX
Python for and while Loops with basic Examples
PPTX
Loops in python including control statements and various test cases
PDF
While-For-loop in python used in college
PPTX
While_for_loop presententationin first year students
PPTX
TN 12 computer Science - ppt CHAPTER-6.pptx
PPTX
1. control structures in the python.pptx
python ppt.pptx
Python unit 2 M.sc cs
Python for and while Loops with basic Examples
Loops in python including control statements and various test cases
While-For-loop in python used in college
While_for_loop presententationin first year students
TN 12 computer Science - ppt CHAPTER-6.pptx
1. control structures in the python.pptx

Similar to Python_Loops.pptxpython loopspython loopspython loopspython loopspython loopspython loopspython loops (20)

PDF
python program
PPTX
Python Flow Control & use of functions.pptx
DOCX
iterations.docx
PPTX
introduction to loops in python/python loops
PPTX
Loops in python.pptx/ introduction to loops in python
PPTX
PPTX
Lecture on Fundamentals of Python Programming-2
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PPT
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
PPTX
This is all about control flow in python intruducing the Break and Continue.pptx
PPTX
module 2.pptx
PPTX
Module_2_1_Building Python Programs_Final.pptx
PPTX
Python programming –part 3
PPTX
Decision control units by Python.pptx includes loop, If else, list, tuple and...
PPT
Looping Statements.ppt yes yes yes yes yes
PDF
PYTHON FULL TUTORIAL WITH PROGRAMMS
PPTX
Looping Statements and Control Statements in Python
PPTX
PyClassDay13.pptx
PPTX
industry coding practice unit-2 ppt.pptx
python program
Python Flow Control & use of functions.pptx
iterations.docx
introduction to loops in python/python loops
Loops in python.pptx/ introduction to loops in python
Lecture on Fundamentals of Python Programming-2
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
This is all about control flow in python intruducing the Break and Continue.pptx
module 2.pptx
Module_2_1_Building Python Programs_Final.pptx
Python programming –part 3
Decision control units by Python.pptx includes loop, If else, list, tuple and...
Looping Statements.ppt yes yes yes yes yes
PYTHON FULL TUTORIAL WITH PROGRAMMS
Looping Statements and Control Statements in Python
PyClassDay13.pptx
industry coding practice unit-2 ppt.pptx
Ad

More from RutviBaraiya (8)

PDF
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
PDF
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
PPTX
hospital_management.pptxhospital_management.pptx
PPTX
Healthcare_system.pptHealthcare_systemHealthcare_systemx
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
PPT
PC_ASSEMBLING_DISSEMBLPC_ASSEMBLING_DISSEMBLING.pptING.ppt
PPTX
C language (1).pptxC language (1C language (1).pptx).pptx
PPT
introductiontocprogramming-130719083552-phpapp01.ppt
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
hospital_management.pptxhospital_management.pptx
Healthcare_system.pptHealthcare_systemHealthcare_systemx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
PC_ASSEMBLING_DISSEMBLPC_ASSEMBLING_DISSEMBLING.pptING.ppt
C language (1).pptxC language (1C language (1).pptx).pptx
introductiontocprogramming-130719083552-phpapp01.ppt
Ad

Recently uploaded (20)

PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
This slide provides an overview Technology
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
PPTX
How Much Does It Cost to Build a Train Ticket App like Trenitalia in Italy.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
Doc9.....................................
Top Generative AI Tools for Patent Drafting in 2025.pdf
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
CroxyProxy Instagram Access id login.pptx
NewMind AI Monthly Chronicles - July 2025
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
agentic-ai-and-the-future-of-autonomous-systems.pdf
Chapter 2 Digital Image Fundamentals.pdf
This slide provides an overview Technology
madgavkar20181017ppt McKinsey Presentation.pdf
NewMind AI Weekly Chronicles - July'25 - Week IV
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Revolutionize Operations with Intelligent IoT Monitoring and Control
Smarter Business Operations Powered by IoT Remote Monitoring
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
How Much Does It Cost to Build a Train Ticket App like Trenitalia in Italy.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Understanding_Digital_Forensics_Presentation.pptx
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
Doc9.....................................

Python_Loops.pptxpython loopspython loopspython loopspython loopspython loopspython loopspython loops

  • 1. Python Loops Python has two primitive loop commands: • While loops • for loops
  • 2. The while Loop: • With the while loop we can execute a set of statements as long as a condition is true. • Example: i = 1 while i < 6: print(i) i += 1 • Output: 1 2 3 4 5
  • 3. The break Statement(While loop): • With the break statement we can stop the loop even if the while condition is true • Example: i = 1 while i < 6: print(i) if i == 3: break i += 1 • Output: 1 2 3
  • 4. The continue Statement(while loop) • With the continue statement we can stop the current iteration, and continue with the next. • Example: i = 0 while i < 6: i += 1 if i == 3: continue print(i) • Output: 1 2 3 4 5 6
  • 5. The else Statement(while loop) • With the else statement we can run a block of code once when the condition no longer is true • Example: i = 1 while i < 6: print(i) i += 1 else: print("i is no longer less than 6") • Output: 1 2 3 4 5 i is no longer less than 6
  • 6. Python For Loop: • A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). • This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. • With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
  • 7. • Example: • Print each fruit in a fruit list: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) • Output: Apple Banana cherry
  • 8. Looping Through a String(for loop) • Even strings are iterable objects, they contain a sequence of characters. • Example: • Loop through the letters in the word "banana“: for x in "banana": print(x) • Output: b a n a n a
  • 9. The break Statement(for loop) • With the break statement we can stop the loop before it has looped through all the items. • Example: • Exit the loop when x is “banana”: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break • Output: Apple banana
  • 10. • Example: • Exit the loop when x is “banana”,but this time the break comes before the print: fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": break print(x) • Output: Apple
  • 11. The continue Statement(for loop) • With the continue statement we can stop the current iteration of the loop, and continue with the next. • Example: • Do not print banana fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": continue print(x) • Output: Apple cherry
  • 12. The range() Function (for loop) • To loop through a set of code a specified number of times, we can use the range() function, • The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. • Example: for x in range(6): print(x) • Output: 0 1 2 3 4 5
  • 13. • The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6) • Example: for x in range(2, 6): print(x) • Output: 2 3 4 5
  • 14. Else in For Loop • The else keyword in a for loop specifies a block of code to be executed when the loop is finished: • Example: • Print all numbers from 0 to 5, and print a message when the loop has ended for x in range(6): print(x) else: print("Finally finished!") • Output: 0 1 2 3 4 5 Finally finished!
  • 15. Nested Loops • A nested loop is a loop inside a loop. • The "inner loop" will be executed one time for each iteration of the "outer loop" • Example: • Print each adjective for every fruit adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y)
  • 16. • Output: Red apple Red banana Red cherry Big apple Big banana Big cherry Testy apple Testy banana Testy cherry
  • 17. The pass Statement • For loops can not be empty but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error. • Example: for x in [0, 1, 2]: pass • Output: # having an empty for loop like this, would raise an error without the pass statement