SlideShare une entreprise Scribd logo
1  sur  19
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.
Note: remember to
increment i, or else
the loop will
continue forever.
The while loop requires
relevant variables to be ready,
in this example we need to
define an indexing variable, i,
which we set to 1.
The break Statement
With the break statement we can stop the loop even if
the while condition is true:
The continue Statement
With the continue statement we can stop the current
iteration, and continue with the next:
The else Statement
With the else statement we can run a block of code once
when the condition no longer is true:
Assignments
Counting Up: Write a program that prints numbers from
1 to 10 using a while loop.
Solution
Even Numbers: Create a program that prints
all even numbers from 1 to 20 using a while
loop.
Solution
Countdown: Write a program that counts
down from 10 to 1 and then prints "Blast off!"
using a while loop.
Square Numbers: Develop a program that
prints the square of numbers from 1 to 5
using a while loop.
Solution
Character Printing: Create a program that
prints each character of a given word entered
by the user on a separate line using a while
loop.
Solution
Letter Counter: Write a program that counts
the number of letters in a word entered by the
user using a while loop.
Solution
Positive Number Sum: Develop a program
that asks the user for positive numbers and
prints their sum. The program should stop
when the user enters a negative number
using a while loop.
Solution
Power of Two: Create a program that prints
the powers of 2 from 2^0 to 2^5 using a while
loop.
Solution
Vowel Counter: Write a program that counts
the number of vowels (a, e, i, o, u) in a word
entered by the user using a while loop.
Solution
Name Repeater: Develop a program that
asks the user for their name and then prints it
10 times using a while loop.
Solution

Contenu connexe

Similaire à loops in Python with examples and different problems

Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
maheshnanda14
 
Year 2 dsa c++ exercises on user defined functions
Year 2 dsa  c++ exercises on user defined functionsYear 2 dsa  c++ exercises on user defined functions
Year 2 dsa c++ exercises on user defined functions
ErnesteNtezirizaza
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
adihartanto7
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
AASTHA76
 
1.5 branching and subroutines
1.5   branching and subroutines1.5   branching and subroutines
1.5 branching and subroutines
allenbailey
 

Similaire à loops in Python with examples and different problems (20)

Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
 
Lab4: Functions
Lab4: FunctionsLab4: Functions
Lab4: Functions
 
Year 2 dsa c++ exercises on user defined functions
Year 2 dsa  c++ exercises on user defined functionsYear 2 dsa  c++ exercises on user defined functions
Year 2 dsa c++ exercises on user defined functions
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
 
Advanced Computer Programming..pptx
Advanced Computer Programming..pptxAdvanced Computer Programming..pptx
Advanced Computer Programming..pptx
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop application
 
Oop lab assignment 01
Oop lab assignment 01Oop lab assignment 01
Oop lab assignment 01
 
Loops in C++
Loops in C++Loops in C++
Loops in C++
 
Vks python
Vks pythonVks python
Vks python
 
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
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Java Practice Set
Java Practice SetJava Practice Set
Java Practice Set
 
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 
Chapter 2- Prog101.ppt
Chapter 2- Prog101.pptChapter 2- Prog101.ppt
Chapter 2- Prog101.ppt
 
Lab5: Functions 2
Lab5: Functions 2Lab5: Functions 2
Lab5: Functions 2
 
1.5 branching and subroutines
1.5   branching and subroutines1.5   branching and subroutines
1.5 branching and subroutines
 
Loops
LoopsLoops
Loops
 
Week 2.pptx
Week 2.pptxWeek 2.pptx
Week 2.pptx
 

Dernier

Dernier (14)

2024-05-15-Surat Meetup-Hyperautomation.pptx
2024-05-15-Surat Meetup-Hyperautomation.pptx2024-05-15-Surat Meetup-Hyperautomation.pptx
2024-05-15-Surat Meetup-Hyperautomation.pptx
 
DAY 0 8 A Revelation 05-19-2024 PPT.pptx
DAY 0 8 A Revelation 05-19-2024 PPT.pptxDAY 0 8 A Revelation 05-19-2024 PPT.pptx
DAY 0 8 A Revelation 05-19-2024 PPT.pptx
 
2024 mega trends for the digital workplace - FINAL.pdf
2024 mega trends for the digital workplace - FINAL.pdf2024 mega trends for the digital workplace - FINAL.pdf
2024 mega trends for the digital workplace - FINAL.pdf
 
ServiceNow CIS-Discovery Exam Dumps 2024
ServiceNow CIS-Discovery Exam Dumps 2024ServiceNow CIS-Discovery Exam Dumps 2024
ServiceNow CIS-Discovery Exam Dumps 2024
 
