Urgent.News

What's breaking now, across thousands of outlets.

Tech

Python: Loops

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…

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.

A 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.

For 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.

The 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.

In 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.

There 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.

The 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Diesel Likely to Get More Expensive

Dubai crude oil has surged almost $20 above Brent. Dubai crude reached $123.6 per barrel on September 11, compared with … Read More The post Diesel Likely to Get More Expensive appeared first on…

More from Sunday 13 September →