loops in Python with examples and different problems
GulziraIzbassova
68 views
19 slides
May 10, 2024
Slide 1 of 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
About This Presentation
Python Loops
Size: 547.41 KB
Language: en
Added: May 10, 2024
Slides: 19 pages
Slide Content
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