{
  "id": 7091586,
  "title": "Python: Loops",
  "url": "https://urgent.news/2026/09/13/python-loops",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-13T09:44:34.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/maryngure/python-loops-29k8"
  },
  "original_language": "en",
  "account": "Computers excel at repetitive tasks. Printing numbers from 1 to 100 or processing scores of 50 students manually would be arduous for a human. Python simplifies this through loops, which enable a block of code to be executed multiple times without repeating it multiple times.\n\nA loop instructs Python to continue a sequence of actions until a certain condition is met. For instance, writing out numbers manually can be replaced by a loop. Instead of printing each number individually, a loop such as \"for number in range (1, 6): print(number)\" can be used. This prints the numbers 1 through 5 in a more efficient manner.\n\nFor loops are particularly useful when dealing with a known number of items, such as strings, lists, or other collections of data. These loops repeat a fixed number of times. For example, a list of names can be iterated through with a for loop. For each name in the list, the loop temporarily stores the name in a variable (often referred to as 'name' in Python) and then executes the indented code for each iteration.\n\nThe range() function is especially beneficial when working with numbers in for loops. It generates a sequence of numbers starting from a specified number and ending just before another specified number. For example, \"for number in range(1, 6): print(number)\" prints the numbers 1 through 5. However, it's important to remember that the ending number in the range() function is not included in the output.\n\nIn contrast to for loops, which iterate over a known sequence of items, while loops continue running as long as a specific condition remains true. For example, \"count = 1 while count <= 5: print(count); count += 1\" prints the numbers 1 through 5. The line \"count += 1\" is crucial, as it modifies the value of 'count' each time the loop repeats. Without this action, the condition would remain true, and an infinite loop would be created.\n\nThere are two key keywords used to control loops: break and continue. The break statement immediately halts the loop, while the continue statement tells Python to skip the current iteration and move on to the next one. For example, \"for number in range(1, 6): if number == 3: continue; print(number)\" prints the numbers 1, 2, 4, and 5, skipping the number 3.\n\nThe enumerate() function provides a way to access both the index (or position) and the value of each item in a list while looping through it. For instance, \"for index, name in enumerate(names): print(index, name)\" prints the index and name of each item in the 'names' list. These concepts provide powerful tools for programmers, allowing them to easily perform repetitive tasks and manage sequences of data in Python.",
  "summary": "One thing I've started noticing as I learn Python is that computers are really good at doing repetitive tasks. Imagine being asked to print the numbers from 1 to 100 manually. Or process the scores of 50 students one by one. That would be exhausting for a human. For Python, however, repeating a task is exactly what loops are designed for. Loops allow us to run a block of code multiple times…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}