1 00:00:00,530 --> 00:00:03,220 Hello and welcome to this video. 2 00:00:03,260 --> 00:00:12,530 In this video I'm going to introduce you to if statements if statements are basically conditional statements 3 00:00:12,980 --> 00:00:20,960 that means they are statements that only execute based on certain conditions. 4 00:00:20,960 --> 00:00:31,220 If statements are important aspects of the control flow of the program they help you make important 5 00:00:31,430 --> 00:00:34,990 decisions in your programs. 6 00:00:35,090 --> 00:00:41,450 They only run the code in the if block when the condition is true. 7 00:00:41,450 --> 00:00:48,160 So when you write an if statement you'll have a block of code that you want to execute. 8 00:00:48,440 --> 00:00:57,620 If the condition that you have set is met so only that particular code will execute if the condition 9 00:00:57,620 --> 00:01:03,580 is not met then nothing runs that code does not execute. 10 00:01:03,920 --> 00:01:14,900 If statements normally can use comparison and logical operators to check if certain conditions have 11 00:01:14,900 --> 00:01:27,920 been met or not this is a basic syntax for writing and if statement so you have the if keyword and then 12 00:01:27,950 --> 00:01:37,620 inside the parentheses you just specify the conditions and then you have this call on here. 13 00:01:37,700 --> 00:01:40,380 This cologne is very important. 14 00:01:40,460 --> 00:01:46,470 It tells the Python interpreter that you are starting a new line of code. 15 00:01:46,670 --> 00:01:53,960 So when you have this call on here it intent if you have a good text editor it will automatically indent 16 00:01:54,410 --> 00:02:00,400 into a new line that enables you to write some new line of code. 17 00:02:00,410 --> 00:02:09,070 So whatever code you're right here in the indented block that is the code that will run or execute if 18 00:02:09,080 --> 00:02:18,170 the condition that you have set in the F block is met to illustrate how the if statement works. 19 00:02:18,280 --> 00:02:24,540 I'm going to create a new file inside my PI charm idc. 20 00:02:24,580 --> 00:02:31,240 So I've already got a directory with some Python files so I'm just going to right click on my directory 21 00:02:31,810 --> 00:02:35,520 and go new and click on Pipeline file. 22 00:02:35,620 --> 00:02:43,430 I'm just going to call it if I need to save it as a python file with a top P Y extensions are quick. 23 00:02:43,430 --> 00:02:46,060 OK so now I've created a new file. 24 00:02:46,210 --> 00:02:49,960 The first thing I want to do is create a couple of variables. 25 00:02:49,960 --> 00:02:53,120 So I'm going to create a variable I'm going to call it a. 26 00:02:53,320 --> 00:02:55,960 I'm going to give it a value of seven. 27 00:02:56,080 --> 00:03:02,950 I'm going to create another variable and I'm going to call these B and I'm going to give it a family 28 00:03:02,980 --> 00:03:16,180 of eight so I want to use the if statement to check for setting conditions before I can run what's inside 29 00:03:16,180 --> 00:03:16,740 this file. 30 00:03:16,990 --> 00:03:29,890 So I'll see if a and then I'm going to use an operator to check I'm going to say if a is less than B 31 00:03:31,220 --> 00:03:35,200 and now add my call on the colon is very important. 32 00:03:35,380 --> 00:03:41,820 When I press Penta you can see is well indented indentation is very important in Python. 33 00:03:42,520 --> 00:03:46,530 So if you're using pi charm it should automatically indented for you. 34 00:03:46,540 --> 00:03:50,080 So now is prompting me to enter a new line of code. 35 00:03:50,330 --> 00:04:01,110 So I'm just going to say print and inside my print parentheses we just see the a variable comma there 36 00:04:01,160 --> 00:04:02,660 now our entire text. 37 00:04:02,760 --> 00:04:13,590 I would say is and then I do a comma to exit that string and then I'll say B B here is referring to 38 00:04:13,590 --> 00:04:15,660 the value of these variable B. 39 00:04:16,620 --> 00:04:20,790 So once I've done I just save my file. 40 00:04:20,790 --> 00:04:23,670 Now what this will do. 41 00:04:23,890 --> 00:04:33,120 Basically I'm using this operator here which is the less than sine to check for condition the condition 42 00:04:33,120 --> 00:04:44,190 I'm checking for is is the value of a variable a is a less than the variable B 7 which is the value 43 00:04:44,190 --> 00:04:49,260 of variable a is less than the value of variable B. 44 00:04:49,470 --> 00:04:51,290 So the condition is true. 45 00:04:51,570 --> 00:04:54,690 And then this block of code will execute. 46 00:04:54,780 --> 00:04:57,850 Now if the condition is false nothing happens. 47 00:04:57,900 --> 00:05:01,470 The code will not run it will just do nothing. 48 00:05:01,470 --> 00:05:07,490 So let me run this code so that you can see all right click and go play. 49 00:05:08,300 --> 00:05:18,280 You can see here is output at 7 which is the value of variable a is smaller than 8. 50 00:05:18,300 --> 00:05:21,470 Now if the condition was false nothing will happen. 51 00:05:21,480 --> 00:05:32,370 Say 5 reverses for example if I changed that to a greater than that will make the condition false because 52 00:05:32,880 --> 00:05:38,900 a which is 7 is not greater than B which is 8. 53 00:05:39,380 --> 00:05:41,940 So now let me try and run this again. 54 00:05:42,170 --> 00:05:50,280 You can see nothing runs you see the code does not execute this block here it does not execute. 55 00:05:50,390 --> 00:05:55,900 OK so what ever code you have inside the F block. 56 00:05:55,890 --> 00:05:57,040 This here. 57 00:05:57,140 --> 00:06:04,280 Well I put the print function is known as the F block so if the condition I set here is wrong or if 58 00:06:04,280 --> 00:06:14,000 it's false this code will not run the code will only run if the condition inside the F block is true 59 00:06:14,120 --> 00:06:17,040 or evaluates to true. 60 00:06:17,150 --> 00:06:27,830 So if statements are very important in your control flow always remember the indentation and the call 61 00:06:27,830 --> 00:06:29,770 on any way you see a colon here. 62 00:06:29,840 --> 00:06:36,080 You telling the interpreter that you want to start a new line of code and make sure it is indented. 63 00:06:36,110 --> 00:06:39,100 If it's not indented the code will not run. 64 00:06:39,110 --> 00:06:45,470 The interpreter will complain are set for this video on if statements. 65 00:06:45,470 --> 00:06:47,490 Thanks for watching and bye for.