In this tutorial we are going to cover the two loops types in Python. The first one is a for loop. A for loop will allow you to iterate over a list in Python. In other words, you can do something for each item in the list. So, let's go ahead and create a list.
numbers = [1, 2, 3, 4, 5]
for item in numbers print(item)
When we run this each number in our list will be printed out in the console. Let's add names to our list instead of numbers.
names = ["Nick", "Someone", "Another Person"]
for item in names print("This persons name is", item)
That is a for loop, and basically the second parameter is to access the list, and then the first parameter is what you want each item in the list to be called while inside it's little block of code. Now we're going to learn about a while loop.
run = True current = 1
while run: if current == 100: run = False else: print(current) current += 1
In this bit of code we are creating two variables, while, and then we write what we want to happen while the program is running. In this case, we are going to check to see if current = 100. If not, we are going to add 1 to the total, and we are starting from 1. Once the total hits 100 the program will stop running. We will be using loops quite a bit throughout this course, so make sure you've mastered this concept. Learn more about my courses at https://josephdelgadillo.com
Alright guys, so we are about to create a calculator program in Python and before
we do that actually we need to learn a few more things. So we're going to go
over a couple of the loop types in Python, so there's two. One of them is a
for loop and one of them is a while loop. So basically a for loop is good for if
you want to iterate over an array or a list
in Python. You can do something for each item in the list, so let's go ahead
and create a list. So let's just call it "numbers =" and we're going to create array
"[1, 2, 3, 4, 5]"
So let's type "for item in numbers:" and we're going to just "print(item)" going
to save and let's go ahead and run this, and you're going to see that on each line it
prints out it's number. So, we would be able to do this if we wanted to have
different names in here.
So, again, "["Nick", "Someone", "Another Person"]" save this. Let's go ahead and run
the script again and it's going to print out "Nick Someone Another Person" So what we can do
here actually is we can "print('This persons name is", item)"
So let's go ahead and you'll see that for each one it does print out that. So
that is a for loop and basically the second parameter here in the for loop is the
array or the list, and then the first one here is what you want each item in
the list to be called while inside it's little block of code. So in this
case we're calling it item. So that is a for loop, now we're going to learn about
a while loop. So let's go ahead and
create two variables,
one is going to be called "run = True" and the
other is going to be called "current = 1" So
let's go ahead and what we're going to do is type "while run:" and then we write
what we want to happen you know if it's currently running. So what we're
going to do is we are going to put an if statement here, so we're going to go "if
current == 100:" actually don't need those brackets.
Alright so if "if current == 100:" we are going to set "run = False" but if
it's not "else:" we're going to "print(current)" then after we print current,
"current += 1"
Let's go ahead and save this and what this is going to do is basically we're
setting run to true initially because we want it to run at least once,
so while run which in this case the first time it goes over it's definitely
going to be true. It's going to check is current equal to a 100. Well, on the
first time
no it's not it's equal to 1, so this block of code won't run. If it's not
equal to a hundred what it's going to do is it's going to print the current
number, and then add one to the current number, and then run is still true so
it's going to go over it again. So what we should see here is it's going to
print the numbers 1 to 99. So let's go ahead and run the script, and that's exactly
what we see here. Now these are two useful concepts that we're going to be
using, so hold onto those, and if you didn't quite understand what I'm doing
here, let me know in the discussion section.