 Okay, welcome back to Python for Java coders. This is lesson six where we're going to talk about loops So there are some differences here in the way that loops work in Python versus Java. There's also of course some similarities. So let's get started Let's take a look at a simple for loop So you can see here in Java use you declare int x to be zero and You set your condition and then you set You know how it's going to be You know Incremented okay in this case. It's plus one each time But that's a little different. So watch what I do here for x in Range and we're going to start at zero and we're going to go to ten colon and print x Okay, so let's go ahead. This is what we got out of Java zero through nine Let's see if we get the same thing out of Python. Oh my word Fantastic. So you can see a couple things. We don't use this structure. This is again the kind of C type This is what C looks like Java's kind of you know built on top of C plus plus which was built on top of C etc etc, etc where I should say inherited some concepts from say built on top of and Python's quite different. So you can see here for x in range again This is our starting value, which is the same as you see here This is our final value plus one So it's less than Okay, so never notice it doesn't get to ten Okay, in Java you could change that to an equal sign if you wanted to get to ten, but in Python you'd have to change this to 11 And this one goes to 11 notice it automatically increments by one Okay, so it went zeroes one two three four five six seven eight nine on the second example You can see it goes zero through eight. Okay, so let's do the same thing in Python so same structure for x in range zero to ten and then if we want it to be Incremented by two we just put a two there No two or no number means it increments by one And print x And we'll run that And zero two four six eight Yeah, conceptually very similar, but you see the structure here is quite a bit different Okay, the next example is counting down from ten to zero Okay, so that's gonna be an interesting one So we got starting at ten x equals ten x is greater than or equal to zero So in this case x there the zero is inclusive and we're incrementing or decrementing actually By one we're going down by one so in python it would be like this for x in range Our starting point which is ten In this case we've got to go to negative one Because we want to end on zero and we want to go one past that since we're going down One past zero is negative one And we want to tell it we're going to count down by negative one so print x I'm going to go ahead and run that And hopefully we'll get the same result ten through zero fantastic so far so good Okay. Wow. This is where it gets kind of interesting And it will come as no surprise that You know python's a little bit easier in this this particular thing now notice here. I used something called the care type and We didn't really go over that at least in my class. We kind of skipped this. It wasn't part of the ITJP This wasn't part of the AP requirements So I'm just going to go ahead and well, I'll leave it there. I'll leave it there Let's let's let's go ahead and do it. We can we can play with that a little bit. So I'm going to go ahead and print And I'm going to go title Equals again, I didn't need to declare that as a string it figured it out all on its own Thank you python for being so clever now We need this basically the same structure Okay, so you remember back to one of the earlier videos. We don't use dot length. We use lint. So for I in range It's a string. So I want to start at zero as always lint title And again, I want to go start at the beginning go to the end. I don't need to put a one there It's automatic And then what I can say is I'm going to use a slice if you remember that from the earlier video title Okay, so I'm gonna go ahead and run that and I should see pictures of you I said that put on you the pictures of you. Sorry again one of my favorite all-time favorite songs Okay, so that is one way to do it now here we use the care thing here We use substring now again in python. We don't really we wouldn't really need a substring here because We use the same structure Okay, so I'm just going to skip that one because we don't really need it now Here's where it gets kind of interesting if you recall this this is a for each loop And python has the same thing So what I can do here. It's it's a lot simpler. Of course say for letter in Title and what I like about python it's just like reading English. Okay, so for each letter in title print letter That's it Okay, again, you don't need to declare it as a type You don't need to do again this python or this java thing to care array Again, I didn't talk about that with my students this year, but because I didn't need it but You know, we didn't need to use substring i i plus one It's just a much much simpler cleaner syntax. So if I run that And we'll get pictures of you again. And if you recall from one of the previous videos, I could have done this end equals quote quote And that would print Horizontally instead and in java I could do that with print instead of printlin So let's take a look at that And hopefully that will come out If I should put this here, sorry print Okay, and The last one This is a while loop Uh four loops are very very different in python, but while loops are essentially the same So I'm going to go ahead and type i equals zero While i is less than the length of the title Colon Okay loops, okay in java where we'd have Currently braces we use indentation instead. I talked about this in previous videos tab again four spaces Is the preferred method And then I would just go ahead and type print But what I could do actually to make it simpler say letter equals title i and then print letter comma end equals quote quote And I have to do i plus plus Actually, no, you can't do i plus was it's i plus equals one i plus plus doesn't work. I don't think Let's make sure that works. Then I'll check. Yeah, okay, that works Let me try i plus plus. I'm pretty sure it doesn't work in in python. I could be wrong. Who knows Yep in valence index. Okay, so this plus equals one. So that is how you increment in python Okay, so again while loops are mostly Almost exactly the same as they are in java Now I could have just gone ahead and done this. This is this is a simplification. I could just go ahead and do this But I tend to I tend to print thing or type things out just because learners find it easier to understand what's going on But you get the same output And again, it's not title dot length. It is len title Okay, so again, we're kind of combining some of the things from previous videos and learning some new things here. So again It's four variable in range starting point ending point plus one And then the increment. Okay. Remember if you're Counting down you have to go and you want zero included you have to go past zero to negative one And and because you're going down it's negative one if you're going up It's plus one if you're going down it is negative one And yeah, so then you can see some slices here instead of substring instead of using care at And then again the printing horizontally In java, it's print versus print lin and python its end equals and a couple little quotation marks there Okay, he's busy Stay tuned for more