A while loop in Python is used to execute a block of code repeatedly as long as a given condition remains True.
Syntax :
Python
while condition: statement(s) Example :
Python
i = 1 while i <= 5: print(i) i += 1 #output 1 2 3 4 5Infinite While Loop :
Python
i = 1 while i <= 5: print(i) This creates an infinite loop because i never increments.
Using Else in While Loop :
Python
i = 1 while i <= 3: print(i) i += 1 else: print("Loop completed") #output 1 2 3 Loop completed