Introduction
Loops help us repeat actions many times without writing the same code again and again. They save time and make programs shorter and easier to read.
Jump into concepts and practice - no test required
Loops help us repeat actions many times without writing the same code again and again. They save time and make programs shorter and easier to read.
for item in collection: # do something with item while condition: # do something while condition is true
for loop repeats for each item in a list or group.
while loop repeats as long as a condition stays true.
for number in range(5): print(number)
count = 0 while count < 3: print('Hello') count += 1
This program uses a for loop to say what fruits we like, one by one.
fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(f'I like {fruit}')
Loops help avoid repeating code and make programs easier to change.
Be careful with while loops to make sure they stop eventually, or the program will run forever.
Loops repeat actions to save time and code.
Use for loops to go through items in a list.
Use while loops to repeat while a condition is true.
for i in range(3):
print(i)i = 0
while i < 3
print(i)
i += 1