📰 IT NachrichtenToday’s NYT Mini Crossword Answers for Saturay, Sept. 12(12.09.2026 um 07:43 Uhr)
🔧 AI Nachrichten Etzioni on AI: What kids tell chatbots, but not you(04.09.2026 um 16:05 Uhr)
🔧 AI Nachrichten OpenAI Wants to Know if an AI Industry Slowdown Would Even Be Legal(11.09.2026 um 01:28 Uhr)
🔧 AI Nachrichten OpenAI puts Pro subscriptions on hold due to Astra demand(10.09.2026 um 22:59 Uhr)
🔧 AI Nachrichten OpenAI’s feud with mathematicians is only escalating(11.09.2026 um 22:57 Uhr)
📰 IT NachrichtenToday’s NYT Mini Crossword Answers for Saturay, Sept. 12(12.09.2026 um 07:43 Uhr)
🔧 AI Nachrichten Etzioni on AI: What kids tell chatbots, but not you(04.09.2026 um 16:05 Uhr)
🔧 AI Nachrichten OpenAI Wants to Know if an AI Industry Slowdown Would Even Be Legal(11.09.2026 um 01:28 Uhr)
🔧 AI Nachrichten OpenAI puts Pro subscriptions on hold due to Astra demand(10.09.2026 um 22:59 Uhr)
🔧 AI Nachrichten OpenAI’s feud with mathematicians is only escalating(11.09.2026 um 22:57 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Basic Loops in Python

↗ Quelle (dev.to)
🗣️ Stimme:

This article explains how to use ‘for’ and ‘while’ statements to create loops in Python, each serving different purposes for repetitive tasks. The article also explores additional control statements such as ‘break,’ ‘continue,’ ‘pass,’ and ‘else’ to manage loop execution.






In Python, the major statements required to create loops are ‘for’ and ‘while’.The for statement is mostly used to iterate over iterable objects (such as a string, tuple, or list). Additionally, like other coding languages (Python Software Foundation (a), n.d.). The ‘while’ loop, on the other hand, is used for repeated execution as long as an expression is true. (Python Software Foundation (b), n.d.).



In other words, both the ‘for’ and the ‘while’ loops are algorithmic, meaning they perform repetitive tasks until a condition is met or a condition remains true. To be more specific, the ‘for’ iterates over sequences executing a set of instructions until a condition is met, for example, until the end of the sequence is reached. In comparison, the ‘while’ will execute a set of instructions as long a condition is true. The loops complement each other and when nested within each other they can be a powerful tool for solving complex problems. This is the main reason Python has more than one loop statement.



The ‘for’ statementThe ‘for’ statement goes through each item in the sequence or iterable, one by one, and executes the block of code for each element. The flow chart below depicts the algorithmic nature of the ‘for’ loop.



Figure 1

The ‘for’ loop



Note: from 4.2 While Loops in Python, by Colorado State University Global (2024b)



A scenario of iterating using a ‘while’ loop as long a condition is true could be similar to the following:




CODE
coffee = 0
homework_num = 100
while coffee < 100:
coffee += 1
print(f"Drinking coffee number {coffee} ...")
if coffee < 100:
print(f"Doing homework number {homework_num }")
homework_num -= 1
if homework_num == 0:
break
else:
print("Rest in peace!")






The ‘break’ will exit the loop. The ‘break’, ‘continue’, ‘pass’, and ‘else’ statements can be used in conjunction with loops to control their execution.




  • The ‘break’ statement is used within loops to exit the loop.

  • The ‘continue’ statement allows the loop to skip the rest of its code block and proceed directly to the next iteration.

  • The ‘pass’ statement acts as a placeholder and does nothing really. It is often used by programmers as a placeholder to bypass blocks of code that are under construction or not yet implemented.

  • The ‘else’ statement executes a block of code after the loop completes normally. In other words, the code within the ‘else’ block runs only if the loop is not terminated by a ‘break’ statement.



For example, the ‘while’ loop example could be rewritten as follows:




CODE
coffee = 0
homework_num = 100
while coffee < 100:
coffee += 1
print(f"Drinking coffee number {coffee} ...")
if coffee < 100:
print(f"Doing homework number {homework_num }")
homework_num -= 1
if homework_num == 0:
break
else:
print("Rest in peace!")






Here the ‘else’ statement is part of the ‘while’ loop, the code within the ‘else’ would be executed if the loop is not terminated by the ‘break’ statement. In this case, the code within the ‘else’ statement will run.



In conclusion, Python’s ‘for’ and ‘while’ loops, along with control statements like ‘break,’ ‘continue,’ ‘pass,’ and ‘else,’ allow for control and flexibility in managing repetitive tasks in programming and creating effective code.






References:



Colorado State University Global (2024a) 4.3 For Loops in Python. Module 4: Python. Repetition. In ITS320: Basic Programming.



Colorado State University Global (2024b) 4.2 While Loops in Python. Module 4: Python. Repetition. In ITS320: Basic Programming.



Python Software Foundation (a). (n.d.). 4. More Control Flow Tools. The Python Tutorial. python.org.






Originally published at Basic Loops in Python - Medium on August 19, 2024.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
Seattle Times sues Microsoft and OpenAI, alleging they trained their AI on its journalism
1 Quelle
Today’s NYT Mini Crossword Answers for Saturay, Sept. 12
1 Quelle
Etzioni on AI: What kids tell chatbots, but not you
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Basic Loops in Python

Thematisch verwandte Begriffe: Basic, Loops, Python · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...