00:05Let's talk about control flow.
00:07So with python you can type, you can call functions, or do instructions, so for example
00:20you can do instructions, lots of instructions, and the program would just execute one by
00:25one right, so it would just go running line by line every instruction.
00:33If you run it, you'll see it outputs all of them.
00:37Now it would be very tedious to, for example if you want to output this 100 times, to type
00:42this manually 100 times.
00:45So what you can do instead is use a so-called for loop, where you just make it output 100
00:54times.
00:56And if you want to do this in python, always make sure you have four spaces in front, otherwise
01:02the code won't run.
01:04So now if you were to run it, you'll see it also outputs 100 times, but it doesn't, it's
01:14only two lines of code, right, but it does it 100 times, so from 0 to 100 it will repeat
01:20whatever code is written below.
01:22And this can be multiple lines.
01:25So you can have another line here, and you'll see then both lines are repeated 100 times.
01:33So as you see, it's repeated 100 times.
01:38And in every repetition, you have this unique variable i, so if I were to show that, and
01:46run it, you'll see there's this unique variable i here that is outputted every time.
01:54So you'll see that number goes up if I scroll down.
01:59So that's the for loop, it lets you repeat code, you can also use it to loop over lists,
02:05for example, if you define a list of animals.
02:19And see where to run it, you see now it does nothing, because we don't loop over it.
02:30But we can loop over it like this, so for every animal in the animals, remember four spaces
02:37here.
02:38Python is very strict about that, so if you use five spaces or three spaces, it's not
02:42gonna work, you need four spaces at all times.
02:45So if you now run it, you'll see it just loops over that list of animals.
02:51You can use f-strings here, so you can say, for example, animal, and run it, and you'll
03:01see it runs that lines of code for every item in the list, one by one.
03:07So a for loop is one way to repeat code, but if all we can do is execute code top
03:14-down and
03:14repeat code, then all programs would behave the same at all times.
03:19Which is not what you want, right?
03:20You want to have keyboard input, to have house input, and in order to have any kind of interaction,
03:29you need to run code only sometimes, right?
03:31So, for example, if your password, say enter password.
03:36If the password is correct, maybe it wants to be logged in, and otherwise show failed
03:41password.
03:42So the commands run depends on what needs to be inputted.
03:48So we know how to input, for example, password is input, enter password.
03:54But now, if we were to say logged in and failed, so let me make that into print statement.
04:10So now we would have, it would always output both, and of course, what it should output
04:15depends on our input.
04:16So in order to selectively run either this line of code, or this line of code, we can
04:22use so-called if statements.
04:31And again, we need four spaces.
04:36So now, this line of code will only be run if the password is equal to 123456.
04:43In the other case, so if it's not the case, it will show failed password.
04:48Now if you run it, you'll see we can enter a password.
04:51So let's enter this password, and you'll see it chose logged in.
04:58It runs this block of code, but it didn't run this block of code.
05:01The else case is the negative case.
05:03So if the password is not 123456.
05:08So let's do that again.
05:11And now we type, for example, 123.
05:14You'll see it says failed password.
05:15So it skipped this line of code completely, and it jumped right here.
05:20Now any block of code can be multiple lines.
05:24So you can have different lines of code you want in here.
05:29It will execute this block of code, or this block of code.
05:32And the block of code can be one line, 10 lines, 100 lines, it doesn't really matter.
05:37So if you run it, and we type the correct password, you'll see it run the three lines of code
05:43here.
05:44And if we now type the wrong password, you'll see it says only run this block of code, the failed
05:54password.
05:54So that's how you can selectively run code.
06:01Now of course you can also do this with numbers.
06:04So let's say we do it with numbers.
06:09Say enter x, and this is of type string, that's something to keep in mind.
06:16So if we print it, you'll see it says type str, we run the print type statement with the variable
06:23x here, and it shows it's of type string or text, and with text we can't do any arithmetic or
06:29mathematics.
06:30So we need to convert x either to an integer or a float, like this, or like this.
06:36So a float will let you have numbers like this.
06:42An integer is just natural numbers, right, so only numbers like that, but you can't have any numbers like this.
06:51So in this case let's just do natural numbers.
06:55And depending on x, now it is of type integer, we converted it, you see type integer, so a number.
07:04Now we can say if x is greater than, smaller than, or equal to something.
07:08So let's say if x is greater than 0, we can say positive, and else output negative.
07:19Now if you run this code, you see we can type x, for example 4, it says positive.
07:27So it runs this lock of code, because x is greater than 0, if we were to run it again,
07:34and we type, for example, minus 10, it says negative.
07:36So it skips this lock of code, and it runs directly this.
07:40So you can use the greater than symbol, you can also use the smaller than symbol, you can say negative
07:48if smaller than, for example, otherwise positive.
07:52So the smaller than symbol can be used as well, if you run it, you see enter x, positive if
07:59I type 6, and if I type minus 5, it says negative.
08:05So you can use smaller than, greater than, you can also use the equality symbol, and now if you run
08:14it, and we type code here, you'll see if I type 9, it doesn't do anything.
08:22If I type 10, you'll see it run that lock of code.
08:25So if statements let you selectively run code.
08:31There's also y-loops, a y-loop is used when you don't know how many times the code should repeat.
08:38So with a for loop, you know exactly, for example, for animal in animals, you know exactly it will do
08:45it just a number of times that the list is long.
08:48Or if you were to use numbers, you would say in range 0 to, for example, 100, so you know
08:56exactly it should repeat 100 times.
08:58But if you're not sure how many times it should repeat, you can use a while loop.
09:02And that's, for example, the case if you're asking a password, you want to keep asking the password until it
09:07is correct.
09:07So you can say password is input, enter password, and now we don't know how many times the user will
09:14type the password until it is correct.
09:17It might be the first time it's correct, it might be three times, or maybe 50 times.
09:21So you can say while password is not, so this is the is not symbol, and then your password, and
09:30then just keep asking the password.
09:34Now if we run it, you see we can type some password, and only when the password is correct, it
09:43will stop repeating this code.
09:46So this will repeat indefinitely until the condition is met.
09:52So that's a wide loop, and finally another type of control flow is a try accept block.
09:58So if we try accept block, it tries some code, and if it goes wrong, it does something wrong.
10:11And it's just a way of error handling actually the try accept block.
10:14So if we run it, you'll see it runs this code, and then ends the program.
10:19Now, what's the difference is between just doing it without, is that, for example, if we can type axis, we
10:27define it as text,
10:28and then we try axis x plus one.
10:31Of course this won't work, because with text we can't just add one to the text, it doesn't make sense.
10:36So it would jump into this block of code, but it wouldn't crash, so it would just continue running here.
10:42So if you run it, you'll see it jumps, this goes wrong, it goes into this block of code,
10:49but then it continues running the program, so it actually outputs end program.
10:54And if without the try accept block, it will just crash, so
11:02if we now run it, you'll see it throws an error here, and it never reaches end program.
11:08So try accept block lets you run codes, and then, if something goes wrong, it doesn't crash your entire program.
11:19It just doesn't execute those lines of code then.
11:22So it's a way of error handling.
11:24Those are the most common control flow blocks.
11:29So your program doesn't have to be just lines of commands.
11:32They can repeat using for loops or while loops.
11:36With if statements, you can make it interactive.
11:39And let's write accept block as this one, lets you do error handling.
11:43And let's write it through.
11:43So that's going to happen.
11:44Yes!
11:44Yeah.
11:44Well, I'll get a script.
11:44Well, I'll get a script.
11:44then I'll get it, then, like,
11:44then I'll get a script.
11:45And then I'll get you to the couple of the characters.
11:45And then I'll get it.
11:45So that's a script.
11:45So I'll get it.
11:45Um, and then I'll get to get out of here.
Comments