While Loop Examples

##### Example : A Python program to print all the numbers from 1 to 10 using a While loop.
``````i = 1
while i <= 10:
print(i)
i = i + 1

Output:
1
2
3
4
5
6
7
8
9
10

-------------  OR -------------

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

Output:
1 2 3 4 5 6 7 8 9 10

-------------  OR -------------

x=int(input("Enter First Value = "))
y=int(input("Enter Second Value = "))

i = x
while i <= y:
print(i, end=" ")
i = i + 1

Output:
Enter First Value = 15
Enter Second Value = 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ``````
##### Example : A Python program to print all the numbers along with their total from 1 to 10 using a While loop.
``````i = 1
sum = 0
while i <= 10:
print(i, end=" ")
sum = sum + i
i = i + 1
else:
print("\n")
print("The sum total is = ", sum)

Output:
1 2 3 4 5 6 7 8 9 10

The sum total is =  55

----------------  OR  ----------------

i = 1
sum = 0
while i <= 10:
print(i, end=" ")
sum = sum + i
i = i + 1

print("\n")
print("The sum total is = ", sum)

Output:
1 2 3 4 5 6 7 8 9 10

The sum total is =  55``````
##### Example : A Python program to print all the numbers from 1 to 10 except 5 using the Continue statement.
``````i = 0
while i < 10:
i = i + 1
if i == 5:
continue
print(i, end=" ")

Output:
1 2 3 4 6 7 8 9 10 ``````
##### Example : A Python program to print all the numbers before 5 from 1 to 10 using a Break statement.
``````i = 1
while i <= 10:
if i == 5:
break
print(i, end=" ")
i = i + 1

Output:
1 2 3 4 ``````
##### Example : A Python program to receive User Inputs after confirmation.
``````while True:
x = int(input("Enter First nO. = "))
y = int(input("Enter Second nO. = "))
z = x + y
print("The addition result is = ",z)

option = input("To quit, Type N or n otherwise any character = ")
if option == 'n' or option == 'N':
break

Output:
Enter First nO. = 40
Enter Second nO. = 50
The addition result is =  90
To quit, Type N or n otherwise any character = n``````

For Loop Examples

##### Example : A Python program to print all the values using For loop.
``````values = [5, 25, 30, 50, 65]
for x in values:
print(x, end=" ")

Output:
5 25 30 50 65

--------------  OR  ---------------

fruits = ["guava", "orange", "apple", "banana", "mango"]
for i in fruits:
print(i, end=" ")

Output:
guava orange apple banana mango``````
##### Example : A Python program to print all the values along with its total using a For loop.
``````sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
sum = sum + x
print(x, end=" ")
else:
print("\n")
print(sum, end=" ")

Output:
5 25 30 50 65

175

-------------  OR -------------

sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
sum = sum + x
print(x, end=" ")

print("\n")
print(sum, end=" ")

Output:
5 25 30 50 65

175``````
##### Example : A Python program to print some values with a Break statement using a For loop.
``````sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
if x == 50:
break
print(x, end=" ")

Output:
5 25 30                                     ``````
##### Example : A Python program to print some values with a Continue statement using a For loop.
``````sum = 0;
values = [5, 25, 30, 50, 65, 200]
for x in values:
if x == 50:
continue
print(x, end=" ")

Output:
5 25 30 65 200 ``````
##### Example : A Python program to print the number using range() in For loop.
``````for i in range(10):
print(i, end = ' ')

Output:
0 1 2 3 4 5 6 7 8 9

-------------  OR -------------

for i in range(10):
print(i)
Output:
0
1
2
3
4
5
6
7
8
9
-------------  OR -------------

for x in range(1,11):
print(x, end=" ")

Output:
1 2 3 4 5 6 7 8 9 10

-------------  OR -------------

for i in range(3, 10):
print(i, end = ' ')

Output:
3 4 5 6 7 8 9

-------------  OR -------------

for x in range(5, 50, 4):
print(x, end=" ")

Output:
5 9 13 17 21 25 29 33 37 41 45 49

-------------  OR -------------

for i in range(15, 3, -1):
print(i, end = ' ')

Output:
15 14 13 12 11 10 9 8 7 6 5 4

-------------  OR -------------

for i in range(15, 3, -4):
print(i, end = ' ')

Output:15 11 7

-------------  OR -------------

for i in range(1, 5):
for j in range(i):
print(i, end=' ')
print()

Output:
1
2 2
3 3 3
4 4 4 4

-------------  OR -------------

for str in 'Welcome':
if str == 'l' or str == 'e' :
continue
print(str, end='')

Output:
Wcom

-------------  OR -------------

for str in 'Hello India':
pass
print(str, end='')

Output:
a

NB: 'pass' statements skip all letters except last letter.``````