Python Loops Explained

Loop or Iterative statements are decision control statements that are used to repeat the execution of a list of statements. In Python, two types of loops are supported.

  • while loop
  • for loop

1. while loop: while loop provides a mechanism to repeat one or more statements while a particular condition is TRUE. In the While loop, the condition is tested before any of the statements in the statements block is executed, if the condition is TRUE then the statements will be executed otherwise if the condition is FALSE, the control will jump to another statement. It has the following syntax:

statement x
while(test condition):
statement
statement y

2. for loop: for loop provides a mechanism to repeat a task until a particular condition is TRUE. For loop is also known as a definite loop because this loop will repeat many times. range() function used in a for loop to iterate over a sequence of numbers. It has the following syntax:

for variable in range(initial range value, terminate range value)
statement
or
for variable in range(beg, end, step)
statement

Example:

for i in range(1,10)
fact=fact*i
print(fact)