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

loops in Python with examples and different problems

  • 1.
  • 2.
    Python has twoprimitive loop commands: while loops for loops
  • 3.
    The while Loop Withthe 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 looprequires 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 Withthe break statement we can stop the loop even if the while condition is true:
  • 8.
    The continue Statement Withthe continue statement we can stop the current iteration, and continue with the next:
  • 9.
    The else Statement Withthe else statement we can run a block of code once when the condition no longer is true:
  • 10.
    Assignments Counting Up: Writea program that prints numbers from 1 to 10 using a while loop. Solution
  • 11.
    Even Numbers: Createa program that prints all even numbers from 1 to 20 using a while loop. Solution
  • 12.
    Countdown: Write aprogram that counts down from 10 to 1 and then prints "Blast off!" using a while loop.
  • 13.
    Square Numbers: Developa program that prints the square of numbers from 1 to 5 using a while loop. Solution
  • 14.
    Character Printing: Createa 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: Writea 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: Writea 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: Developa program that asks the user for their name and then prints it 10 times using a while loop. Solution