Python for loop index range

If you’re moving to Python from C or Java, you might be confused by Python’s for loops. Python doesn’t actually have for loops… at least not the same kind of for loop that C-based languages have. Python’s for loops are actually foreach loops.

In this article I’ll compare Python’s for loops to those of other languages and discuss the usual ways we solve common problems with for loops in Python.

    For loops in other languages

    Before we look at Python’s loops, let’s take a look at a for loop in JavaScript:

    1
    2
    3
    4
    
    var colors = ["red", "green", "blue", "purple"];
    for [var i = 0; i 

    Chủ Đề