From the course: Python Quick Start

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Writing a while loop

Writing a while loop - Python Tutorial

From the course: Python Quick Start

Writing a while loop

Iteration is the process of repeatedly performing a set of instructions. A while loop is another structure that helps you achieve iteration. In this video, I will show you how to write a while loop. First, I want to introduce a helpful function called Len. Len is a built-in function in Python that takes in a sequence as input and returns the length of the sequence as output. For example, say I have a list of vowels stored in a variable named vowels and I want to know the length of the list. I would do this. vowels = a, e, i, o, u. And the next line, length = len(vowels). Then I print(length) So I'm going to go ahead and run this cell and I get 5. So the list vowels has a length of 5. Now, let's say I want to iterate over the list vowels and print out each vowel using a while loop. I could do this. Vowels equals, and that part is the same as earlier, so I'm going to copy-paste. i = 0 while i < len of vowels. Print(vowels[i]) and then i += 1. I'll go ahead and run this now and I get a…

Contents