Lesson 1 of 2
  1. 1
    The 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…
  2. 2
    The condition is the expression that decides whether the loop is going to continue being executed or not. There are 5 steps to this program: 1. The loop_condition variable is set to True 2. Th…
  3. 3
    Inside a while loop, you can do anything you could do elsewhere, including arithmetic operations.
  4. 4
    A 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.
  5. 5
    An 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 …
  6. 6
    The break is a one-line statement that means “exit the current loop.” An alternate way to make our counting loop exit and stop executing is with the break statement. * First, create a while wit…
  7. 7
    Something 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…
  8. 8
    Now 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…
  9. 9
    An 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”.
  10. 10
    This 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.
  11. 11
    Using 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”.
  12. 12
    String 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…
  13. 13
    Perhaps 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 …
  14. 14
    You 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, ‘…
  15. 15
    A 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 …
  16. 16
    It’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…
  17. 17
    Just 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…
  18. 18
    As mentioned, the else block won’t run in this case, since break executes when it hits ‘tomato’.
  19. 19
    To 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