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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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.

Print Integers (Solution)

00:00 After you finish your first challenge, let’s go back to the training field and do some exercises to train for loops. Just like before, it’s a good idea to paste the exercise as a comment and format it so you can read everything.

00:15 In this exercise, you need to write a for loop that prints out the integers two through ten. And this you should print on each line using the range() function.

00:25 Okay, let’s get going.

00:28 for i in, and then you use the range() function. And for now, let’s just put 10 there and see what happens by printing out the value of i for each step of the for loop, save the file, and run the module.

00:47 Okay. And you can see it’s starting from 0, and then it counts up to 9. That’s not entirely what you want, so you need to adjust the argument that the range() function takes.

01:00 So instead of having the 10 there, you need to adjust the stop value one step after the point where you actually want to stop. So if you want to stop at 10, you need to have an 11 in there.

01:12 So you could either put 10 + 1 there or write the actual number there, which is 11.

01:20 When you run it, then you see the loop goes from 0 to 10, which is almost what you want because the loop still starts with 0.

01:28 So you also need to adjust the start parameter that range() takes. So you need to add an argument as the first argument in the range() function call, which in this case is 2.

01:40 So now it’s for i in range(2, 11), and then you print(i). Let’s run it. And now it says 2, 3, 4, 5, 6, 7, 8, 9, and 10.

01:51 Perfect. And that’s what you needed to do. So let’s hop over to the next exercise.

Become a Member to join the conversation.