Coding

Python Programming Tutorial #7 – While Loops

  • 00:00:00 hey guys welcome back to the seventh
  • 00:00:03 video in my Python programming series
  • 00:00:05 today we're going to be talking about
  • 00:00:07 while loops so in the last video we
  • 00:00:08 talked about four loops and before that
  • 00:00:10 we talked a lot about conditions so it's
  • 00:00:13 going to be important to understand
  • 00:00:14 those things before we can move on today
  • 00:00:16 to this video so if you haven't seen
  • 00:00:17 those videos go back at my channel and
  • 00:00:20 check those out first okay so let's get
  • 00:00:22 right into it today we're going to be
  • 00:00:24 talking about while loops so let's first
  • 00:00:26 of all just put an example down and get
  • 00:00:28 these syntax okay so we have well
  • 00:00:31 condition then we are going to do
  • 00:00:35 whatever is inside the loop so it seems
  • 00:00:38 simple enough but they can get fairly
  • 00:00:42 complex so well condition is equal to
  • 00:00:46 true so for example this condition here
  • 00:00:50 this could be a variable well that is
  • 00:00:53 equal to true then we're going to do
  • 00:00:54 whatever is inside of this loop so again
  • 00:00:58 we keep talking about conditions they're
  • 00:01:01 very important in Python we really need
  • 00:01:03 to understand them before we can move
  • 00:01:05 into more complex topics okay so well
  • 00:01:09 conditions equal to true we're going to
  • 00:01:10 do whatever's in this loop so how does
  • 00:01:12 this really work well pretty much what
  • 00:01:16 happens is we have the while loop here
  • 00:01:19 we have the condition so this is true
  • 00:01:20 this means we're going to do this now
  • 00:01:22 what happens is we actually come back up
  • 00:01:24 to the top after we've done this we say
  • 00:01:25 well is this condition still true well
  • 00:01:28 yes it is
  • 00:01:29 so that means now we're going to do this
  • 00:01:30 again and we just continuously do this
  • 00:01:33 until eventually the condition is equal
  • 00:01:35 to false or we have a keyword appear
  • 00:01:39 called break like this now I'll do an
  • 00:01:42 example so we can understand this but
  • 00:01:44 this is just the basic syntax we have
  • 00:01:46 while we have a condition and then we
  • 00:01:48 have a colon followed by an indented
  • 00:01:50 block which will be whatever is going to
  • 00:01:51 run okay so let's start off by using a
  • 00:01:54 variable we're going to just call it
  • 00:01:55 loop we're going to set it equal to true
  • 00:01:57 now we're going to make our while loop
  • 00:02:00 and say well loop now by just putting a
  • 00:02:04 loop here it defaults to say well loop
  • 00:02:06 is equal to false on putting loop
  • 00:02:09 there's the exact same thing as typing
  • 00:02:11 this it's just shorter to put
  • 00:02:13 loop instead of doing that equals equals
  • 00:02:15 true okay we're going to get some input
  • 00:02:19 from the console so we're going to say
  • 00:02:20 name is equal to input and then I'm just
  • 00:02:24 going to put in here insert something
  • 00:02:27 okay and then we're going to put an if
  • 00:02:30 statement which we talked about in
  • 00:02:32 another video so if you haven't seen
  • 00:02:33 that go back and check that out when I
  • 00:02:35 say if name is equal to stop then we are
  • 00:02:41 going to break out of the loop by typing
  • 00:02:45 break or we could also do this we could
  • 00:02:48 set the condition equal to false so we
  • 00:02:50 set the variable up here equal to false
  • 00:02:52 now I know I just typed a lot there so
  • 00:02:53 let's talk about what really is going to
  • 00:02:56 happen so we have our condition we say
  • 00:02:58 well loop is equal to true right now we
  • 00:03:00 set it equal to true up at the top so
  • 00:03:01 it's going to automatically run at least
  • 00:03:03 once now we get input from the console
  • 00:03:06 in store it in the variable name now we
  • 00:03:10 have a basic if statement here that
  • 00:03:12 pretty much says if name is equal to
  • 00:03:14 stop well then we're going to stop the
  • 00:03:16 loop and we're going to break out of it
  • 00:03:18 otherwise we're going to keep asking the
  • 00:03:21 user to insert something until
  • 00:03:24 eventually they type stop so let's see
  • 00:03:26 how this all works
  • 00:03:28 okay so insert something I say one two
  • 00:03:31 three it didn't like that once we insert
  • 00:03:34 something again I say we okay what about
  • 00:03:37 hello keep going okay now I'm going to
  • 00:03:39 type stop and you see that the program
  • 00:03:43 stops running okay so again I'll show
  • 00:03:47 you that we can do this by just setting
  • 00:03:49 the variable equal to false same thing
  • 00:03:52 again if I type hi keeps going I type
  • 00:03:55 stop it's going to stop and then maybe
  • 00:03:59 if we get rid of the variable here we
  • 00:04:01 can also just type in the word break
  • 00:04:03 like that and same thing again if I type
  • 00:04:06 in some random things and I type stop it
  • 00:04:10 stops okay so pretty much the way that
  • 00:04:13 the break keyword works is by simply
  • 00:04:16 saying okay are we inside any loops
  • 00:04:18 right now
  • 00:04:19 it checks whatever loop you're inside so
  • 00:04:21 right now we're inside the while loop
  • 00:04:22 and it says okay well this line has now
  • 00:04:25 run so what we're going to do is we're
  • 00:04:27 going to get
  • 00:04:27 out of the loop so it pretty much will
  • 00:04:29 now start reading from the next blank
  • 00:04:32 line in the file meaning that it's going
  • 00:04:35 to get out of this loop and just
  • 00:04:38 continue going down the program okay and
  • 00:04:41 that's pretty much it for while loops we
  • 00:04:44 can talk a little bit more about why we
  • 00:04:46 would use a while loop instead of using
  • 00:04:48 a for loop the reason we might use a
  • 00:04:50 while loop is for something like this so
  • 00:04:51 for example if you want to ask someone
  • 00:04:53 to insert a password so we're going to
  • 00:04:56 password like this and we'll just change
  • 00:05:00 this variable password so paths are
  • 00:05:02 short and now maybe we wanted to check
  • 00:05:05 we want to say well oops well let's just
  • 00:05:09 do password because past is a key word
  • 00:05:11 in Python I forgot about that password
  • 00:05:14 and we want to make sure that the
  • 00:05:16 password has for example at least one
  • 00:05:19 letter in it well we're going to keep
  • 00:05:21 asking the user to re input their
  • 00:05:23 password until it meets the certain
  • 00:05:26 criteria so for example it might have to
  • 00:05:28 have one capital it might have to have
  • 00:05:30 one letter in it I may have to have a
  • 00:05:33 few numbers something like that right so
  • 00:05:35 we would keep asking the user for a
  • 00:05:36 password until they gave it to us
  • 00:05:39 correctly now the reason we wouldn't use
  • 00:05:41 a for-loop for this is because a for
  • 00:05:43 loop usually we know how long we want to
  • 00:05:46 run the for loop for that's why we put a
  • 00:05:49 number in the range so for example we
  • 00:05:51 put 10 or we put 12 well as a while loop
  • 00:05:53 we don't know how long we're going to
  • 00:05:55 run it for so for example someone could
  • 00:05:57 get the password first try that they can
  • 00:05:59 put it in correctly with the right
  • 00:06:01 criteria or it could take them 30 tries
  • 00:06:03 right we don't know how long it's going
  • 00:06:04 to take them okay so that's it for
  • 00:06:07 today's video if you liked the video
  • 00:06:10 please like it and subscribe and I will
  • 00:06:13 see you again tomorrow tomorrow with a
  • 00:06:15 new video