Loops - Python Concepts
Loops are one of the most important building blocks in programming. They allow us to repeat tasks efficiently without writing the same code multiple times. In Python, loops help automate repetitive operations, making our code shorter, cleaner, and easier to maintain.
🔹 What are Loops in Python?
A loop is a programming structure that repeats a block of code until a certain condition is met. Instead of manually executing the same statement multiple times, loops handle repetition automatically.
✅ Uses of Loops
- Automating repetitive tasks
- Processing items in a collection (like lists, dictionaries, or strings)
- Performing mathematical calculations repeatedly
- Reading files line by line
- Creating patterns and handling complex data structures
🔹 While Loop in Python
The while loop runs as long as a given condition is True
. It is commonly used when the number of iterations is not known in advance.
📌 Syntax:
while condition:
# code block
📌 Example:
count = 1
while count <= 5:
print("Count is:", count)
count += 1
🔹 For Loop in Python
The for loop is used to iterate over a sequence of items (like lists, tuples, or strings). It is especially useful when the number of iterations is known.
📌 Syntax:
for variable in sequence:
# code block
📌 Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
🔹 Break Statement
The break statement is used to exit a loop immediately, even if the condition is still true.
📌 Example:
for i in range(10):
if i == 5:
break
print(i)
👉 This will stop the loop when i
reaches 5.
🔹 Continue Statement
The continue statement skips the current iteration and moves to the next one.
📌 Example:
for i in range(6):
if i == 3:
continue
print(i)
👉 This will print all numbers except 3.
🔹 Looping Through Strings
Strings are sequences, so you can loop through them using for
loops.
📌 Example:
for letter in "Python":
print(letter)
🔹 Range Function
The range() function generates a sequence of numbers, often used with loops.
📌 Example:
for i in range(1, 6):
print(i)
👉 Prints numbers from 1 to 5.
🔹 Else in Loops
Python allows an else
block after a loop. It executes only when the loop completes without a break
.
📌 Example:
for i in range(5):
print(i)
else:
print("Loop completed successfully!")
🔹 Pass Statement
The pass statement is used when a statement is required syntactically, but you don’t want to execute any code.
📌 Example:
for i in range(5):
if i == 3:
pass
print(i)
🔹 Nested Loops
A nested loop is a loop inside another loop. It is useful for working with multi-dimensional data structures.
📌 Example:
for i in range(3):
for j in range(2):
print("i =", i, "j =", j)
🔹 FAQs on Python Loops
1. What is the difference between while and for loops?
While loops are used when the number of iterations is unknown, while for loops are used when iterating over a sequence or range.
2. Can loops be infinite in Python?
Yes, if the condition never becomes false (in while loops) or if you use while True
without a break.
3. Can we use break and continue together?
Yes, break stops the loop, and continue skips the current iteration.
4. Is else in loops mandatory?
No, it is optional and only runs when loops complete naturally.
🔹 Python Loop Exercises
- Write a while loop to print numbers from 1 to 10.
- Write a for loop that prints all even numbers between 1 and 20.
- Write a program using break to stop at number 7 in a range of 1–15.
- Write a loop to count the number of vowels in a string.
- Write a nested loop to print a multiplication table (1–5).
🔹 Final Thoughts
Loops in Python are powerful tools for handling repetitive tasks. By mastering while loops, for loops, break, continue, nested loops, range, and else in loops, you’ll be able to write efficient and professional-level code. Keep practicing the exercises, and soon you’ll be comfortable using loops in real-world projects.
0 Comments