Loops

Learn about 'while' and 'for' loops in Python.

StartPython Illustration
  1. 1

    The [...] loop is similar to an [...] statement: it executes the code inside of it if some condition is true. The difference is that the [...] loop will continue to execute as long as the co...

  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 [...] variable is set to [...] 2. The [...

  3. 3

    Inside a [...] loop, you can do anything you could do elsewhere, including arithmetic operations.

  4. 4

    A common application of a [...] loop is to check user input to see if it is valid. For example, if you ask the user to enter [...] or [...] and they instead enter [...] , then you should re...

  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. [...] ) 2. The logic of the loop prevents the loop condi...

  6. 6

    The [...] 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 [...] statement. * First, create a [......

  7. 7

    Something completely different about Python is the [...] / [...] construction. [...] / [...] is similar to [...] / [...] , but there is a difference: the [...] block will execute anytime...

  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: [...] In this exercise, allow the user to guess what the number is [......

  9. 9

    An alternative way to loop is the [...] loop. The syntax is as shown in the code editor. This example means "for each number [...] in the range 0 - 9, print [...] ".

  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 [...] loop, you can print out each individual character in a string. The example in the editor is almost plain English: "for each character [...] in [...] , print [...] ".

  12. 12

    String manipulation is useful in [...] loops if you want to modify some content in a string. [...] The example above iterates through each character in [...] and, in the end, prints out [...

  13. 13

    Perhaps the most useful (and most common) use of [...] loops is to go through a list. On each iteration, the variable [...] will be the next value in the list. So, the first time through, it ...

  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. [...] 1. First, we c...

  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 [...] function comes in handy. [...] will create pairs of elements when passed two lists, and will stop ...

  17. 17

    Just like with [...] , [...] loops may have an [...] associated with them. In this case, the [...] statement is executed after the [...] , but only if the [...] ends normally—that is, no...

  18. 18

    As mentioned, the [...] block won't run in this case, since [...] executes when it hits [...] .

  19. 19

    To wrap up this lesson, let's create our own [...] / [...] 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, [...] , [...] , and [...] are all integers, but [...] is not). For the purpose of this lesson, we'll also say that **a numbe...

  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 [...] , just multiply all the integers from 1 through [...] . For example: - [...

  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 [.....

  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

Loops

StartPython Illustration