Python Programming for Complete Beginners: A Structured Course

Lesson 4: For & While Loops

Objective: Automate repetitive tasks.

  • for loops (for i in range(5)).

  • while loops (while x < 10).

Example

< for loop >

Print each fruit in a fruit list:

fruits = [“apple”“banana”“cherry”]
for x in fruits:
  print(x)
 
< While loop >

Print i as long as i is less than 6:

i = 1
while i < 6:
print(i)
  i += 1
 
< break loop>
fruits = [“apple”“banana”“cherry”]
for x in fruits:
  if x == “banana”:
    break
  print(x)
 

Exercise:

  • Print numbers 1 to 10 using a loop.