Loops
Learn about 'while' and 'for' loops in Python.
StartLoops
Lesson 1 of 2
- 1The while loop is similar to an if statement: it executes the code inside of it if some condition is true. The difference is that the while loop will continue to execute as long as the condition i…
- 3Inside a while loop, you can do anything you could do elsewhere, including arithmetic operations.
- 4A common application of a while loop is to check user input to see if it is valid. For example, if you ask the user to enter y or n and they instead enter 7, then you should re-prompt them for input.
- 5An infinite loop is a loop that never exits. This can happen for a few reasons: 1. The loop condition cannot possibly be false (e.g. while 1 != 2) 2. The logic of the loop prevents the loop …
- 7Something completely different about Python is the while/else construction. while/else is similar to if/else, but there is a difference: the else block will execute anytime the loop condition i…
- 8Now you should be able to make a game similar to the one in the last exercise. The code from the last exercise is below: count = 0 while count < 3: num = random.randint(1, 6) print num if nu…
- 9An alternative way to loop is the for loop. The syntax is as shown in the code editor. This example means “for each number i in the range 0 - 9, print i”.
- 10This kind of loop is useful when you want to do something a certain number of times, such as append something to the end of a list.
- 11Using a for loop, you can print out each individual character in a string. The example in the editor is almost plain English: “for each character c in thing, print c”.
- 12String manipulation is useful in for loops if you want to modify some content in a string. word = “Marble” for char in word: print char, The example above iterates through each character in w…
- 13Perhaps the most useful (and most common) use of for loops is to go through a list. On each iteration, the variable num will be the next value in the list. So, the first time through, it will be …
- 14You may be wondering how looping over a dictionary would work. Would you get the key or the value? The short answer is: you get the key which you can use to get the value. d = {‘x’: 9, ‘y’: 10, ‘…
- 15A weakness of using this for-each style of iteration is that you don’t know the index of the thing you’re looking at. Generally this isn’t an issue, but at times it is useful to know how far into …
- 16It’s also common to need to iterate over two lists at once. This is where the built-in zip function comes in handy. zip will create pairs of elements when passed two lists, and will stop at the e…
- 17Just like with while, for loops may have an else associated with them. In this case, the else statement is executed after the for, but only if the for ends normally—that is, not with a break. This…
- 18As mentioned, the else block won’t run in this case, since break executes when it hits ‘tomato’.
- 19To wrap up this lesson, let’s create our own for/else statement from scratch.
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory