Python Loop

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Python Loops

Lesson 2.5

CHAPTER
2
In This Lesson

While Loop
For Loop
For..Else Loop
Loop Control Statements
Nested Loop
Python Loops

• A loop statement allows to execute a statement or


group of statements multiple times.
• Two commonly used conditional loops: the FOR, WHILE
and NESTED Loops.
While Loop
While Loop
• While Loops repeats a statement or group of
statements while a given condition is TRUE, and
stops if its condition becomes False.
• It tests the condition before executing the loop body.
Syntax

while(boolean expression):
(statement1)
(statement2)
...
(statementN)
Example 1:

i = 1
1
while i <= 5: 2
print(i) 3
4
i += 1
5
Example 2:

i = 1 *
while i <= 5: **
***
print("*" * i) ****
i += 1 *****
Example 3:

x = [2,5,9,13,25]
i = 0 2
while(i<len(x)): 5
print(x[i]) 9
13
i += 1 25

You might also like