Loops
Try making a program that prints the digits (0 to 9). It's pretty long, right? Well, in this section, you'll learn how to do that with only two lines of code!
Try making a program that prints the digits (0 to 9). It's pretty long, right? Well, in this section, you'll learn how to do that with only two lines of code!
The for loop is a common type of loop, but each programming language has its own version. Python's version of a for loop is used to loop through each item in a list, tuple, set, dict, or iterable object (more on that later). Here's the basic format:
for variable in list/tuple/set/dict/iter:
code...
code...
When Python runs this, it will set variable to be the 1st item of the list/tuple/set/dict/iter. It will then run the code inside. Then, it will set variable to be the 2nd item of the list/tuple/set/dict/iter. It will then run the code inside again. Python keeps doing this until it has set variable to each item in the list/tuple/set/dict/iter. Remember our example from the top of the page? Let's try that with a for loop:
for i in [0,1,2,3,4,5,6,7,8,9]:
print(i)
Try doing this in Python. It should have the same results as the program you created at the top of this page.
Remember the example above? There's an even shorter way to write this, with the range([start,]end[,step])
function.
The range()
function is very useful for when you need to loop through numbers. However, with the two optional arguments, it may seem confusing. Let's look at each in turn:
range(end)
will count through the numbers from 0 to end, excluding end.range(start,end)
will count through the numbers from start to end, excluding end.range(start,end,step)
will count through the numbers from start to end, only counting every stepth number.The range()
function does not return a list, tuple, or set. Instead, it returns an iterable object, which cannot be viewed. You can, however, change it to one of those by using list()
, tuple()
, or set()
. I would recommend converting a range to a list or tuple, as a set's order may not be what you expect.
Now that we know how to use range, let's try to make a program that prints even numbers from 2 to 10. Here's how:
for i in range(2,11,2):
print(i)
Running this gives:
2
4
6
8
10
Let's look closer at our example:
i
variable tells Python to store each item in i
.range(2,11,2)
tells Python to create a range:2
tells Python to start the range at 2.11
tells Python to end the range just before 11, at 10.2
tells Python to count in steps of 2. print(i)
on the next line tells Python to print the variable i.That's all for the for loop!
The while loop is different from the for loop. Instead of looping through values in a list, it keep running code until a statement returns False. Here's the format:
while condition:
code...
code...
The while loop keeps running until condition returns False. If condition returns False the first time it is evaluated, the code inside will NOT be run. Let's try an example:
a = 0
while a < 4:
print(a)
a = a + 1
This should output:
0
1
2
3
Let's look closer at our program:
a = 0
sets the variable a to 0.while
keyword lets Python know that we're starting a while loop.a < 4
tells Python to stop when the statement a < 4 is False.print(a)
tells Python to print the variable a.a = a + 1
tells Python to increment the variable a by 1.Python has two very special keywords used in loops: break
and continue
.
The break
keyword exits the loop immediately.
The continue
keyword continues the loop from the top of the loop.
for i in range(10):
if i == 3:
continue
print(i)
if i > 5:
break
This program will print:
0
1
2
4
5
6
Can you see why it doesn't print 3, 7, 8, and 9? The reason it doesn't print 3 is because of the
if i == 3:
continue
block. Python sees this and, once i
is 3, runs the command inside, continue
, which tells Python to act as if it just reached the end of the loop.
The reason it doesn't print 7, 8, and 9 is because of the
if i > 5:
break
block. Python sees this and, once i
is greater than 5, exits the loop.