Deciding The Topic of our Magazine.pptx.
Deciding The Topic of our Magazine.pptx.Deciding The Topic of our Magazine.pptx.
Deciding The Topic of our Magazine.pptx.
 
SaaStr Workshop Wednesday with CEO of Guru
SaaStr Workshop Wednesday with CEO of GuruSaaStr Workshop Wednesday with CEO of Guru
SaaStr Workshop Wednesday with CEO of Guru
 
Microsoft Fabric Analytics Engineer (DP-600) Exam Dumps 2024.pdf
Microsoft Fabric Analytics Engineer (DP-600) Exam Dumps 2024.pdfMicrosoft Fabric Analytics Engineer (DP-600) Exam Dumps 2024.pdf
Microsoft Fabric Analytics Engineer (DP-600) Exam Dumps 2024.pdf
 
The Influence and Evolution of Mogul Press in Contemporary Public Relations.docx
The Influence and Evolution of Mogul Press in Contemporary Public Relations.docxThe Influence and Evolution of Mogul Press in Contemporary Public Relations.docx
The Influence and Evolution of Mogul Press in Contemporary Public Relations.docx
 
The Concession of Asaba International Airport: Balancing Politics and Policy ...
The Concession of Asaba International Airport: Balancing Politics and Policy ...The Concession of Asaba International Airport: Balancing Politics and Policy ...
The Concession of Asaba International Airport: Balancing Politics and Policy ...
 
STM valmiusseminaari 26-04-2024 PUUMALAINEN Ajankohtaista kansainvälisestä yh...
STM valmiusseminaari 26-04-2024 PUUMALAINEN Ajankohtaista kansainvälisestä yh...STM valmiusseminaari 26-04-2024 PUUMALAINEN Ajankohtaista kansainvälisestä yh...
STM valmiusseminaari 26-04-2024 PUUMALAINEN Ajankohtaista kansainvälisestä yh...
 
Understanding Poverty: A Community Questionnaire
Understanding Poverty: A Community QuestionnaireUnderstanding Poverty: A Community Questionnaire
Understanding Poverty: A Community Questionnaire
 
TSM unit 5 Toxicokinetics seminar by Ansari Aashif Raza.pptx
TSM unit 5 Toxicokinetics seminar by  Ansari Aashif Raza.pptxTSM unit 5 Toxicokinetics seminar by  Ansari Aashif Raza.pptx
TSM unit 5 Toxicokinetics seminar by Ansari Aashif Raza.pptx
 
Databricks Machine Learning Associate Exam Dumps 2024.pdf
Databricks Machine Learning Associate Exam Dumps 2024.pdfDatabricks Machine Learning Associate Exam Dumps 2024.pdf
Databricks Machine Learning Associate Exam Dumps 2024.pdf
 
ACM CHT Best Inspection Practices Kinben Innovation MIC Slideshare.pdf
ACM CHT Best Inspection Practices Kinben Innovation MIC Slideshare.pdfACM CHT Best Inspection Practices Kinben Innovation MIC Slideshare.pdf
ACM CHT Best Inspection Practices Kinben Innovation MIC Slideshare.pdf
 

loops in Python with examples and different problems

  • 2. Python has two primitive loop commands: while loops for loops
  • 3. The while Loop With the while loop we can execute a set of statements as long as a condition is true. Note: remember to increment i, or else the loop will continue forever.
  • 4. The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.
  • 5. The break Statement With the break statement we can stop the loop even if the while condition is true:
  • 6.
  • 7.
  • 8. The continue Statement With the continue statement we can stop the current iteration, and continue with the next:
  • 9. The else Statement With the else statement we can run a block of code once when the condition no longer is true:
  • 10. Assignments Counting Up: Write a program that prints numbers from 1 to 10 using a while loop. Solution
  • 11. Even Numbers: Create a program that prints all even numbers from 1 to 20 using a while loop. Solution
  • 12. Countdown: Write a program that counts down from 10 to 1 and then prints "Blast off!" using a while loop.
  • 13. Square Numbers: Develop a program that prints the square of numbers from 1 to 5 using a while loop. Solution
  • 14. Character Printing: Create a program that prints each character of a given word entered by the user on a separate line using a while loop. Solution
  • 15. Letter Counter: Write a program that counts the number of letters in a word entered by the user using a while loop. Solution
  • 16. Positive Number Sum: Develop a program that asks the user for positive numbers and prints their sum. The program should stop when the user enters a negative number using a while loop. Solution
  • 17. Power of Two: Create a program that prints the powers of 2 from 2^0 to 2^5 using a while loop. Solution
  • 18. Vowel Counter: Write a program that counts the number of vowels (a, e, i, o, u) in a word entered by the user using a while loop. Solution
  • 19. Name Repeater: Develop a program that asks the user for their name and then prints it 10 times using a while loop. Solution