0% found this document useful (0 votes)
2 views2 pages

For loop and while loop

The document explains the two main types of loops in Python: For Loops and While Loops. It provides syntax and examples for each type, including how to iterate over sequences and control loop flow using statements like break and continue. Additionally, it offers examples demonstrating the use of these control statements.

Uploaded by

Ravi N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

For loop and while loop

The document explains the two main types of loops in Python: For Loops and While Loops. It provides syntax and examples for each type, including how to iterate over sequences and control loop flow using statements like break and continue. Additionally, it offers examples demonstrating the use of these control statements.

Uploaded by

Ravi N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In Python, loops are used to execute a block of code repeatedly.

The two main types of loops


are:

🔹 1. For Loop

Used to iterate over a sequence (like a list, tuple, string, or range).

✅ Syntax:

python
CopyEdit
for variable in sequence:
# code block

✅ Example 1: Using range()

python
CopyEdit
for i in range(5):
print(i)
# Output: 0 1 2 3 4

✅ Example 2: Iterating over a list

python
CopyEdit
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

🔹 2. While Loop

Repeats a block of code as long as a condition is True.

✅ Syntax:

python
CopyEdit
while condition:
# code block

✅ Example:

python
CopyEdit
count = 0
while count < 5:
print(count)
count += 1
# Output: 0 1 2 3 4
🔸 Loop Control Statements

These are used to control the flow inside loops:

Statement Description
break Exits the loop immediately
continue Skips the current iteration
pass Does nothing (placeholder)

✅ Example using break:

python
CopyEdit
for i in range(5):
if i == 3:
break
print(i)
# Output: 0 1 2

✅ Example using continue:

python
CopyEdit
for i in range(5):
if i == 3:
continue
print(i)
# Output: 0 1 2 4

Let me know if you'd like this content converted into a downloadable PDF once the tools are
working again.

You might also like