0% found this document useful (0 votes)
9 views1 page

Loop Statements

The document contains examples of Python code demonstrating the use of various loops (while, for), conditional statements (if-else), lists, input/output, pattern printing and other basic Python programming concepts. Multiple code cells show how to iterate over lists, print patterns, check for prime and other special numbers, take user input and more through examples, step-by-step.
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)
9 views1 page

Loop Statements

The document contains examples of Python code demonstrating the use of various loops (while, for), conditional statements (if-else), lists, input/output, pattern printing and other basic Python programming concepts. Multiple code cells show how to iterate over lists, print patterns, check for prime and other special numbers, take user input and more through examples, step-by-step.
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
You are on page 1/ 1

In [1]: #Even no's upto 20

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

In [11]: for i in range(2,13):


if i == 9:
continue
print(i)
print("Program over")

2
3
4
5
6
7
8
10
11
12
Program over

In [13]: for i in range(2,13):


if i == 7:
break
print(i)
print("Program over")

2
3
4
5
6
Program over

In [9]: number = 0

for number in range(10):


number = number+1
if number == 5:
print("Your think is important")
pass
print(number)
print("Out of loop")

1
2
3
4
Your think is important
5
6
7
8
9
10
Out of loop

In [3]: #Nested loop


i = 1
while i<=3:
print("Laptop")
j =1
while j<=3:
print("Book")
j+=1
i+=1

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)

[10, 'Sravan', 10.25]

In [3]: for i in x:
print(x)

[10, 'Sravan', 10.25]


[10, 'Sravan', 10.25]
[10, 'Sravan', 10.25]

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)

[34, 45, 67, 878, 344, 232, 23, 57]

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'>

In [10]: #Even no's upto 20


i = 0
while (i<=20):
print(i)
i = i+2
else:
print("End of the loop")

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

In [8]: #Even no's upto 20


for i in range(1,100,5):
print(i)

1
6
11
16
21
26
31
36
41
46
51
56
61
66
71
76
81
86
91
96

In [18]: n = int(input("Enter the number"))


while n >0:
r = n%10
print(r,end = " ")
n = n//10

Enter the number56


6 5

In [3]: for i in range(1,9):


if i==5:
continue
print(i)
print("Program over")

1
2
3
4
6
7
8
Program over

In [ ]:

In [2]: #Ex Display Even no's upto 20

i = 2

while (i <= 20):


print(i)
i = i+2
print("End of the loop")

2
4
6
8
10
12
14
16
18
20
End of the loop

In [3]: for i in range(0,20,2):


print(i)

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

In [31]: #Prime number

a = int(input("Enter the number :"))

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")

Enter the number :5


Prime number

In [16]: #program on find the reverse of the given number Armstrong

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

In [ ]: #program on find the reverse of the given number palindrome

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

In [1]: #Fabonaci series

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 the range value :10


[0, 1, 1, 2, 3, 5, 8, 13, 21]

In [3]: #Factorial number

n = int(input("Enter input number :"))


fact = 1
if n < 0:
print("Factorial does not exist for negative numbers")
elif n == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, n+1):
fact = fact * i
print("The factorial of",n,"is",fact)

Enter input number :10


The factorial of 10 is 3628800

In [2]: #Number is perfect or not

number = int(input("Enter number "))


sum = 0
i = 1
while(i<=number //2):
if(number % i==0):
sum = sum+i
i = i+1
if(sum==number):
print("The given number is perfect")
else:
print("The given number is not perfect")

Enter number 6
The given number is perfect

In [3]: #Strong number


sum = 0
num =int(input("Enter a number :"))
temp = num
while(num>0):
i=1
fact=1
rem=num%10
while(i<=rem):
fact = fact*i
i = i+1
sum = sum+fact
num = num//10
if(sum == temp):
print("Given number is a strong number")
else:
print("Given number is not a strong number")

Enter a number :145


Given number is a strong number

In [20]: #Pattern program


rows=int(input("Enter any rows"))
for i in range(1,rows+1):
print(i,end =" ")

Enter any rows5


1 2 3 4 5

In [25]: rows=int(input("Enter any rows"))


for i in range(1,rows+1):
print("*",end =" ")

Enter any rows5


* * * * *

In [8]: #Pattern program


rows=int(input("Enter any rows"))
for i in range(1,rows+1):
for j in range(1,i+1):
print(i,end=" ")
print()

Enter any rows5


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

In [23]: #Pattern program


rows=int(input("Enter any rows"))
for i in range(1,rows+1):
for j in range(1,i+1):
print(j,end=" ")
print()

Enter any rows5


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

In [24]: #Pattern program


rows=int(input("Enter any rows"))
for i in range(1,rows+1):
for j in range(1,i+1):
print("*",end=" ")
print()

Enter any rows5


*
* *
* * *
* * * *
* * * * *

In [6]: x = [23,22,5223,324124,24141,241,233412]
x.append(44)
print(x)

[23, 22, 5223, 324124, 24141, 241, 233412, 44]

In [4]: rows=int(input("Enter any rows"))


for i in range(1,rows+1):
print(i,end=" ")
print()

Enter any rows5


1 2 3 4 5

In [5]: print("learning is","Importants",sep=" plus ")

learning is plus Importants

In [6]: print("Hello","world",end ="!")

Hello world!

In [7]: print("Hello","world",sep="_",end="!")

Hello_world!

In [8]: print("a","b","c",sep = "*")

a*b*c

In [30]: print("a","b","c",end = "!")

a b c!

In [ ]:

In [9]: rows = int(input("Enter the number :"))


for i in range(rows,i+1):
print(i)

Enter the number :5

In [ ]:

You might also like