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:
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