How to Create a Loop in Python to Iterate 10 Times?

Estimated read time 1 min read

To create a loop in Python to iterate 10 times, you can use either a for loop or a while loop. Here are examples of how to create such loops:

Example 1: Using a for loop

for i in range(10):

In this example, we use a for loop with the range() function to loop over the numbers 0 to 9. The loop iterates 10 times, and for each iteration, the value of i is printed to the console.

Example 2: Using a while loop

i = 0
while i < 10:
    i += 1

In this example, we define a variable i with an initial value of 0. We then use a while loop to loop until i is greater than or equal to 10. For each iteration of the loop, we print the value of i to the console, and then increment i by 1 using the += operator.

You can modify these examples to perform different actions inside the loop, such as performing calculations or iterating over different ranges of numbers.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply