Lecture9 # Understanding Loops in Python

Lecture9 # Understanding Loops in Python

Loops are a powerful programming concept in Python that allows you to repeat a set of instructions multiple times. They help automate tasks, process large amounts of data, and make your code more efficient. In this beginner-friendly guide, we will explore the two main types of loops in Python: for loops and while loops.

My Python codes: https://masterwithhamza@bitbucket.org/hamxaflutterapps/mypythoncodes.git

for Loop

A for loop is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a block of code for each item in that sequence. Here's the basic structure of a for loop:

for variable in sequence:
    # Code to be executed for each item in the sequence

Example:

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)

In this example, the for loop iterates through the list of fruits and prints each fruit's name.

Example:

for i in range(5):
    print(i)

This for loop prints the numbers from 0 to 4 because range(5) generates a sequence of numbers from 0 to 4.

while Loop

A while loop repeatedly executes a block of code as long as a condition remains True. Here's the basic structure of a while loop:

while condition:
    # Code to be executed as long as the condition is True

Example:

count = 0

while count < 5:
    print(count)
    count += 1

In this example, the while loop continues to execute as long as the count is less than 5. It prints the value of count and increment it until the condition is no longer True.

Conclusion

Loops are a fundamental programming concept that allows you to perform repetitive tasks and iterate over sequences of data in Python. By mastering for and while loops, and understanding control statements and nested loops, you can make your code more efficient and powerful. Loops are a critical skill for any Python programmer, and they open up endless possibilities for automating and processing data in your programs.