Loops are used to repeat statement upto n number of times. In Python, we have 2 types of loops: while loop and for loop.
In While Loop gererally we have initialization, condition, and increment/decrement.
i=1 while i<10: print(i) i+=1
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). In For loop, generally we do not need to initialization and increment
nums = [10,20,15,12,25] for n in nums: print(n)
name = 'Ankit' for i in name: print(i)
for i in [10,20,30,15,25]: print(i) for i in range(10): print(i) for i in range(11,21): print(i) for i in range(11,21,2): print(i) for i in range(20,10,-1): print(i)
num=7 for i in range(2,num): if num%i==0: print('Not Prime') break else: print('Prime')
Nested loop means a loop inside another loop.
for i in range(1,6): for j in range(1,6): print("i=",i," j=",j)
i=1 while i<5: j=1 while j<=5: print("hi", i, j) j=j+1 i=i+1