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.
  1. 1
    The best way to get good at anything is a lot of practice. This lesson is full of practice problems for you to work on. Each exercise will contain minimal instructions to help you solve these probl…
  2. 2
    All right! Let’s get started. Remember how an even number is a number that is divisible by 2?
  3. 3
    An integer is just a number without a decimal part (for instance, -17, 0, and 42 are all integers, but 98.6 is not). For the purpose of this lesson, we’ll also say that **a number with a decimal p…
  4. 4
    Awesome! Now let’s try something a little trickier. Try summing the digits of a number.
  5. 5
    All right! Now we’re cooking. Let’s try a factorial problem. To calculate the factorial of a non-negative integer x, just multiply all the integers from 1 through x. For example: - factorial(4) w…
  6. 6
    A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself. (That’s a mouthful!) In other words, if you want to test if a number in a variable x is…
  7. 7
    Great work so far! Let’s practice writing some functions that work with strings.
  8. 8
    Nice work. Next up: vowels!
  9. 9
    Scrabble is a game where players get points by spelling words. Words are scored by adding together the point values of each individual letter (we’ll leave out the double and triple letter and word …
  10. 10
    You’re doing great with these string function challenges. Last one!
  11. 11
    Great work so far. Let’s finish up by practicing with a few functions that take lists as arguments.
  12. 12
    Awesome! Now let’s practice filtering a list.
  13. 13
    Great! Now let’s try a little multiplication.
  14. 14
    Awesome! Now for something a bit trickier.
  15. 15
    Great work! You’ve covered a lot in these exercises. Last but not least, let’s write a function to find the median of a list. The median is the middle number in a sorted sequence of numbers. F…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo