Loading topic
for loop does not work only on lists. It works on any object that can hand out its items one at a time. This chapter explains what that means in practice, which built-in types qualify, and what happens when you loop over something that does not.4 min readfor statement runs a block of code once for each item an iterable produces. This chapter covers the syntax, what you can loop over, how the loop variable behaves, and the errors intermediate learners hit most often. It assumes you can write and run a Python 3 script, and that you know variables,4 min readprint(), integers, and the basic for statement. By the end you will be able to predict exactly which numbers range() produces for any call. You will also be able to recognize the erro4 min readrange(), the step. It also covers how a negative step reverses direction and which bounds you need when you count down. It assumes you already know range(stop) and range(start, stop), that the stop value is excluded, and how a basic for loop runs.4 min readfor loop, know how range(start, stop, step) picks its numbers, and are comfortable with list indexing and slicing. Here you will see what a range object is, what it can do, and where it differs from a list.4 min readfor loop, know what a function is, and understand that Python names are bound to objects. It covers where the loop variable lives, what value it has after the loop, and the bugs this causes. Run each snippet as its own script, because several reuse the na5 min readfor i in range(len(seq)) pattern: what it does, the few cases where it is the right tool, and the mistakes it invites. It assumes you can write a basic for loop, you know that range(start, stop, step) excludes stop, and you can index and slice lists and strings.5 min readfor loop over a list, that you know range() and len(), and that you have seen tuples and unpacking such as a, b = (1, 2). Here you replace the range(len(...)) pattern with enumerate(), which hands you each position and its item together.5 min readfor loop, unpack a tuple into several names, and use enumerate(). It also assumes you know that a for loop works on any iterable. The strict=True option needs Python 3.10 or newer.4 min readfor loop over a list or a range(), and that you are comfortable with if statements and boolean expressions. It covers the two statements that change a loop's normal flow, how they behave in edge cases, and the mistakes that come up most often.6 min readfor loop over a list or range(), and that you know how indentation defines a block. You will learn how a loop inside a loop executes, how to count its iterations, and how break, continue and else behave when loops are nested. You will also see the5 min readfor and while loops, use break and continue, and read if/else blocks. It also assumes you know range() and enumerate() from earlier chapters. Here you learn what else means when it is attached to a loop, when it is useful, and where it misleads re5 min readfor loop, know that list.remove() and list.append() change a list in place, and have read the earlier chapters on the for statement and the iterator protocol. Here you will see why changing a list during iteration gives wrong results without raising5 min readfor loop. It assumes you can write for loops over lists, strings and range(), and that you know how to use try/except and define a simple class. By the end you should be able to read a for loop as a sequence of iter() and next() calls,5 min readfor loop over a list or a range(), call append() on a list, and use if and else. It also assumes you know how enumerate() and zip() work. It shows how a common loop pattern, building a new collection from an iterable, can be written as a single expr5 min readrange(start, stop, step) works, that a range object is a lazy sequence, and how for loops, enumerate() and zip() behave. Most range bugs come from a few misunderstandings. Each section below shows the mistake, what Python does, and the fix.4 min read