Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Range, Break, and Continue

Give Feedback

In this lesson, you’ll see how execution of a for loop can be interrupted using the break and continue statements. break terminates the loop completely and continue only terminates the current iteration.

break example:

>>>
>>> for i in ['foo', 'bar', 'baz', 'qux']:
...     if 'b' in i:
...         break
...     print(i)
...
foo

continue usage example:

>>>
>>> for i in ['foo', 'bar', 'baz', 'qux']:
...     if 'b' in i:
...         continue
...     print(i)
...
foo
qux

00:00 Now let’s take a look at iterating over a range of numbers. As you’ve probably imagined by now, you could create a list of numbers, and then for i in a: we’ll make it print i, and that will print those values out.

00:22 You can see 0, 1, 2, 3, 4. And that’s all right for when you’re creating a small list, but what about if you want to do tens or hundreds or even thousands of numbers?

00:32 Well, fortunately, Python has a built-in range() function which allows us to give a sequence of integers out. So we can replace this… with just that. If you just have a single value in there, range() will start from 0 and then go up to—but not including—the value that you give.

00:52 That will be a direct replacement for what we’ve just seen. I’ll run it there. You can see we get the same there. Clearly, this now makes it nice and easy to generate large numbers without having to spend half an afternoon typing out a list of numbers.

01:10 You get it very quickly, it goes all the way up to 999. But there’s more to it than that. If you give it two numbers, the first one is the start number and the second one is the stop.

01:23 So here we will go from 10 to 19—because, again, 20 isn’t included. And if we want to change the increment, we have the thing called stride, or step—certainly step makes more sense to me.

01:46 So let’s do it in 2. And now that will only do the even numbers. And you see again, because of the way the numbers work out, when you get up to 18, because 20 isn’t included, if we’d put 21, we’d get that there. So you can see, we go all the way up to 20 there.

02:10 And it doesn’t matter that this doesn’t fit into that pattern. It’s sensible enough not to get upset by that.

02:19 The final area we’re going to look at is altering the for loop behavior. So, it’s possible to use a break or a continue statement within a loop and alter the way that the loop is executed. Let’s take a look at that. So once again, we’ll set up our list of words.

02:41 And make a loop.

02:47 So there we have it. It’s just going to print those four values out. I’m sure you’re confident in that by now.

02:56 Let’s modify the way that that works. So firstly, let’s look at the break. If we put a condition here, if there’s a 'b' in i, let’s break.

03:10 So, when we get to our second value, this will be True and break will happen and it won’t be executed. We see we just get foo that time, because the second time around, there’s a 'b' present in there.

03:27 If we change this from break to continue, what happens is just that time around the loop is aborted, and we go back to here with the next value.

03:38 You can see that the 'bar' and 'baz' ones will not get printed, but 'foo' and 'qux' will.

03:48 And there you go. So if you need to alter the way that a loop works, maybe not execute the command depending on what’s happening, then you have control over that completely. Now, another thing is that for loops have else clauses.

04:06 Here, we will see if the loop gets executed normally,

04:15 we get the contents of the loop, but we also get Done at the end because we finished the execution normally. Now, if that doesn’t happen, the else doesn’t get executed.

04:28 We can say if 'b' in i:—just like before—break. And this time, not only does the loop get broken, but that else clause there doesn’t get executed.

nkemakolam on June 13, 2019

wow

marktompkins on Dec. 25, 2019

very interesting!

kbutler52 on Dec. 27, 2019

Thank you so much for breaking this topic down for me! Love how you helped me to understand it!!!!!

DanielHao5 on Jan. 22, 2020

It’s a good intro to this important topic. However, it should be expanded to show some good and practical examples - eg. deep-dive on some edge cases and show how-to approach on those questions.

Abdullah Dev on Feb. 28, 2020

Great work !! easy to understand and follow.

emalfiza on March 10, 2020

Your explanation is so amazing. just wow Darren

markthiele on March 17, 2020

Thanks, very nice!

paritalagt on April 5, 2020

its good explanation ,but you should include some advanced concept with for loop

jasonwilliams78 on May 1, 2020

Yes this is a great intro, but I could sure use more depth to the for loop using compounding logic on an intermediate level

Ghani on Oct. 15, 2020

Very good tutorial Darren; thank you so much!

Deepak Nallajerla on Dec. 18, 2020

When I started this video course I am thinking “I already know about for loops do I need to watch this video” on other side I was telling to myself Real Python will try to include something new. I am right, I didn’t know about “else” part in for loops. I learned one new thing today.

Become a Member to join the conversation.