Loop (1) .HTML
Loop (1) .HTML
'''
Range():
Range function used to generate sequence of numbers with in a given range.
Range function return type is sequence of numbers.
'''
values=range(10)
print(values)
range(0, 10)
In [4]:
#By default sequence starts at 0, increments by 1.
# iterating through the sequence
values=range(10)
for i in values:
print(i,end=" ")
0 1 2 3 4 5 6 7 8 9
In [5]:
### Here end is predeined parameter in print function.That controls what charters are pr
values=range(10)
for i in values:
print(i,end="python ")
0python 1python 2python 3python 4python 5python 6python 7python 8python 9python
In [6]:
# create a sequence from 0 to 10 (10 is not included)
#in python lower bound or starting postion included.upperbound is excluded
values= range(10)
# convert to list and print it
print(list(values))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [ ]:
'''
Loops are iterative statements.to execute some block of statements repeatedly we use loo
1.for loop
2.while loop
importat points in loops:
1.initialization
2.condition:
3.updation
syntax:
for loop:
In Python, we use a for loop to iterate over sequences [Like lists, strings, dictionari
syntax:
for variable in sequence:
# executable statements
In [8]:
lst=[1,2,3,4,5]
for i in lst:
print(i,end=" ")
1 2 3 4 5
In [7]:
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
books=['raamayanam', 'mahabharatam', 'Bible','Quran']
# Start of loop
for book in books:
print(book)
raamayanam
mahabharatam
Bible
Quran
In [9]:
#The range() function is used in for loop to iterate the loop a certain number of times
# iterate the loop 10 times
for i in range(10):
print(i,end=" ")
0 1 2 3 4 5 6 7 8 9
In [10]:
for i in range(10,20):#step size not specified by detault step size is 1.
print(i,end=" ")
10 11 12 13 14 15 16 17 18 19
In [12]:
for i in range(10,20,2):
print(i,end=" ")
10 12 14 16 18
In [14]:
#find 12 mutiples up to 18
a,b=map(int,input().split())
for i in range(1,b+1,1):
print(a*i,end=" ")
12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 192 204 216
In [15]:
#find 18 mutiples up to 12
a,b=map(int,input().split())
for i in range(1,a+1,1):
print(b*i,end=" ")
In [17]:
# print even numbers betwwen 101 to 200
a,b=map(int,input().split())
for i in range(a,b+1,1):
if i%2==0:
print(i,end=" ")
102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144
146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188
190 192 194 196 198 200
In [18]:
#find factors of the given number:
n=int(input())
for i in range(1,n+1,1):
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
if n%i==0:
print(i,end=" ")
1 2 5 10
In [19]:
#find factors count of the given number:
n=int(input())
count=0
for i in range(1,n+1,1):
if n%i==0:
count+=1
print(count)
In [20]:
#prime number:
n=int(input())
count=0
for i in range(1,n+1,1):
if n%i==0:
count+=1
if count==2:
print("prime")
else:
print("not prime")
prime
In [1]:
'''
Python supports a else block to be associated with a for loop.
If a else block is used with a for loop, it is executed only when the for loop terminate
'''
for i in range(1,11,1):
print(i,end=" ")
else:
print("for loop terminated normally.so else block also executed")
In [2]:
#The for loop terminates encountering a break statement, this time else block never exe
for i in range(1,11,1):
if i==5:
break
print(i,end=" ")
else:
print("loop is terminated break appears..so else block not executed")
1 2 3 4
In [ ]:
while loop:
is used to exexcute bloc k of statements repeatedly until specified condition is Tru
syntax:
while condition:
//body of the loop
(or)
while(condition):
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
//body of the loop
In [3]:
# print 1 to 10 numbers using while loop
i=1
while i<=10:
print(i,end=" ")
i=i+1
1 2 3 4 5 6 7 8 9 10
In [ ]:
#find the digit count of the given number
#find factorial of the given number
#find the lcm of the given two numbers
#generate finocii sequence up to given range:
input:5
output:0 1 1 2 3
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF