Lesson 8 of 55 · Python
Loops – For
Duration: 20 minutes
Lesson 8 of 55 · Python
Duration: 20 minutes
for loop iterates over any iterable – lists, tuples, strings, ranges, or generators. It abstracts away manual indexing.\n\npython\n# Iterate over a list\nfruits = ['apple', 'banana', 'cherry']\nfor fruit in fruits:\n print(fruit)\n\n# Iterate over a range of numbers\nfor i in range(3):\n print('i =', i)\n\n\nIf you need both the index and the value, use enumerate():\npython\nfor idx, fruit in enumerate(fruits, start=1):\n print(idx, fruit)\n\n