Basic Loop Structures/Iterative Statements in Python

Looping is a fundamental programming concept that allows for the execution of a block of code repeatedly based on a specified condition. In Python, loops are used to iterate over sequences (like lists, tuples, dictionaries, and strings) or to repeat a block of code a specific number of times.

Types of Loop Structures in Python:

Syntax of Loop Structures:

For Loop Syntax:

for item in iterable:
            # Block of code to execute for each item

While Loop Syntax:

while condition:
            # Block of code to execute while condition is true

Code Example of Loop Structures:

Code Example

# For Loop Example
        fruits = ["apple", "banana", "cherry"]
        for fruit in fruits:
            print(fruit)
        
        # While Loop Example
        count = 0
        while count < 5:
            print("Count is:", count)
            count += 1

Output

apple
banana
cherry
Count is: 0
Count is: 1
Count is: 2
Count is: 3
Count is: 4

Important Points on Loop Structures:

Conclusion

Understanding basic loop structures is crucial for effective programming in Python. Mastery of these concepts allows for the efficient execution of repetitive tasks and manipulation of data structures.