Introduction
In programming, many situations require performing the same task multiple times. Writing the same code repeatedly is inefficient and difficult to maintain.
Python solves this problem using Loops.
Loops allow a block of code to run repeatedly until a condition becomes False or a sequence is completed.
Loops are extremely important in Artificial Intelligence, Data Science, Machine Learning, Automation, and software development because they help process large amounts of data efficiently.
In this tutorial, you will learn Python loops, different types of loops, loop control statements, nested loops, and practical applications.
Learning Objectives
- Understand loops in Python.
- Learn for loops.
- Understand while loops.
- Use nested loops.
- Understand break and continue statements.
- Build repetitive programs efficiently.
What are Loops in Python?
Loops are control structures used to repeat a block of code multiple times.
Instead of writing repetitive code manually, a loop automatically executes instructions repeatedly.
Example without loops:
print("Python")
print("Python")
print("Python")
Using a loop:
for i in range(3):
print("Python")
Output:
Python
Python
Python
Why Loops are Important
Loops are important because they save time, reduce code repetition, and simplify programming logic.
Loops are widely used in:
- Data Processing
- Machine Learning Training
- Data Cleaning
- Automation Scripts
- Game Development
- Web Applications
- Artificial Intelligence Models
Types of Loops in Python
Python mainly provides two types of loops:
- for loop
- while loop
1. The for Loop
The for loop is used for iterating through sequences.
A sequence can be:
- List
- Tuple
- String
- Dictionary
- Range of numbers
Syntax
for variable in sequence:
statement
Example Using Range
for i in range(5):
print(i)
Output:
0
1
2
3
4
Using range() Function
The range() function generates a sequence of numbers.
Single Parameter
for i in range(5):
print(i)
Output:
0 1 2 3 4
Start and End Parameters
for i in range(1,6):
print(i)
Output:
1
2
3
4
5
Step Parameter
for i in range(1,11,2):
print(i)
Output:
1
3
5
7
9
Looping Through Strings
A for loop can iterate through characters in a string.
word = "Python"
for letter in word:
print(letter)
Output:
P
y
t
h
o
n
Looping Through Lists
Lists are commonly used with loops.
fruits = ["Apple","Banana","Orange"]
for item in fruits:
print(item)
Output:
Apple
Banana
Orange
2. The while Loop
The while loop repeats code while a condition remains True.
Syntax
while condition:
statement
Example
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Infinite Loops
If the loop condition never becomes False, the loop runs forever.
This is called an Infinite Loop.
Example:
while True:
print("Running")
This loop never stops.
Nested Loops
A nested loop means placing one loop inside another loop.
Example
for i in range(3):
for j in range(2):
print(i,j)
Output:
0 0
0 1
1 0
1 1
2 0
2 1
Loop Control Statements
Python provides loop control statements to modify loop behavior.
- break
- continue
- pass
1. break Statement
The break statement immediately exits the loop.
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
2. continue Statement
The continue statement skips the current iteration.
for i in range(6):
if i == 3:
continue
print(i)
Output:
0
1
2
4
5
3. pass Statement
The pass statement is a placeholder that does nothing.
for i in range(3):
pass
Loops in Artificial Intelligence
Loops are extremely important in Artificial Intelligence and Machine Learning.
They are used for:
- Model Training
- Data Processing
- Feature Engineering
- Prediction Iteration
- Algorithm Execution
AI models often process thousands or millions of records using loops.
Real-World Examples
Multiplication Table
number = 5
for i in range(1,11):
print(number * i)
Password Validation Example
attempts = 1
while attempts <= 3:
print("Try Login")
attempts += 1
Python Example
numbers = [10,20,30,40]
for value in numbers:
print(value)
Output:
10
20
30
40
Interview Questions
1. What are loops in Python?
Loops are used to repeat a block of code multiple times.
2. Name two main loops in Python.
for loop and while loop.
3. What does the break statement do?
It immediately exits the loop.
4. What does continue do?
It skips the current iteration.
Assignment
- Create a for loop printing numbers from 1 to 10.
- Create a while loop printing even numbers.
- Print characters of a string using a loop.
- Use break in a loop example.
- Create a multiplication table program.
Quiz
Q1. Which loop iterates through sequences?
- A. while
- B. for
- C. break
- D. pass
Answer: B. for
Q2. Which statement exits a loop immediately?
- A. continue
- B. pass
- C. break
- D. import
Answer: C. break
Q3. Which loop runs while a condition is True?
- A. while
- B. for
- C. def
- D. class
Answer: A. while
Summary
In this tutorial, you learned Loops in Python. You explored for loops, while loops, nested loops, and loop control statements including break, continue, and pass.
Loops help automate repetitive tasks and are essential for Artificial Intelligence, Machine Learning, Data Science, and software development.
Next Tutorial
Tutorial 17: Functions in Python
```
