Loops in Python

 Loops in Python


Loops are used to iterate over a sequence of items in Python. Here's an example of a for loop:


python


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


for fruit in fruits:

    print(fruit)

This will print each item in the fruits list on a separate line.


We can also use a while loop to iterate over a sequence of items:


python


i = 0


while i < 5:

    print(i)

    i += 1

This will print the numbers 0 through 4, since the loop will continue to run as long as i is less than 5.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...