Loops are exactly what they are. They make a line of code run repeatedly so you don’t have to type it hella times. Just like putting a song on repeat so you don’t have to keep starting it over. We can also call it an automated copy-paste.
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
message = fruit + " is a fruit"
print(message)
Output:
apple is a fruit
banana is a fruit
orange is a fruit
This code saves us from having to print each of those fruits by themselves. Imagine all the space that’d take up.
To use this function, after you define a list, you’d first type ‘for’, then set a variable to access each item in the list. Just how that list was called ‘fruits’, it makes the most sense to call our variable ‘fruit’. You want it to access a fruit in the fruits. You can name it whatever you want, just use common sense for the sake of organization. Then you add ‘in’ after the variable, add the list name with a colon at the end, then finish with an indented function of whatever you want it to do. In this case it was to insert the list item (the fruit) into a message and print that message.
Here’s a math one with adding all the numbers in a list.
numbers = [1, 2, 3]
total = 0
for n in numbers:
total = total + n
print(total)
Output: 6
So this one confused me because I thought it would print it separately like with the fruits. First, ‘total’ is not an actual function, it’s just a variable name. So when the code runs its first iteration with the first list item, 1, that adds 0+1 which makes the total 1. Now when it moves on to its second iteration with 2, it carries over that previous total, so that adds 2+1=3. Again, for the third and last iteration, it carries that last total 3, and adds by the 3 in the list, which makes 6.
Even outside of bracketed lists, you can use a loop to break down all the characters in a string
for letter in "abc":
print(letter)
Output:
a
b
c
‘Letter’ is just a variable name. You could type anything and it’ll still break down that string, long as you use it consistently.
While
‘While’ loops will repeat a code as long as a condition is satisfied
i = 0
while i < 20:
print(i)
i = i + 2
Output:
0
2
4
6
8
10
12
14
16
18
This code makes sure that i+2 will iterate on each until it reaches 20. So again, we’re telling the code to run its iterations as long as it meets a certain condition. It’s formally called a ‘while’ loop, but you can keep a mental note that its function is an ‘as long as’. As long as ‘i’ is less than 20, it will continue to iterate.
Practically, this can be used to prompt a program to do or not do something if that condition wasn’t met. Like in some shooter games, if your health gets low, the screen turns red. So the developer would use a ‘while’ loop like “as long as health is less than 30, make the screen do this”. Or for something more universal, as long as you don’t put the correct password, the program will keep prompting you to try again. Or even as long as the wrong password is put more than 4 times, it will prompt you to reset it, or even lock you out from trying again.
Extra Tips
There’s a built-in ‘range’ function that can generate numbers from 0 up to whatever number you set it to.
for number in range(5):
print(number)
Output:
0
1
2
3
4
The number you set it to is where it stops, if you want to see that number itself, you’ll have to go up one.
It’s simpler and cleaner than using a ‘while’ loop like we just did.
for i in range(10):
print(i)
vs
i = 0
while i < 10:
print(i)
i = i +
Output:
0
1
2
...
9
but I’ll have to learn some other variations of that.
Instead of starting from 0, we can set the range to start from another number
for a in range(2, 9):
print(a)
Output:
2
3
...
8
You can still put your limit number, just make sure you precede it with the starting number and separate it with a comma
We also got nested loops:
for i in range(3):
for j in range(3):
print(i + j)
The ‘i’ is the heading loop and the ‘j’ is nested inside of it. So when we add those ranged variables together, it’ll output each combination of those ranges. The output would only show the results, I just put the equations there to illustrate how it’d work.
Output:
i=0, j=0: 0 + 0 = 0
i=0, j=1: 0 + 1 = 1
i=0, j=2: 0 + 2 = 2
i=1, j=0: 1 + 0 = 1
i=1, j=1: 1 + 1 = 2
i=1, j=2: 1 + 2 = 3
i=2, j=0: 2 + 0 = 2
i=2, j=1: 2 + 1 = 3
i=2, j=2: 2 + 2 = 4
We can append an item to a list, which is another form of adding to a list. I’ll have to figure out what the real difference is.
result = [1, 2]
result = result + [3]
print(result)
result = [1, 2]
result.append(3)
print(result)
Output: [1, 2, 3]
These are 2 separate codes, I just put them in the same block. They output the same result.
Continue & Break
The ‘continue’ keyword can skip an iteration in a loop.
for i in [1, 2, 3]:
if i == 2:
continue
print(i)
Output:
1
3
Here it’s added as a simple indentation. It can also apply to any ‘if’ condition we set for it. While it’s formally called ‘continue’ we can look at it as a ‘skip’ function.
There’s also a ‘break’ keyword that will stop the loop before it ends. So while ‘continue’ is for skipping an item in the middle of a list, ‘break’ will shorten that list at whatever point you set it to.
colors = ["red", "green", "blue"]
for color in colors:
if color == "green":
break
print(color)
Output:
red
In this code, I break the list at ‘green’, so it will only show any items before ‘green’.
I’m officially at that point where I just feel stuck. I needed help with most of the challenges, some I had to skip over. I’m past halfway in the course so I’m just gonna tough it out to the end and come back to tie it together.