0% found this document useful (0 votes)
2 views5 pages

Python While Loop Examples

The document contains a series of Python code snippets demonstrating various uses of while loops, including counting, reversing numbers, summing digits, and printing patterns. Each snippet illustrates different loop behaviors, such as conditional statements, nested loops, and the use of else with loops. The examples cover a range of programming concepts suitable for beginners to understand loop mechanics in Python.

Uploaded by

jasvendrajassi
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)
2 views5 pages

Python While Loop Examples

The document contains a series of Python code snippets demonstrating various uses of while loops, including counting, reversing numbers, summing digits, and printing patterns. Each snippet illustrates different loop behaviors, such as conditional statements, nested loops, and the use of else with loops. The examples cover a range of programming concepts suitable for beginners to understand loop mechanics in Python.

Uploaded by

jasvendrajassi
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

1.

i=1
while i <= 5:
print(i, end=" ")
i += 1
2.

i = 10
while i > 0:
print(i, end=" ")
i -= 2

3.

i=1
while i <= 10:
if i % 2 == 0:
print(i, end=" ")
i += 1

4.

num = 123
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num = num // 10
print(rev)

5.

i=5
while i > 0:
print(i, end=" ")
i -= 1
else:
print("Loop ended")
6.

i=1
while i <= 5:
print(i * i, end=" ")
i += 1

7.

i=1
while i <= 10:
if i == 5:
print("Found 5")
i += 1
else:
print("Loop completed")

8.

num = 28
sum_digits = 0
while num > 0:
sum_digits += num % 10
num = num // 10
print(sum_digits)

9.

i=0
while i < 5:
print("*" * i)
i += 1
10.

i=1
while i <= 3:
j=1
while j <= 3:
print(i * j, end=" ")
j += 1
print()
i += 1

11.

i=1
while i <= 5:
if i == 3:
print("Skipping 3")
print(i, end=" ")
i += 1
else:
print("Loop ended")

12.

num = 145
sum_fact = 0
while num > 0:
digit = num % 10
fact = 1
while digit > 0:
fact *= digit
digit -= 1
sum_fact += fact
num = num // 10
print(sum_fact)
13.

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

14.

i=1
while i <= 5:
j=1
while j <= i:
print(j, end=" ")
j += 1
print()
i += 1

15.

i=1
while i <= 5:
if i == 3:
print("Middle of the loop")
print(i, end=" ")
i += 1
else:
print("Loop completed")

16.

i = 10
while i < 5:
print(i, end=" ")
i += 1
else:
print("Loop never runs")
17.

i=5
while i > 0:
print(i, end=" ")
i -= 1
else:
print("Loop ended")

18.

i=1
while i <= 5:
if i == 6:
print("This won't print")
print(i, end=" ")
i += 1
else:
print("Loop completed")

19.

i=1
while i <= 5:
print(i, end=" ")
i += 2
else:
print("Loop ended")

20.

i=1
while i <= 5:
if i == 3:
print("Reached 3")
print(i, end=" ")
i += 1
else:
print("Loop completed")

You might also like