Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
Python - Variable declarations
Conditional Statements in Python
Loops in Python
Python Functions
Python Classes
OOPS in Python
Python - Hello World Application
Python - import statements
Python - Web API

Loops in Python



Loops in Python allow you to repeatedly execute a block of code as long as a certain condition is met. There are two main types of loops in Python: for loops and while loops. Let's go through each of them with examples:

1. for Loop

A for loop is used to iterate over a sequence (such as a list, tuple, dictionary, set, or string) and execute a block of code for each element in the sequence.

Basic for Loop Example

python
# List of numbers
numbers = [1, 2, 3, 4, 5]

# Using a for loop to iterate through the list
for number in numbers:
    print(number)

Output:

1
2
3
4
5

Using for Loop with range()

The range() function generates a sequence of numbers, which can be useful for iterating a specific number of times.

python
# Using range to generate numbers from 0 to 4
for i in range(5):
    print(i)

Output:

0
1
2
3
4

2. while Loop

A while loop repeatedly executes a block of code as long as the specified condition is True.

Basic while Loop Example

python
# Initialize a variable
count = 1

# Using a while loop to iterate until the count is less than or equal to 5
while count <= 5:
    print(count)
    count += 1  # Increment the count by 1

Output:

1
2
3
4
5

Using Break and Continue

Break Statement

The break statement is used to exit the loop prematurely when a certain condition is met.

python
for number in range(1, 10):
    if number == 5:
        break  # Exit the loop when number is 5
    print(number)

Output:

1
2
3
4

Continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop.

python
for number in range(1, 10):
    if number % 2 == 0:
        continue  # Skip even numbers
    print(number)

Output:

1
3
5
7
9

Nested Loops

You can use loops inside other loops (nested loops) to perform more complex iterations.

Nested for Loop Example

python
# Nested loop to print a multiplication table
for i in range(1, 4):
    for j in range(1, 4):
        print(f"{i} * {j} = {i * j}")

Output:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9

Practical Example

Here's a practical example that combines loops and conditions:

python
# Calculate the sum of all odd numbers from 1 to 10
total_sum = 0
for number in range(1, 11):
    if number % 2 != 0:
        total_sum += number

print(f"The sum of all odd numbers from 1 to 10 is: {total_sum}")

Output:

The sum of all odd numbers from 1 to 10 is: 25

Loops are a fundamental part of Python that allow you to automate repetitive tasks and handle sequences of data efficiently. If you have any specific questions or need further examples, feel free to ask! ??




All rights reserved | Privacy Policy | Sitemap