Python loop controls are deceptively simple. The Python while loop works as expected, but the for loop is both elegant and powerful.
- [Instructor] Python provides two basic types of loops. A while loop tests a conditional expression and the body of the loop is executed while the condition remains true. A for loop iterates over a sequence and the body of the loop is executed for each element of the sequence and until the sequence is exhausted. Here in Comodo I've opened a working copy of while.py, from chapter two of the exercise files, and we see a while loop down here, starting on line seven and the condition is n is less than five.
So we've initialized n in the line above and while n remains less than five and of course it starts at zero, which is less than five, so the condition is true. You notice that we increment n each time through the loop, at the end of the loop. Make n plus equals one and for each value of n, so it'll be zero, one, two, three, and four, it will print an element of this list up here and the elements are numbered zero, one, two, three, and four. When n gets to five it's no longer less than five, the condition is false, and the loop will finish.
So when I save this and run it, you see, we print each of the elements of the words list. For a more practical example, I have a working copy of fibonacci.py, from chapter two of the exercise files. This simply prints a fibonacci series. I'll save this and run it and you can see there's a series of fibonacci numbers. This condition, here, while b is less than 1,000, is what stops it with the fibonacci number of 987.
A for loop is very different. Here we have a working copy of for.py, from chapter two of the exercise files. For many uses this is far more convenient than a while loop. You notice here, on line six, for i in words and so we have our same list of words. For each element of words, in order because it's an itterable. For each element of words the variable i will be assigned that element and the body of the loop is executed, which in this case is simply print i.
You see it's much easier than doing this with the while loop and when I save this and run it, you see we get exactly the same result. Every element of the list is passed to the print function and printed and you can see that it's very easy. This is why you'll see this loop very, very often in the case of containers in Python and other itterables. For i in words is just a whole lot easier than initialize i, test a condition, increment i.
So for many uses this can be a lot more convenient than a while loop. Python provides the two most useful forms of loops, a while loop and a for loop. You'll see many examples of these loops as you continue with this course.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.