Loops in Python
Loops are a fundamental concept in programming, allowing for the execution of a block of code multiple times. Python provides for and while loops to iterate over a sequence of items. This blog will guide you through their syntax and use cases, enhancing your automation and processing capabilities.
For Loops
The for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, set, or string). It’s more like an iterator method as found in other object-orientated programming languages.
Example:
for i in range(5):
print(i)
This will output the numbers 0 to 4 on separate lines.
While Loops
The while loop in Python allows for repeated execution as long as an expression is true.
Example:
count = 0
while count < 5:
print(count)
count += 1
This will output the numbers 0 to 4 on separate lines, similar to the for loop example.
Nested Loops
You can use one or more loop inside any another for or while loop.
Example:
for i in range(3):
for j in range(2):
print(i, j)
This will output: 0 0 0 1 1 0 1 1 2 0 2 1
Loop Control Statements
Loop control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements:
break: Terminates the loop statement and transfers execution to the statement immediately following the loop.continue: Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.pass: Used when a statement is required syntactically but you do not want any command or code to execute.
Example using break:
for val in "string":
if val == "i":
break
print(val)
Output will be: s t r
By understanding and utilizing these loops, you can make your Python scripts more efficient and concise.
Related Topics
Related Resources
The Ultimate AI Learning Roadmap for Software Engineers (2025 Edition)
The line between 'software engineer' and 'AI engineer' is disappearing. Are you prepared for the shift from deterministic coding to orchestrating intelligent, probabilistic systems? This comprehensive AI learning roadmap is designed specifically for software professionals. It's a practical, timeline based guide to not only learn the necessary skills but also leverage your existing engineering expertise to transition into a high impact AI role, covering everything from mathematical foundations to production grade MLOps and Generative AI.
articleBuild AI Agents from Scratch with Python and Gemini: A Beginner Friendly Guide to Use Cases and Challenges
AI agents are moving beyond simple chatbots, and with Python and Gemini, beginners can now start building useful autonomous workflows faster than ever. This article introduces AI agents in a practical, beginner friendly way and shows how Python and Gemini can be used to create them from scratch. It covers the core building blocks, a simple development path, real world use cases, and the main challenges to watch for when getting started.
articleList Comprehension in Python
Python is known for its simplicity and readability, and one of the features that truly embodies these qualities is list comprehension. If you’re new to Python, list comprehension might seem a bit intimidating at first, but once you get the hang of it, you’ll see how it can make your code more concise and elegant. In this blog, we’ll explore list comprehension, starting with simple examples and gradually moving on to more complex ones. By the end of this guide, you’ll have a solid understanding of how to use list comprehension in your own projects.