0% found this document useful (0 votes)
13 views

Whileloop - Jupyter Notebook

Uploaded by

Mamata swain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Whileloop - Jupyter Notebook

Uploaded by

Mamata swain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8/5/23, 6:19 PM whileloop - Jupyter Notebook

Loop: Something that repeats continuously.

While Loop
Python while loop is used to run a block code until a certain condition is met.

The syntax of while loop is:

initialization
while condition:
#body of while
increment/decrement

1. A while loop evaluates the condition


2. If the condition evaluates to True, the code inside the while loop is exe
cuted.
3. condition is evaluated again.
4. This process continues until the condition is False.
5. When condition evaluates to False, the loop stops.

localhost:8888/notebooks/Desktop/python july/whileloop.ipynb 1/4


8/5/23, 6:19 PM whileloop - Jupyter Notebook

In [1]:

1 i = 1 #initiallization
2 while i<6:
3 print(i)
4 i += 1

1
2
3
4
5

In [3]:

1 i = 100
2 while i>=1:
3 print(i,end=',')
4 i -= 1

100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,7
6,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,
51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,2
7,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,

In [5]:

1 i = 1
2 while i<=100:
3 print(i,end=" ")
4 i += 2

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 5
3 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

localhost:8888/notebooks/Desktop/python july/whileloop.ipynb 2/4


8/5/23, 6:19 PM whileloop - Jupyter Notebook

In [7]:

1 i = 100
2 while i>=2:
3 print(i,end=' ')
4 i -= 2

100 98 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 5
2 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2

Wapp to print all the numbers that are divisible by 5 between 100-200 in reverse.

In [5]:

1 i = 200
2 while i >= 100:
3 if i%5==0:
4 print(i,end=' ')
5 i -= 1

200 195 190 185 180 175 170 165 160 155 150 145 140 135 130 125 120 115 11
0 105 100

In [ ]:

Infinite while Loop in Python

If the condition of a loop is always True, the loop runs for infinite times (unt
il the memory is full). For example,

This is aa infinity loop

a = 1
while True:
print(a)
a += 1

localhost:8888/notebooks/Desktop/python july/whileloop.ipynb 3/4


8/5/23, 6:19 PM whileloop - Jupyter Notebook

Q. Wapp to print table of enter number using while loop.

In [8]:

1 num = int(input("Enter a number : "))


2 i = 1
3 while i<11:
4 print(num,'X',i,'=',num*i)
5 i += 1

Enter a number : 6
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60

localhost:8888/notebooks/Desktop/python july/whileloop.ipynb 4/4

You might also like