Python – Introduction to Loops

Loop: A Block of instructions execute repeatedly as long the condition is valid.

Note: Block executes only once whereas Loop executes until condition become False

Python Supports 3 types of Loops:

  1. For Loop
  2. While Loop
  3. While – else Loop

For Loop: We use for loop only when we know the number of repetitions. For example,

  • Print 1 to 10 numbers
  • Print String elements
  • Print Multiplication table
  • Print String character by character in reverse order

While loop: We use while loop when we don’t know the number of repetitions.

  • Display contents of File
  • Display records of Database table

While – else : while-else loop is a type of loop that combines a while loop with an else statement that is executed after the loop has completed. The else block is executed only if the while loop completed normally

Scroll to Top