Loop Statements
Loop Statements
i = 0
while ( i<=20):
print(i)
i = i+2
print("End of while loop")
0
2
4
6
8
10
12
14
16
18
20
End of while loop
2
3
4
5
6
7
8
10
11
12
Program over
2
3
4
5
6
Program over
In [9]: number = 0
1
2
3
4
Your think is important
5
6
7
8
9
10
Out of loop
Laptop
Book
Book
Book
Laptop
Book
Book
Book
Laptop
Book
Book
Book
In [ ]:
In [ ]:
In [ ]: #we want to print line by line , in for and while loop we can excecute the program
print("Hello")
print("It's ok")
print("Great thinking")
In [ ]: i = 0
while i<5:
print(i)
i+=1
In [ ]: 153,370,371,407
In [1]: x = [10,"Sravan",10.25]
print(x)
In [3]: for i in x:
print(x)
In [3]: a = [23,45,66,77,66]
for i in a:
print(i)
23
45
66
77
66
In [6]: x = [34,45,67,878,344,232,23,57]
print(x)
In [8]: x = [34,45,67,878,344,232,23,57]
for i in x:
print(i)
print(type(x))
34
<class 'list'>
45
<class 'list'>
67
<class 'list'>
878
<class 'list'>
344
<class 'list'>
232
<class 'list'>
23
<class 'list'>
57
<class 'list'>
0
2
4
6
8
10
12
14
16
18
20
End of the loop
In [5]: x = [32,232,343,45,4646]
for i in x:
print(i)
32
232
343
45
4646
1
6
11
16
21
26
31
36
41
46
51
56
61
66
71
76
81
86
91
96
1
2
3
4
6
7
8
Program over
In [ ]:
i = 2
2
4
6
8
10
12
14
16
18
20
End of the loop
0
2
4
6
8
10
12
14
16
18
In [10]: z = [232,2432,343,38,788,545]
for i in z:
print(i)
232
2432
343
38
788
545
count = 0
for i in range(2,a):
if(a%i==0):
count += 1
if count==0:
print("Prime number")
else:
print("It's not prime")
num = 153
sum = 0
temp = num
while (temp >0):
digit = temp%10
sum += digit**3
temp //= 10
if num == sum:
print("it is an Armstrong")
else:
print("it is not Armstrong")
it is an Armstrong
num = 1221
rev = 0
temp = num
while num >0:
rem = temp % 10
rev = rev*10+rem
temp = temp//10
if rev == temp:
print("it is an palindrome")
else:
print("it is not palindrome")
it is an palindrome
a = 0
b = 1
i = 1
value = int(input("Enter the range value :"))
x =[0,1]
while(i<value-2):
result = a+b
x.append(result)
a = b
b = result
i = i+1
print(x)
Enter number 6
The given number is perfect
In [6]: x = [23,22,5223,324124,24141,241,233412]
x.append(44)
print(x)
Hello world!
In [7]: print("Hello","world",sep="_",end="!")
Hello_world!
a*b*c
a b c!
In [ ]:
In [ ]: