Enroll in The Complete Python 3 Course: Beginner to Advanced!
Click here to subscribe for more videos like this!
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.