Loops in Python
Still in Edition mode
fd
<ref>76</ref>
Contents
Loops and Iterations
Loops are a fundamental concept in programming that allow for the repetition of a set of instructions until a certain condition is met. They help automate repetitive tasks, making the code more efficient, concise, and easier to maintain. For example, if you want to print a list of sustainable fashion brands, instead of writing each print statement separately, you can use a loop to iterate over a list containing those brands and print each one.
For Loop
The for
loop is used to iterate over a sequence, such as a list, tuple, dictionary, set, or string. It is especially useful when you know the number of iterations in advance.
Syntax:
for variable in sequence: # Code block to be executed
Example: Iterating over a list of sustainable fashion brands.
brands = ["Patagonia", "Everlane", "Allbirds", "People Tree"] for brand in brands: print(brand)
Output:
Patagonia Everlane Allbirds People Tree
Iterating Over a List
You can use a for
loop to iterate over each element in a list. This is useful when performing an operation on each item, such as applying a discount to a list of prices.
Example:
prices = [100, 200, 300, 400] discounted_prices = [] for price in prices: discounted_prices.append(price * 0.9) # Apply a 10% discount print(discounted_prices)
Output:
[90.0, 180.0, 270.0, 360.0]
Iterating Over a String
A for
loop can also iterate over the characters in a string, which is useful for tasks such as counting vowels or finding substrings.
Example:
text = "Sustainable Fashion" for char in text: print(char)
Output:
S u s t a i n a b l e F a s h i o n
Iterating with range()
The range()
function is often used with a for
loop to generate a sequence of numbers, especially when the number of iterations is known.
Example:
for i in range(1, 6): print(f"Collection {i}")
Output:
Collection 1 Collection 2 Collection 3 Collection 4 Collection 5
While Loop
The while
loop repeats a block of code as long as a specified condition is true. It is useful when the number of iterations is not known beforehand.
Syntax:
while condition: # Code block to be executed
Example: Printing numbers from 1 to 5.
count = 1 while count <= 5: print(f"Design {count}") count += 1
Output:
Design 1 Design 2 Design 3 Design 4 Design 5
Control Statements: continue
and break
The continue
statement is used to skip the rest of the code inside a loop for the current iteration and move to the next iteration. The break
statement is used to exit the loop prematurely.
Example with continue
:
# Skip printing the brand "Allbirds" brands = ["Patagonia", "Everlane", "Allbirds", "People Tree"] for brand in brands: if brand == "Allbirds": continue print(brand)
Output:
Patagonia Everlane People Tree
Example with break
:
# Stop printing when the brand is "Allbirds" for brand in brands: if brand == "Allbirds": break print(brand)
Output:
Patagonia Everlane
For more details, visit the official Python documentation on <a href="https://docs.python.org/3/tutorial/controlflow.html#for-statements">loops</a> and <a href="https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops">control flow statements</a>.
The author of this entry is Gustavo Rodriguez. AI was used partially in the writing process.