Iterating with for Python 3


show more Iterating with for provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Iterating with for

One of the most common reasons for using a loop in programming is what's called an iterator. This is where you need to step through some data structure, taking one object at a time, and doing something with each of those objects before going back for the next one. Let's take a look at how this works in Python with the 'for' loop. We'll make a working copy of for.py, and call it for-working.py, and we'll go ahead and open that working copy. Here, we are opening a file with lines of text and reading that file line-by- line and printing out each line on the screen.

So, let's go ahead and run it and see what it does, and we see that it prints these lines of text, and there they are. If we open the file here, lines.txt, we see that it has these five lines of text. You'll notice that in the file the lines are right next to each other, and in our result, it's printing a blank line between them. The reason for that is that the print function prints a blank line ...

Iterating with for
Video duration: 3m 54s 6h 36m Beginner

Viewers:

Iterating with for provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...