0% found this document useful (0 votes)
43 views3 pages

WhileandForLoop Notes

Chapter 5 covers Python loops, specifically the while and for loops. The while loop executes statements repeatedly until a condition is false, while the for loop iterates over a sequence. Examples illustrate how to use both loops for tasks like printing messages and generating multiplication tables.

Uploaded by

vidhu.bhis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

WhileandForLoop Notes

Chapter 5 covers Python loops, specifically the while and for loops. The while loop executes statements repeatedly until a condition is false, while the for loop iterates over a sequence. Examples illustrate how to use both loops for tasks like printing messages and generating multiplication tables.

Uploaded by

vidhu.bhis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 5 Python

Python While Loop: is used to execute a block of statements repeatedly until a given
condition is satisfied. When the condition becomes false, the line immediately after the loop
in the program is executed.

Syntax of while loop in Python


while expression:
statement(s)
Flowchart of Python While Loop

Example1: In this example, the condition for while will be True as long as the
counter variable (count) is less than 3.

count = 0
while (count < 3):
count = count + 1
print("Hello")
Output
Hello
Hello
Hello

Example2: Printing the table of 2


num=2
i=1
while i<=10:
print(num, ‘x’, i, ‘=’, num*i)
i=i+1
2x1=2
2x2=4
2x3=6
2x4=8
2x5=10
2x6=12
2x7=14
2x8=16
2x9=18
2x10=20

Python For Loop: The For Loops in Python used for repeated execution of a group of
statements for the desired number of items. For loop is used for iterating over a sequence like
a String, Tuple, List, Set, or Dictionary.

Syntax of For loop in Python


for var in sequence:
# statements

Flowchart of Python For Loop


Example: 1 Python program to display numbers from a list using a for loop.

list = [1,2,4,6,88,125]
for i in list:
print(i)

Output
1
2
4
6
88
125

Example: 2 Python program to print a multiplication table of a given number

given_number = 5
for i in range(11):
print (given_number, " x" , i , " =", 5*i )

Output
5x0=0
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

You might also like