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.…
- 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 Efficiently (2017)with 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.