There are two kinds of loops: for loops and while loops. First, we will start off with for loops. For loops don't really have anything to do with input, but are used when something needs to be repeated a certain amount of times. Like the elements of input, this function only works in a program. Write this in a new window:
for loop in [1, 2, 3, 4, 5]:
print("hello")
Run the program. You should get an output like this:
hello
hello
hello
hello
hello
Hey, how did that happen? Well, "loop" in the program actually does whatever is in the block of code for each time it says in the square brackets. However, we can call "loop" whatever we want! Try changing that word to "chips" or "cow" or maybe "helicopter" and the program will still run the same. Also, there's a quicker way to use for loops in case you need one to repeat many times. This shortcut is called range().
In the square brackets of our first loop program, we started with 1, right? Well, another way that programmers have argued about is starting with 0. Most people do it that way, but you can choose which way you want to. Either way starts at 1. Oh, and remember: When telling it to go to, for example, 10, it will only do it 9 times. Remember, Python goes up to right under that number, and doesn't do the last one (this doesn't make a difference with starting with 1 or 0)! Anyway, let's try another program, this time, with range(). I will also use "i," "j," and "k" for the loop names. That's what most programmers use for loop names. Try this program:
for i in range(1, 11):
print(i, "times 8 =", i * 8)
for j in range(1, 10, 2):
print(j)
for k in range(5, 26, 5):
print(k)
for loop in range(10, 1, -1):
print(loop)
When you run the program, you should get an output like this:
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
5 times 8 = 40
6 times 8 = 48
7 times 8 = 56
8 times 8 = 64
9 times 8 = 72
10 times 8 = 80
1
3
5
7
9
5
10
15
20
25
10
9
8
7
6
5
4
3
2
The range seems confusing, but I will explain how the range worked each time. Let's start with the first range, which created this segment:
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
5 times 8 = 40
6 times 8 = 48
7 times 8 = 56
8 times 8 = 64
9 times 8 = 72
10 times 8 = 80
So, i, in this case, Went from 1 to 10, and printed each time: i, "times 8 =", i * 8. That means it takes i and the iteration it's in (each time through a loop is an iteration), multiplies it by 8 (after the string). Now, for the second loop that created this part:
1
3
5
7
9
In this part, the range had 3 numbers in it! What was the first one for? Well, j went from 1 to 10, counting by 2. Putting 3 numbers in range() means you are skip counting by something. Here is another example of skip counting, but by a different number:
5
10
15
20
25
In that segment, k started at 5, then skip counted by 5 to 25. Now, here's another skip counting loop, but this time, it goes a different way:
10
9
8
7
6
5
4
3
2
You'll notice that in the range that created that segment, it said range(10, 1, -1). It means that it started at 10, then went to 1, by subtracting 1. In the range, -1 doesn't mean negative one, it means subtract one.
Now, let's try a loop with a range!
for letter in "Hi there":
print(letter)
Press Enter, Backspace so you aren't typing in the block of code, and press Enter again.
H
i
t
h
e
r
e
What happened? Instead of saying "Hi there," it said each character of it on a single line! Here's why: each character in the string counts as and iteration, and the program takes the string as what it is printing.
Well, those are the ways you can use loops. However, these were only for loops. We still haven't learned while loops, the type involving input. Make sure you have for loops down! Loops are used many times in programming.
Now, let's try a loop with a range!
for letter in "Hi there":
print(letter)
Press Enter, Backspace so you aren't typing in the block of code, and press Enter again.
H
i
t
h
e
r
e
What happened? Instead of saying "Hi there," it said each character of it on a single line! Here's why: each character in the string counts as and iteration, and the program takes the string as what it is printing.
Well, those are the ways you can use loops. However, these were only for loops. We still haven't learned while loops, the type involving input. Make sure you have for loops down! Loops are used many times in programming.
No comments:
Post a Comment